Skip to content

Commit 3cb8b0a

Browse files
committed
Simplify advance_to_first_block_item
1 parent 1bd2c8f commit 3cb8b0a

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
@@ -334,8 +334,9 @@ impl<'a> FmtVisitor<'a> {
334334
self.push_str(&indent_str);
335335
} else {
336336
let first_non_ws = item.body.first().map(|s| s.span().lo());
337-
let opening_nls = &self.advance_to_first_block_item(first_non_ws);
338-
self.push_str(&opening_nls);
337+
if let Some(opening_nls) = self.advance_to_first_block_item(first_non_ws) {
338+
self.push_str(&opening_nls);
339+
}
339340

340341
for item in &item.body {
341342
self.format_body_element(item);
@@ -863,20 +864,19 @@ pub(crate) fn format_impl(
863864
visitor.block_indent = item_indent;
864865
visitor.last_pos = lo + BytePos(open_pos as u32);
865866

866-
let open_nls = &visitor.advance_to_first_block_item(
867-
inner_attributes(&item.attrs)
868-
.first()
869-
.map(|a| a.span.lo())
870-
.or_else(|| items.first().map(|i| i.span().lo())),
871-
);
867+
let first_non_ws = inner_attributes(&item.attrs)
868+
.first()
869+
.map(|a| a.span.lo())
870+
.or_else(|| items.first().map(|i| i.span().lo()));
871+
let opening_nls = visitor.advance_to_first_block_item(first_non_ws);
872872

873873
visitor.visit_attrs(&item.attrs, ast::AttrStyle::Inner);
874874
visitor.visit_impl_items(items);
875875

876876
visitor.format_missing(item.span.hi() - BytePos(1));
877877

878-
let inner_indent_str = if !open_nls.is_empty() {
879-
result.push_str(open_nls);
878+
let inner_indent_str = if let Some(opening_nls) = opening_nls {
879+
result.push_str(&opening_nls);
880880
visitor.block_indent.to_string(context.config)
881881
} else {
882882
visitor.block_indent.to_string_with_newline(context.config)
@@ -1235,17 +1235,17 @@ pub(crate) fn format_trait(
12351235
visitor.block_indent = offset.block_only().block_indent(context.config);
12361236
visitor.last_pos = block_span.lo() + BytePos(open_pos as u32);
12371237

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

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

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

1247-
let inner_indent_str = if !open_nls.is_empty() {
1248-
result.push_str(open_nls);
1247+
let inner_indent_str = if let Some(opening_nls) = opening_nls {
1248+
result.push_str(&opening_nls);
12491249
visitor.block_indent.to_string(context.config)
12501250
} else {
12511251
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
@@ -190,64 +190,59 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
190190
pub(crate) fn advance_to_first_block_item(
191191
&mut self,
192192
first_item_pos: Option<BytePos>,
193-
) -> String {
194-
let mut result = String::new();
195-
if let Some(first_item_pos) = first_item_pos {
196-
let missing_span = self.next_span(first_item_pos);
197-
let snippet = self.snippet(missing_span);
198-
199-
if self.config.preserve_block_start_blank_lines() {
200-
// First we need to find the span to look for blank lines in. This is either the
201-
// - span between the opening brace and first item, or
202-
// - span between the opening brace and a comment before the first item
203-
// We do this so that we get a span of contiguous whitespace, which makes
204-
// processing the blank lines easier.
205-
let blank_lines_snippet = if let Some(hi) = self
206-
.snippet_provider
207-
.span_to_snippet(missing_span)
208-
.and_then(|s| s.find('/'))
209-
{
210-
self.snippet_provider.span_to_snippet(mk_sp(
211-
missing_span.lo(),
212-
missing_span.lo() + BytePos::from_usize(hi),
213-
))
193+
) -> Option<String> {
194+
let missing_span = first_item_pos.map(|pos| self.next_span(pos))?;
195+
let snippet = self.snippet(missing_span);
196+
197+
let len = CommentCodeSlices::new(snippet)
198+
.next()
199+
.and_then(|(kind, _, s)| {
200+
if kind == CodeCharKind::Normal {
201+
s.rfind('\n')
214202
} else {
215-
self.snippet_provider.span_to_snippet(missing_span)
216-
};
217-
218-
if let Some(snippet) = blank_lines_snippet {
219-
let has_multiple_blank_lines =
220-
if let (Some(l), Some(r)) = (snippet.find('\n'), snippet.rfind('\n')) {
221-
l != r
222-
} else {
223-
false
224-
};
225-
if has_multiple_blank_lines {
226-
let mut lines = snippet.lines().map(&str::trim);
227-
lines.next(); // Eat block-opening newline
228-
while let Some("") = lines.next() {
229-
result.push('\n');
230-
}
231-
}
232-
} else {
233-
debug!("Failed to preserve blank lines for {:?}", missing_span);
203+
None
234204
}
235-
}
205+
});
206+
if let Some(len) = len {
207+
self.last_pos = self.last_pos + BytePos::from_usize(len);
208+
}
236209

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

253248
pub(crate) fn visit_block(
@@ -271,8 +266,9 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
271266
let first_non_ws = inner_attrs
272267
.and_then(|attrs| attrs.first().map(|attr| attr.span.lo()))
273268
.or_else(|| b.stmts.first().map(|s| s.span().lo()));
274-
let opening_nls = &self.advance_to_first_block_item(first_non_ws);
275-
self.push_str(&opening_nls);
269+
if let Some(opening_nls) = self.advance_to_first_block_item(first_non_ws) {
270+
self.push_str(&opening_nls);
271+
}
276272

277273
// Format inner attributes if available.
278274
if let Some(attrs) = inner_attrs {
@@ -1003,8 +999,9 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
1003999
.first()
10041000
.map(|attr| attr.span.lo())
10051001
.or_else(|| m.items.first().map(|s| s.span().lo()));
1006-
let opening_nls = &self.advance_to_first_block_item(first_non_ws);
1007-
self.push_str(&opening_nls);
1002+
if let Some(opening_nls) = self.advance_to_first_block_item(first_non_ws) {
1003+
self.push_str(&opening_nls);
1004+
}
10081005

10091006
self.visit_attrs(attrs, ast::AttrStyle::Inner);
10101007
self.walk_mod_items(m);

0 commit comments

Comments
 (0)