Skip to content

Commit 8ec76e5

Browse files
tests: add more tests for long chain elements
1 parent 9767afe commit 8ec76e5

File tree

5 files changed

+130
-42
lines changed

5 files changed

+130
-42
lines changed

src/chains.rs

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -892,13 +892,15 @@ struct ChainFormatterVisual<'a> {
892892
shared: ChainFormatterShared<'a>,
893893
// The extra offset from the chain's shape to the position of the `.`
894894
offset: usize,
895+
has_long_first_child: bool,
895896
}
896897

897898
impl<'a> ChainFormatterVisual<'a> {
898899
fn new(chain: &'a Chain) -> ChainFormatterVisual<'a> {
899900
ChainFormatterVisual {
900901
shared: ChainFormatterShared::new(chain),
901902
offset: 0,
903+
has_long_first_child: false,
902904
}
903905
}
904906
}
@@ -911,8 +913,8 @@ impl<'a> ChainFormatter for ChainFormatterVisual<'a> {
911913
shape: Shape,
912914
) -> Option<()> {
913915
let parent_shape = shape.visual_indent(0);
914-
let (mut root_rewrite, is_long) = format_chain_item(parent, context, parent_shape)?;
915-
self.shared.has_long_parent = is_long;
916+
let (mut root_rewrite, parent_too_long) = format_chain_item(parent, context, parent_shape)?;
917+
self.shared.has_long_parent = parent_too_long;
916918
let multiline = root_rewrite.contains('\n');
917919
self.offset = if multiline {
918920
last_line_width(&root_rewrite).saturating_sub(shape.used_width())
@@ -926,18 +928,34 @@ impl<'a> ChainFormatter for ChainFormatterVisual<'a> {
926928
self.shared.rewrites.push(root_rewrite);
927929
return Some(());
928930
}
929-
let child_shape = parent_shape
930-
.visual_indent(self.offset)
931-
.sub_width(self.offset)?;
932-
let rewrite = item.rewrite(context, child_shape)?;
931+
let child_shape = if !self.shared.has_long_parent {
932+
parent_shape
933+
.visual_indent(self.offset)
934+
.sub_width(self.offset)?
935+
} else {
936+
let child_shape = shape.visual_indent(0);
937+
root_rewrite.push_str(&child_shape.to_string_with_newline(context.config));
938+
child_shape
939+
};
940+
941+
let (rewrite, first_child_too_long) = format_chain_item(item, context, child_shape)?;
942+
self.has_long_first_child = first_child_too_long;
943+
933944
match wrap_str(rewrite, context.config.max_width(), shape) {
934945
Some(rewrite) => root_rewrite.push_str(&rewrite),
935946
None => {
936947
// We couldn't fit in at the visual indent, try the last
937948
// indent.
938-
let rewrite = item.rewrite(context, parent_shape)?;
949+
let (rewrite, too_long) = format_chain_item(item, context, parent_shape)?;
950+
if too_long
951+
|| wrap_str(rewrite.clone(), context.config.max_width(), parent_shape)
952+
.is_none()
953+
{
954+
self.has_long_first_child = true;
955+
} else {
956+
self.offset = 0;
957+
}
939958
root_rewrite.push_str(&rewrite);
940-
self.offset = 0;
941959
}
942960
}
943961

@@ -949,10 +967,14 @@ impl<'a> ChainFormatter for ChainFormatterVisual<'a> {
949967
}
950968

951969
fn child_shape(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<Shape> {
952-
shape
953-
.with_max_width(context.config)
954-
.offset_left(self.offset)
955-
.map(|s| s.visual_indent(0))
970+
if !self.shared.has_long_parent {
971+
shape
972+
.with_max_width(context.config)
973+
.offset_left(self.offset)
974+
.map(|s| s.visual_indent(0))
975+
} else {
976+
Some(shape.visual_indent(0))
977+
}
956978
}
957979

958980
fn format_children(&mut self, context: &RewriteContext<'_>, child_shape: Shape) -> Option<()> {
@@ -983,7 +1005,9 @@ impl<'a> ChainFormatter for ChainFormatterVisual<'a> {
9831005
shape: Shape,
9841006
) -> Option<String> {
9851007
match self.shared.join_rewrites(context, child_shape) {
986-
Some(rewrite) if self.shared.has_long_item() => Some(rewrite),
1008+
Some(rewrite) if self.shared.has_long_item() || self.has_long_first_child => {
1009+
Some(rewrite)
1010+
}
9871011
Some(rewrite) => wrap_str(rewrite, context.config.max_width(), shape),
9881012
None => None,
9891013
}

tests/source/chains_long_items_block.rs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,17 @@ fn issue_3863() {
66
}
77

88
fn long_parent() {
9+
// Args that do not fit
10+
let bar = baz("ffffffffffffffffffffffffffffffffffffasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfadfasdfasdfasdfadfasdfasdf").foo().bar().baz();
11+
12+
baz("ffffffffffffffffffffffffffffffffffffasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfadfasdfasdfasdfadfasdfasdf").foo().bar().baz();
13+
14+
// Long element no args
915
let foo = looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooonnnnnnnnnnnnnnnnnnnnnnnnggggggggggggggggggggggggggggggggggggggggggg().foo().bar().baz();
1016

1117
asdffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff().foo().bar().baz();
1218

13-
// With args
19+
// Long element with args that fit
1420
let bar = looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooonnnnnnnnnnnnnnnnnnnnnnnnggggggggggggggggggggggggggggggggggggggggggg("ffffffffffffffffffffffffffffffffffff").foo().bar().baz();
1521

1622
asdffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff("ffffffffffffffffffffffffffffffffffff").foo().bar().baz();
@@ -22,24 +28,31 @@ fn long_inner() {
2228

2329
qux().baz("ffffffffffffffffffffffffffffffffffffasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfadfasdfasdfasdfadfasdfasdf").foo().bar().baz();
2430

31+
// Long element with args that fit
32+
let bar = bar().asdffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff("ffffffffffffffffffffffffffffffffffff").foo().bar().baz();
33+
34+
qux().asdffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff("ffffffffffffffffffffffffffffffffffff").foo().bar().baz();
35+
2536
// Long element no args
2637
let foo = bar().asdffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff().foo().bar().baz();
2738

2839
qux().asdffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff().foo().bar().baz();
40+
}
41+
42+
fn long_tail() {
43+
// Args that do not fit
44+
bar().xxxxxxx.map(|x| x + 5).map(|x| x / 2).fold(0, |acc, x| acc + x).baz("fadfasdf39ru8ahsdfasdfasdfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
45+
46+
let foo = bar().xxxxxxx.map(|x| x + 5).map(|x| x / 2).fold(0, |acc, x| acc + x).baz("fadfasdf39ru8ahsdfasdfasdfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
2947

3048
// Long element with args that fit
31-
let bar = bar().asdffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff("ffffffffffffffffffffffffffffffffffff").foo().bar().baz();
49+
bar().xxxxxxx.map(|x| x + 5).map(|x| x / 2).fold(0, |acc, x| acc + x).doooooooooooooooooooooooooooooooooooooooooooooooooooooo_stufffffffffffffffffffffffffffffffffffffff("abc123def456");
3250

33-
qux().asdffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff("ffffffffffffffffffffffffffffffffffff").foo().bar().baz();
34-
}
51+
let foo = bar().xxxxxxx.map(|x| x + 5).map(|x| x / 2).fold(0, |acc, x| acc + x).doooooooooooooooooooooooooooooooooooooooooooooooooooooo_stufffffffffffffffffffffffffffffffffffffff("abc123def456");
3552

36-
fn long_tail() {
53+
// Long element no args
3754
bar().xxxxxxx.map(|x| x + 5).map(|x| x / 2).fold(0, |acc, x| acc + x).doooooooooooooooooooooooooooooooooooooooooooooooooooooo_stufffffffffffffffffffffffffffffffffffffff();
3855

3956
let foo = bar().xxxxxxx.map(|x| x + 5).map(|x| x / 2).fold(0, |acc, x| acc + x).doooooooooooooooooooooooooooooooooooooooooooooooooooooo_stufffffffffffffffffffffffffffffffffffffff();
4057

41-
// With args
42-
bar().xxxxxxx.map(|x| x + 5).map(|x| x / 2).fold(0, |acc, x| acc + x).doooooooooooooooooooooooooooooooooooooooooooooooooooooo_stufffffffffffffffffffffffffffffffffffffff("abcdefghadfasdfasdfasdfasdfadf");
43-
44-
let foo = bar().xxxxxxx.map(|x| x + 5).map(|x| x / 2).fold(0, |acc, x| acc + x).doooooooooooooooooooooooooooooooooooooooooooooooooooooo_stufffffffffffffffffffffffffffffffffffffff("abcdefghadfasdfasdfasdfasdfadf");
4558
}
Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
// rustfmt-indent_style: Visual
22

3-
fn long_inner() {
3+
fn long_parent() {
4+
let bar = bazffffffffffffffffffffffffffffffffffffasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfadfasdfasdfasdfadfasdfasdf().foo().bar().baz();
5+
6+
bazffffffffffffffffffffffffffffffffffffasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfadfasdfasdfasdfadfasdfasdf().foo().foo().bar().baz();
7+
8+
}
9+
10+
fn long_first_child() {
411
let foo = bar().asdffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff().foo().bar().baz();
12+
513
qux().asdffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff().foo().bar().baz();
614
}
715

816
fn long_tail() {
917
bar().xxxxxxx.map(|x| x + 5).map(|x| x / 2).fold(0, |acc, x| acc + x).doooooooooooooooooooooooooooooooooooooooooooooooooooooo_stufffffffffffffffffffffffffffffffffffffff("abcdefghadfasdfasdfasdfasdfadf");
10-
}
11-
12-
fn assignment_long_tail() {
13-
let foo = bar().xxxxxxx.map(|x| x + 5).map(|x| x / 2).fold(0, |acc, x| acc + x).doooooooooooooooooooooooooooooooooooooooooooooooooooooo_stufffffffffffffffffffffffffffffffffffffff("abcdefghadfasdfasdfasdfasdfadf");
1418

19+
let foo = bar().xxxxxxx.map(|x| x + 5).map(|x| x / 2).fold(0, |acc, x| acc + x).doooooooooooooooooooooooooooooooooooooooooooooooooooooo_stufffffffffffffffffffffffffffffffffffffff("abcdefghadfasdfasdfasdfasdfadf");
1520
}

tests/target/chains_long_items_block.rs

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@ fn issue_3863() {
1010
}
1111

1212
fn long_parent() {
13+
// Args that do not fit
14+
let bar = baz("ffffffffffffffffffffffffffffffffffffasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfadfasdfasdfasdfadfasdfasdf")
15+
.foo()
16+
.bar()
17+
.baz();
18+
19+
baz("ffffffffffffffffffffffffffffffffffffasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfadfasdfasdfasdfadfasdfasdf")
20+
.foo()
21+
.bar()
22+
.baz();
23+
24+
// Long element no args
1325
let foo = looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooonnnnnnnnnnnnnnnnnnnnnnnnggggggggggggggggggggggggggggggggggggggggggg()
1426
.foo()
1527
.bar()
@@ -20,7 +32,7 @@ fn long_parent() {
2032
.bar()
2133
.baz();
2234

23-
// With args
35+
// Long element with args that fit
2436
let bar = looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooonnnnnnnnnnnnnnnnnnnnnnnnggggggggggggggggggggggggggggggggggggggggggg("ffffffffffffffffffffffffffffffffffff")
2537
.foo()
2638
.bar()
@@ -46,46 +58,63 @@ fn long_inner() {
4658
.bar()
4759
.baz();
4860

49-
// Long element no args
50-
let foo = bar()
61+
// Long element with args that fit
62+
let bar = bar()
5163
.asdffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff(
64+
"ffffffffffffffffffffffffffffffffffff",
5265
)
5366
.foo()
5467
.bar()
5568
.baz();
5669

5770
qux()
5871
.asdffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff(
72+
"ffffffffffffffffffffffffffffffffffff",
5973
)
6074
.foo()
6175
.bar()
6276
.baz();
6377

64-
// Long element with args that fit
65-
let bar = bar()
78+
// Long element no args
79+
let foo = bar()
6680
.asdffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff(
67-
"ffffffffffffffffffffffffffffffffffff",
6881
)
6982
.foo()
7083
.bar()
7184
.baz();
7285

7386
qux()
7487
.asdffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff(
75-
"ffffffffffffffffffffffffffffffffffff",
7688
)
7789
.foo()
7890
.bar()
7991
.baz();
8092
}
8193

8294
fn long_tail() {
95+
// Args that do not fit
96+
bar()
97+
.xxxxxxx
98+
.map(|x| x + 5)
99+
.map(|x| x / 2)
100+
.fold(0, |acc, x| acc + x)
101+
.baz("fadfasdf39ru8ahsdfasdfasdfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
102+
103+
let foo = bar()
104+
.xxxxxxx
105+
.map(|x| x + 5)
106+
.map(|x| x / 2)
107+
.fold(0, |acc, x| acc + x)
108+
.baz("fadfasdf39ru8ahsdfasdfasdfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
109+
110+
// Long element with args that fit
83111
bar()
84112
.xxxxxxx
85113
.map(|x| x + 5)
86114
.map(|x| x / 2)
87115
.fold(0, |acc, x| acc + x)
88116
.doooooooooooooooooooooooooooooooooooooooooooooooooooooo_stufffffffffffffffffffffffffffffffffffffff(
117+
"abc123def456",
89118
);
90119

91120
let foo = bar()
@@ -94,16 +123,16 @@ fn long_tail() {
94123
.map(|x| x / 2)
95124
.fold(0, |acc, x| acc + x)
96125
.doooooooooooooooooooooooooooooooooooooooooooooooooooooo_stufffffffffffffffffffffffffffffffffffffff(
126+
"abc123def456",
97127
);
98128

99-
// With args
129+
// Long element no args
100130
bar()
101131
.xxxxxxx
102132
.map(|x| x + 5)
103133
.map(|x| x / 2)
104134
.fold(0, |acc, x| acc + x)
105135
.doooooooooooooooooooooooooooooooooooooooooooooooooooooo_stufffffffffffffffffffffffffffffffffffffff(
106-
"abcdefghadfasdfasdfasdfasdfadf",
107136
);
108137

109138
let foo = bar()
@@ -112,6 +141,5 @@ fn long_tail() {
112141
.map(|x| x / 2)
113142
.fold(0, |acc, x| acc + x)
114143
.doooooooooooooooooooooooooooooooooooooooooooooooooooooo_stufffffffffffffffffffffffffffffffffffffff(
115-
"abcdefghadfasdfasdfasdfasdfadf",
116144
);
117145
}

tests/target/chains_long_items_visual.rs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,28 @@
11
// rustfmt-indent_style: Visual
22

3-
fn long_inner() {
4-
let foo = bar().asdffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff().foo().bar().baz();
5-
qux().asdffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff().foo().bar().baz();
3+
fn long_parent() {
4+
let bar = bazffffffffffffffffffffffffffffffffffffasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfadfasdfasdfasdfadfasdfasdf()
5+
.foo()
6+
.bar()
7+
.baz();
8+
9+
bazffffffffffffffffffffffffffffffffffffasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfadfasdfasdfasdfadfasdfasdf()
10+
.foo()
11+
.foo()
12+
.bar()
13+
.baz();
14+
}
15+
16+
fn long_first_child() {
17+
let foo = bar().asdffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff()
18+
.foo()
19+
.bar()
20+
.baz();
21+
22+
qux().asdffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff()
23+
.foo()
24+
.bar()
25+
.baz();
626
}
727

828
fn long_tail() {
@@ -11,9 +31,7 @@ fn long_tail() {
1131
.map(|x| x / 2)
1232
.fold(0, |acc, x| acc + x)
1333
.doooooooooooooooooooooooooooooooooooooooooooooooooooooo_stufffffffffffffffffffffffffffffffffffffff("abcdefghadfasdfasdfasdfasdfadf");
14-
}
1534

16-
fn assignment_long_tail() {
1735
let foo = bar().xxxxxxx
1836
.map(|x| x + 5)
1937
.map(|x| x / 2)

0 commit comments

Comments
 (0)