Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit cd8549e

Browse files
authored
Merge pull request rust-lang#3036 from topecongiro/issue-2932
Combine chain items only when the item gets orphaned otherwise
2 parents 635efdf + e2b9c66 commit cd8549e

35 files changed

+191
-116
lines changed

src/attr.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,8 @@ impl Rewrite for ast::MetaItem {
229229
None,
230230
&inner_meta_item.ident,
231231
shape,
232-
).map_or(false, |s| s.len() + path.len() + 2 <= shape.width),
232+
)
233+
.map_or(false, |s| s.len() + path.len() + 2 <= shape.width),
233234
_ => false,
234235
}
235236
}

src/bin/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,8 @@ fn determine_operation(matches: &Matches) -> Result<Operation, ErrorKind> {
421421
// we will do comparison later, so here tries to canonicalize first
422422
// to get the expected behavior.
423423
p.canonicalize().unwrap_or(p)
424-
}).collect();
424+
})
425+
.collect();
425426

426427
Ok(Operation::Format {
427428
files,

src/cargo-fmt/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,8 @@ fn format_crate(
163163
if verbosity == Verbosity::Verbose {
164164
println!("[{}] {:?}", t.kind, t.path)
165165
}
166-
}).map(|t| t.path)
166+
})
167+
.map(|t| t.path)
167168
.collect();
168169

169170
run_rustfmt(&files, &rustfmt_args, verbosity)

