Skip to content

Commit a32b0e7

Browse files
committed
Fix some bugs
1 parent 018fa85 commit a32b0e7

File tree

3 files changed

+30
-32
lines changed

3 files changed

+30
-32
lines changed

src/expr.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -192,14 +192,17 @@ fn rewrite_struct_lit<'a>(context: &RewriteContext,
192192
}
193193

194194
let path_str = pprust::path_to_string(path);
195-
// Foo { a: Foo } - indent is +3, width is -5.
196-
let (indent, budget) = match context.config.struct_lit_style {
195+
let (indent, h_budget, v_budget) = match context.config.struct_lit_style {
197196
StructLitStyle::VisualIndent => {
198-
(offset + path_str.len() + 3, width - (path_str.len() + 5))
197+
// Foo { a: Foo } - indent is +3, width is -5.
198+
let budget = width - (path_str.len() + 5);
199+
(offset + path_str.len() + 3, budget, budget)
199200
}
200201
StructLitStyle::BlockIndent => {
202+
// If we are all on one line, then we'll ignore the indent, and we
203+
// have a smaller budget.
201204
let indent = context.block_indent + context.config.tab_spaces;
202-
(indent, width - indent)
205+
(indent, width - (path_str.len() + 5), width - indent)
203206
}
204207
};
205208

@@ -227,13 +230,13 @@ fn rewrite_struct_lit<'a>(context: &RewriteContext,
227230
|item| {
228231
match *item {
229232
StructLitField::Regular(ref field) => {
230-
rewrite_field(context, &field, budget, indent)
233+
rewrite_field(context, &field, h_budget, indent)
231234
.unwrap_or(context.codemap.span_to_snippet(field.span)
232235
.unwrap())
233236
},
234237
StructLitField::Base(ref expr) => {
235238
// 2 = ..
236-
expr.rewrite(context, budget - 2, indent + 2)
239+
expr.rewrite(context, h_budget - 2, indent + 2)
237240
.map(|s| format!("..{}", s))
238241
.unwrap_or(context.codemap.span_to_snippet(expr.span)
239242
.unwrap())
@@ -252,8 +255,8 @@ fn rewrite_struct_lit<'a>(context: &RewriteContext,
252255
context.config.struct_lit_trailing_comma
253256
},
254257
indent: indent,
255-
h_width: budget,
256-
v_width: budget,
258+
h_width: h_budget,
259+
v_width: v_budget,
257260
ends_with_newline: true,
258261
};
259262
let fields_str = write_list(&items, &fmt);

tests/source/struct_lits.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ fn main() {
88

99
Foo { a: foo() /* comment*/, /* comment*/ b: bar(), ..something };
1010

11-
Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo { a: foo(), b: bar(), };
11+
Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo { a: foo(), b: bar(), };
12+
13+
Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo { a: foo(), b: bar(), };
1214

1315
Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo {
1416
// Comment

tests/target/struct_lits.rs

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,45 +13,38 @@ fn main() {
1313
..something
1414
};
1515

16-
Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo {
16+
Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo { a: foo(), b: bar() };
17+
18+
Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo {
1719
a: foo(),
1820
b: bar(),
1921
};
2022

21-
Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo { // Comment
22-
a: foo(), /* C
23-
* o
24-
* m
25-
* m
26-
* e
27-
* n
28-
* t */
23+
Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo {
24+
// Comment
25+
a: foo(), // Comment
2926
// Comment
30-
b: bar(), /* C
31-
* o
32-
* m
33-
* m
34-
* e
35-
* n
36-
* t */
27+
b: bar(), /* Comment */
3728
};
3829

3930
Foo { a: Bar, b: foo() };
4031

41-
A { // Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus. Sed sit
42-
// amet ipsum mauris. Maecenas congue ligula ac quam viverra nec consectetur ante
32+
A {
33+
// Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus. Sed
34+
// sit amet ipsum mauris. Maecenas congue ligula ac quam viverra nec consectetur ante
4335
// hendrerit. Donec et mollis dolor.
4436
first: item(),
4537
// Praesent et diam eget libero egestas mattis sit amet vitae augue.
4638
// Nam tincidunt congue enim, ut porta lorem lacinia consectetur.
4739
second: Item,
4840
};
4941

50-
Diagram { // o This graph demonstrates how
51-
// / \ significant whitespace is
52-
// o o preserved.
53-
// /|\ \
54-
// o o o o
42+
Diagram {
43+
// o This graph demonstrates how
44+
// / \ significant whitespace is
45+
// o o preserved.
46+
// /|\ \
47+
// o o o o
5548
graph: G,
5649
}
5750
}

0 commit comments

Comments
 (0)