Skip to content

Commit 3a295ee

Browse files
committed
Adjust rhs shape width to account for next line indentation
Fixes 5321 Previously the `Shape` returned from `expr::shape_from_rhs_tactic` would always return a `Shape` with a `width` == `config.max_width` when the `rhs_tactic` was `RhsTactics::ForceNextLineWithoutIndent` and the indentation was 0. For example, when rewriting generic trait bounds at the top level of a file. In this case, trait bounds that were between (`max_width` - `tab_spaces`) and `max_width`, would be considered properly formatted and wouldn't wrap. Now, the shape takes into account the whitespace to properly wrap when the `max_width` is exceeded.
1 parent a37d3ab commit 3a295ee

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

src/expr.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2111,6 +2111,9 @@ fn shape_from_rhs_tactic(
21112111
rhs_tactic: RhsTactics,
21122112
) -> Option<Shape> {
21132113
match rhs_tactic {
2114+
RhsTactics::ForceNextLineWithoutIndent if shape.indent.width() == 0 => shape
2115+
.with_max_width(context.config)
2116+
.sub_width(context.config.tab_spaces()),
21142117
RhsTactics::ForceNextLineWithoutIndent => shape
21152118
.with_max_width(context.config)
21162119
.sub_width(shape.indent.width()),

tests/source/issue_5321.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
trait AllTheOthers1: Trait3 + Trait4 + Trait5 + Trait6 + Trait7 + Trait8 + Trait9 + Trait10 + Trait11 + Trait12 + Trait14
2+
{
3+
}
4+
5+
trait AllTheOthers2: Trait4 + Trait5 + Trait6 + Trait7 + Trait8 + Trait9 + Trait10 + Trait11 + Trait12 + Trait13 + Trait15
6+
{
7+
}
8+
9+
mod Module {
10+
trait AllTheOthers3: Trait5 + Trait6 + Trait7 + Trait8 + Trait9 + Trait10 + Trait11 + Trait12 + Trait13 + Trait14
11+
{
12+
}
13+
14+
trait AllTheOthers4: Trait6 + Trait7 + Trait8 + Trait9 + Trait10 + Trait11 + Trait12 + Trait13 + Trait14 + Trait15
15+
{
16+
}
17+
}

tests/target/issue_5321.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
trait AllTheOthers1:
2+
Trait3
3+
+ Trait4
4+
+ Trait5
5+
+ Trait6
6+
+ Trait7
7+
+ Trait8
8+
+ Trait9
9+
+ Trait10
10+
+ Trait11
11+
+ Trait12
12+
+ Trait14
13+
{
14+
}
15+
16+
trait AllTheOthers2:
17+
Trait4
18+
+ Trait5
19+
+ Trait6
20+
+ Trait7
21+
+ Trait8
22+
+ Trait9
23+
+ Trait10
24+
+ Trait11
25+
+ Trait12
26+
+ Trait13
27+
+ Trait15
28+
{
29+
}
30+
31+
mod Module {
32+
trait AllTheOthers3:
33+
Trait5 + Trait6 + Trait7 + Trait8 + Trait9 + Trait10 + Trait11 + Trait12 + Trait13 + Trait14
34+
{
35+
}
36+
37+
trait AllTheOthers4:
38+
Trait6
39+
+ Trait7
40+
+ Trait8
41+
+ Trait9
42+
+ Trait10
43+
+ Trait11
44+
+ Trait12
45+
+ Trait13
46+
+ Trait14
47+
+ Trait15
48+
{
49+
}
50+
}

0 commit comments

Comments
 (0)