src/chains.rs

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ use utils::{
8080

8181
use std::borrow::Cow;
8282
use std::cmp::min;
83-
use std::iter;
8483

8584
use syntax::source_map::{BytePos, Span};
8685
use syntax::{ast, ptr};
@@ -132,8 +131,8 @@ impl ChainItemKind {
132131
fn is_block_like(&self, context: &RewriteContext, reps: &str) -> bool {
133132
match self {
134133
ChainItemKind::Parent(ref expr) => is_block_expr(context, expr, reps),
135-
ChainItemKind::MethodCall(..) => reps.contains('\n'),
136-
ChainItemKind::StructField(..)
134+
ChainItemKind::MethodCall(..)
135+
| ChainItemKind::StructField(..)
137136
| ChainItemKind::TupleField(..)
138137
| ChainItemKind::Comment(..) => false,
139138
}
@@ -559,7 +558,8 @@ impl<'a> ChainFormatterShared<'a> {
559558
shape.width
560559
} else {
561560
min(shape.width, context.config.width_heuristics().chain_width)
562-
}.saturating_sub(almost_total);
561+
}
562+
.saturating_sub(almost_total);
563563

564564
let all_in_one_line = !self.children.iter().any(ChainItem::is_comment)
565565
&& self.rewrites.iter().all(|s| !s.contains('\n'))
@@ -625,12 +625,7 @@ impl<'a> ChainFormatterShared<'a> {
625625
Some(())
626626
}
627627

628-
fn join_rewrites(
629-
&self,
630-
context: &RewriteContext,
631-
child_shape: Shape,
632-
block_like_iter: impl Iterator<Item = bool>,
633-
) -> Option<String> {
628+
fn join_rewrites(&self, context: &RewriteContext, child_shape: Shape) -> Option<String> {
634629
let connector = if self.fits_single_line {
635630
// Yay, we can put everything on one line.
636631
Cow::from("")
@@ -645,17 +640,13 @@ impl<'a> ChainFormatterShared<'a> {
645640
let mut rewrite_iter = self.rewrites.iter();
646641
let mut result = rewrite_iter.next().unwrap().clone();
647642
let children_iter = self.children.iter();
648-
let iter = rewrite_iter.zip(block_like_iter).zip(children_iter);
643+
let iter = rewrite_iter.zip(children_iter);
649644

650-
for ((rewrite, prev_is_block_like), chain_item) in iter {
645+
for (rewrite, chain_item) in iter {
651646
match chain_item.kind {
652647
ChainItemKind::Comment(_, CommentPosition::Back) => result.push(' '),
653648
ChainItemKind::Comment(_, CommentPosition::Top) => result.push_str(&connector),
654-
_ => {
655-
if !prev_is_block_like {
656-
result.push_str(&connector);
657-
}
658-
}
649+
_ => result.push_str(&connector),
659650
}
660651
result.push_str(&rewrite);
661652
}
@@ -667,15 +658,14 @@ impl<'a> ChainFormatterShared<'a> {
667658
// Formats a chain using block indent.
668659
struct ChainFormatterBlock<'a> {
669660
shared: ChainFormatterShared<'a>,
670-
// For each rewrite, whether the corresponding item is block-like.
671-
is_block_like: Vec<bool>,
661+
root_ends_with_block: bool,
672662
}
673663

674664
impl<'a> ChainFormatterBlock<'a> {
675665
fn new(chain: &'a Chain) -> ChainFormatterBlock<'a> {
676666
ChainFormatterBlock {
677667
shared: ChainFormatterShared::new(chain),
678-
is_block_like: Vec::with_capacity(chain.children.len() + 1),
668+
root_ends_with_block: false,
679669
}
680670
}
681671
}
@@ -703,33 +693,32 @@ impl<'a> ChainFormatter for ChainFormatterBlock<'a> {
703693
None => break,
704694
}
705695

706-
root_ends_with_block = item.kind.is_block_like(context, &root_rewrite);
696+
root_ends_with_block = last_line_extendable(&root_rewrite);
707697

708698
self.shared.children = &self.shared.children[1..];
709699
if self.shared.children.is_empty() {
710700
break;
711701
}
712702
}
713-
self.is_block_like.push(root_ends_with_block);
714703
self.shared.rewrites.push(root_rewrite);
704+
self.root_ends_with_block = root_ends_with_block;
715705
Some(())
716706
}
717707

718708
fn child_shape(&self, context: &RewriteContext, shape: Shape) -> Option<Shape> {
719709
Some(
720-
if self.is_block_like[0] {
710+
if self.root_ends_with_block {
721711
shape.block_indent(0)
722712
} else {
723713
shape.block_indent(context.config.tab_spaces())
724-
}.with_max_width(context.config),
714+
}
715+
.with_max_width(context.config),
725716
)
726717
}
727718

728719
fn format_children(&mut self, context: &RewriteContext, child_shape: Shape) -> Option<()> {
729720
for item in &self.shared.children[..self.shared.children.len() - 1] {
730721
let rewrite = item.rewrite(context, child_shape)?;
731-
self.is_block_like
732-
.push(item.kind.is_block_like(context, &rewrite));
733722
self.shared.rewrites.push(rewrite);
734723
}
735724
Some(())
@@ -746,8 +735,7 @@ impl<'a> ChainFormatter for ChainFormatterBlock<'a> {
746735
}
747736

748737
fn join_rewrites(&self, context: &RewriteContext, child_shape: Shape) -> Option<String> {
749-
self.shared
750-
.join_rewrites(context, child_shape, self.is_block_like.iter().cloned())
738+
self.shared.join_rewrites(context, child_shape)
751739
}
752740

753741
fn pure_root(&mut self) -> Option<String> {
@@ -841,8 +829,7 @@ impl<'a> ChainFormatter for ChainFormatterVisual<'a> {
841829
}
842830

843831
fn join_rewrites(&self, context: &RewriteContext, child_shape: Shape) -> Option<String> {
844-
self.shared
845-
.join_rewrites(context, child_shape, iter::repeat(false))
832+
self.shared.join_rewrites(context, child_shape)
846833
}
847834

848835
fn pure_root(&mut self) -> Option<String> {

src/closures.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,8 @@ fn rewrite_closure_expr(
198198
} else {
199199
Some(rw)
200200
}
201-
}).map(|rw| format!("{} {}", prefix, rw))
201+
})
202+
.map(|rw| format!("{} {}", prefix, rw))
202203
}
203204

204205
// Rewrite closure whose body is block.
@@ -367,8 +368,10 @@ where
367368
.map(|e| match e.node {
368369
ast::ExprKind::Closure(..) => true,
369370
_ => false,
370-
}).unwrap_or(false)
371-
}).count()
371+
})
372+
.unwrap_or(false)
373+
})
374+
.count()
372375
> 1
373376
}
374377

src/comment.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,8 @@ fn rewrite_comment_inner(
370370
}
371371

372372
line
373-
}).map(|s| left_trim_comment_line(s, &style))
373+
})
374+
.map(|s| left_trim_comment_line(s, &style))
374375
.map(|(line, has_leading_whitespace)| {
375376
if orig.starts_with("/*") && line_breaks == 0 {
376377
(
@@ -542,7 +543,8 @@ fn trim_custom_comment_prefix(s: &str) -> String {
542543
} else {
543544
line
544545
}
545-
}).collect::<Vec<_>>()
546+
})
547+
.collect::<Vec<_>>()
546548
.join("\n")
547549
}
548550

@@ -630,7 +632,8 @@ fn light_rewrite_comment(
630632
};
631633
// Preserve markdown's double-space line break syntax in doc comment.
632634
trim_right_unless_two_whitespaces(left_trimmed, is_doc_comment)
633-
}).collect();
635+
})
636+
.collect();
634637
Some(lines.join(&format!("\n{}", offset.to_string(config))))
635638
}
636639

