Skip to content

Commit 1e2d3a1

Browse files
committed
Simplify advance_to_first_block_item
1 parent f0f3194 commit 1e2d3a1

File tree

2 files changed

+67
-70
lines changed

2 files changed

+67
-70
lines changed

src/formatting/items.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,9 @@ impl<'a> FmtVisitor<'a> {
342342
self.push_str(&indent_str);
343343
} else {
344344
let first_non_ws = item.body.first().map(|s| s.span().lo());
345-
let opening_nls = &self.advance_to_first_block_item(first_non_ws);
346-
self.push_str(&opening_nls);
345+
if let Some(opening_nls) = self.advance_to_first_block_item(first_non_ws) {
346+
self.push_str(&opening_nls);
347+
}
347348

348349
for item in &item.body {
349350
self.format_body_element(item);
@@ -866,20 +867,19 @@ pub(crate) fn format_impl(
866867
visitor.block_indent = item_indent;
867868
visitor.last_pos = lo + BytePos(open_pos as u32);
868869

869-
let open_nls = &visitor.advance_to_first_block_item(
870-
inner_attributes(&item.attrs)
871-
.first()
872-
.map(|a| a.span.lo())
873-
.or_else(|| items.first().map(|i| i.span().lo())),
874-
);
870+
let first_non_ws = inner_attributes(&item.attrs)
871+
.first()
872+
.map(|a| a.span.lo())
873+
.or_else(|| items.first().map(|i| i.span().lo()));
874+
let opening_nls = visitor.advance_to_first_block_item(first_non_ws);
875875

876876
visitor.visit_attrs(&item.attrs, ast::AttrStyle::Inner);
877877
visitor.visit_impl_items(items);
878878

879879
visitor.format_missing(item.span.hi() - BytePos(1));
880880

881-
let inner_indent_str = if !open_nls.is_empty() {
882-
result.push_str(open_nls);
881+
let inner_indent_str = if let Some(opening_nls) = opening_nls {
882+
result.push_str(&opening_nls);
883883
visitor.block_indent.to_string(context.config)
884884
} else {
885885
visitor.block_indent.to_string_with_newline(context.config)
@@ -1234,17 +1234,17 @@ pub(crate) fn format_trait(
12341234
visitor.block_indent = offset.block_only().block_indent(context.config);
12351235
visitor.last_pos = block_span.lo() + BytePos(open_pos as u32);
12361236

1237-
let open_nls =
1238-
&visitor.advance_to_first_block_item(trait_items.first().map(|i| i.span().lo()));
1237+
let opening_nls =
1238+
visitor.advance_to_first_block_item(trait_items.first().map(|i| i.span().lo()));
12391239

12401240
for item in trait_items {
12411241
visitor.visit_trait_item(item);
12421242
}
12431243

12441244
visitor.format_missing(item.span.hi() - BytePos(1));
12451245

1246-
let inner_indent_str = if !open_nls.is_empty() {
1247-
result.push_str(open_nls);
1246+
let inner_indent_str = if let Some(opening_nls) = opening_nls {
1247+
result.push_str(&opening_nls);
12481248
visitor.block_indent.to_string(context.config)
12491249
} else {
12501250
visitor.block_indent.to_string_with_newline(context.config)

src/formatting/visitor.rs

Lines changed: 53 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -188,64 +188,59 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
188188
pub(crate) fn advance_to_first_block_item(
189189
&mut self,
190190
first_item_pos: Option<BytePos>,
191-
) -> String {
192-
let mut result = String::new();
193-
if let Some(first_item_pos) = first_item_pos {
194-
let missing_span = self.next_span(first_item_pos);
195-
let snippet = self.snippet(missing_span);
196-
197-
if self.config.preserve_block_start_blank_lines() {
198-
// First we need to find the span to look for blank lines in. This is either the
199-
// - span between the opening brace and first item, or
200-
// - span between the opening brace and a comment before the first item
201-
// We do this so that we get a span of contiguous whitespace, which makes
202-
// processing the blank lines easier.
203-
let blank_lines_snippet = if let Some(hi) = self
204-
.snippet_provider
205-
.span_to_snippet(missing_span)
206-
.and_then(|s| s.find('/'))
207-
{
208-
self.snippet_provider.span_to_snippet(mk_sp(
209-
missing_span.lo(),
210-
missing_span.lo() + BytePos::from_usize(hi),
211-
))
191+
) -> Option<String> {
192+
let missing_span = first_item_pos.map(|pos| self.next_span(pos))?;
193+
let snippet = self.snippet(missing_span);
194+
195+
let len = CommentCodeSlices::new(snippet)
196+
.next()
197+
.and_then(|(kind, _, s)| {
198+
if kind == CodeCharKind::Normal {
199+
s.rfind('\n')
212200
} else {
213-
self.snippet_provider.span_to_snippet(missing_span)
214-
};
215-
216-
if let Some(snippet) = blank_lines_snippet {
217-
let has_multiple_blank_lines =
218-
if let (Some(l), Some(r)) = (snippet.find('\n'), snippet.rfind('\n')) {
219-
l != r
220-
} else {
221-
false
222-
};
223-
if has_multiple_blank_lines {
224-
let mut lines = snippet.lines().map(&str::trim);
225-
lines.next(); // Eat block-opening newline
226-
while let Some("") = lines.next() {
227-
result.push('\n');
228-
}
229-
}
230-
} else {
231-
debug!("Failed to preserve blank lines for {:?}", missing_span);
201+
None
232202
}
233-
}
203+
});
204+
if let Some(len) = len {
205+
self.last_pos = self.last_pos + BytePos::from_usize(len);
206+
}
234207

235-
let len = CommentCodeSlices::new(snippet)
236-
.next()
237-
.and_then(|(kind, _, s)| {
238-
if kind == CodeCharKind::Normal {
239-
s.rfind('\n')
240-
} else {
241-
None
208+
if self.config.preserve_block_start_blank_lines() {
209+
// First we need to find the span to look for blank lines in. This is either the
210+
// - span between the opening brace and first item, or
211+
// - span between the opening brace and a comment before the first item
212+
// We do this so that we get a span of contiguous whitespace, which makes processing the
213+
// blank lines easier.
214+
let blank_lines_snippet = if let Some(hi) = self
215+
.snippet_provider
216+
.span_to_snippet(missing_span)
217+
.and_then(|s| s.find('/'))
218+
{
219+
self.snippet_provider.span_to_snippet(mk_sp(
220+
missing_span.lo(),
221+
missing_span.lo() + BytePos::from_usize(hi),
222+
))
223+
} else {
224+
self.snippet_provider.span_to_snippet(missing_span)
225+
};
226+
227+
if let Some(snippet) = blank_lines_snippet {
228+
if snippet.find('\n') != snippet.rfind('\n') {
229+
let mut lines = snippet.lines().map(&str::trim);
230+
lines.next(); // Eat block-opening newline
231+
let mut result = String::new();
232+
while let Some("") = lines.next() {
233+
result.push('\n');
242234
}
243-
});
244-
if let Some(len) = len {
245-
self.last_pos = self.last_pos + BytePos::from_usize(len);
235+
if !result.is_empty() {
236+
return Some(result);
237+
}
238+
}
239+
} else {
240+
debug!("Failed to preserve blank lines for {:?}", missing_span);
246241
}
247242
}
248-
result
243+
None
249244
}
250245

251246
pub(crate) fn visit_block(
@@ -269,8 +264,9 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
269264
let first_non_ws = inner_attrs
270265
.and_then(|attrs| attrs.first().map(|attr| attr.span.lo()))
271266
.or_else(|| b.stmts.first().map(|s| s.span().lo()));
272-
let opening_nls = &self.advance_to_first_block_item(first_non_ws);
273-
self.push_str(&opening_nls);
267+
if let Some(opening_nls) = self.advance_to_first_block_item(first_non_ws) {
268+
self.push_str(&opening_nls);
269+
}
274270

275271
// Format inner attributes if available.
276272
if let Some(attrs) = inner_attrs {
@@ -995,8 +991,9 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
995991
.first()
996992
.map(|attr| attr.span.lo())
997993
.or_else(|| m.items.first().map(|s| s.span().lo()));
998-
let opening_nls = &self.advance_to_first_block_item(first_non_ws);
999-
self.push_str(&opening_nls);
994+
if let Some(opening_nls) = self.advance_to_first_block_item(first_non_ws) {
995+
self.push_str(&opening_nls);
996+
}
1000997

1001998
self.visit_attrs(attrs, ast::AttrStyle::Inner);
1002999
self.walk_mod_items(m);

0 commit comments

Comments
 (0)