@@ -1455,7 +1458,8 @@ mod test {
14551458
.filter_map(|(s, c)| match s {
14561459
FullCodeCharKind::Normal | FullCodeCharKind::InString => Some(c),
14571460
_ => None,
1458-
}).collect()
1461+
})
1462+
.collect()
14591463
}
14601464

14611465
#[test]

src/config/file_lines.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,8 @@ impl FileLines {
216216
.map(|(file, range)| JsonSpan {
217217
file: file.to_owned(),
218218
range: (range.lo, range.hi),
219-
}).collect(),
219+
})
220+
.collect(),
220221
}
221222
}
222223

src/config/options.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,8 @@ impl IgnoreList {
394394
path.push(s);
395395
path
396396
}
397-
}).collect();
397+
})
398+
.collect();
398399
}
399400

400401
fn skip_file_inner(&self, file: &Path) -> bool {

src/expr.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,7 +1029,8 @@ impl<'a> Rewrite for ControlFlow<'a> {
10291029
false,
10301030
true,
10311031
mk_sp(else_block.span.lo(), self.span.hi()),
1032-
).rewrite(context, shape)
1032+
)
1033+
.rewrite(context, shape)
10331034
}
10341035
ast::ExprKind::If(ref cond, ref if_block, ref next_else_block) => {
10351036
ControlFlow::new_if(
@@ -1040,7 +1041,8 @@ impl<'a> Rewrite for ControlFlow<'a> {
10401041
false,
10411042
true,
10421043
mk_sp(else_block.span.lo(), self.span.hi()),
1043-
).rewrite(context, shape)
1044+
)
1045+
.rewrite(context, shape)
10441046
}
10451047
_ => {
10461048
last_in_chain = true;
@@ -1237,7 +1239,8 @@ fn rewrite_string_lit(context: &RewriteContext, span: Span, shape: Shape) -> Opt
12371239
new_indent.to_string(context.config),
12381240
line.trim_left()
12391241
)
1240-
}).collect::<Vec<_>>()
1242+
})
1243+
.collect::<Vec<_>>()
12411244
.join("\n")
12421245
.trim_left(),
12431246
);
@@ -1486,7 +1489,12 @@ fn rewrite_index(
14861489
.offset_left(offset)
14871490
.and_then(|shape| shape.sub_width(1 + rhs_overhead))
14881491
} else {
1489-
shape.visual_indent(offset).sub_width(offset + 1)
1492+
match context.config.indent_style() {
1493+
IndentStyle::Block => shape
1494+
.offset_left(offset)
1495+
.and_then(|shape| shape.sub_width(1)),
1496+
IndentStyle::Visual => shape.visual_indent(offset).sub_width(offset + 1),
1497+
}
14901498
};
14911499
let orig_index_rw = index_shape.and_then(|s| index.rewrite(context, s));
14921500

src/formatting.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,8 @@ impl FormattingError {
240240
fl.file
241241
.get_line(fl.lines[0].line_index)
242242
.map(|l| l.into_owned())
243-
}).unwrap_or_else(String::new),
243+
})
244+
.unwrap_or_else(String::new),
244245
}
245246
}
246247

src/git-rustfmt/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ fn prune_files(files: Vec<&str>) -> Vec<&str> {
4646
return true;
4747
}
4848
pruned_prefixes.iter().all(|pp| !f.starts_with(pp))
49-
}).collect()
49+
})
50+
.collect()
5051
}
5152

5253
fn git_diff(commits: &str) -> String {

src/imports.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ impl<'a> FmtVisitor<'a> {
4444
Some(item.vis.clone()),
4545
Some(item.span.lo()),
4646
Some(item.attrs.clone()),
47-
).rewrite_top_level(&self.get_context(), shape);
47+
)
48+
.rewrite_top_level(&self.get_context(), shape);
4849
match rw {
4950
Some(ref s) if s.is_empty() => {
5051
// Format up to last newline
@@ -291,7 +292,8 @@ impl UseTree {
291292
} else {
292293
Some(item.attrs.clone())
293294
},
294-
).normalize(),
295+
)
296+
.normalize(),
295297
),
296298
_ => None,
297299
}
@@ -345,13 +347,15 @@ impl UseTree {
345347
context.snippet_provider.span_after(a.span, "{"),
346348
a.span.hi(),
347349
false,
348-
).collect();
350+
)
351+
.collect();
349352
result.path.push(UseSegment::List(
350353
list.iter()
351354
.zip(items.into_iter())
352355
.map(|(t, list_item)| {
353356
Self::from_ast(context, &t.0, Some(list_item), None, None, None)
354-
}).collect(),
357+
})
358+
.collect(),
355359
));
356360
}
357361
UseTreeKind::Simple(ref rename, ..) => {

0 commit comments

Comments
 (0)