Skip to content

Commit 4f226eb

Browse files
authored
Merge pull request #1424 from topecongiro/long-struct
Split long fields in structs
2 parents 6c206d2 + 3a1ffa7 commit 4f226eb

File tree

3 files changed

+74
-18
lines changed

3 files changed

+74
-18
lines changed

src/items.rs

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,25 +1220,60 @@ impl Rewrite for ast::StructField {
12201220
}
12211221

12221222
let type_annotation_spacing = type_annotation_spacing(context.config);
1223-
let result = match name {
1224-
Some(name) => {
1225-
format!("{}{}{}{}:{}",
1226-
attr_str,
1227-
vis,
1228-
name,
1229-
type_annotation_spacing.0,
1230-
type_annotation_spacing.1)
1231-
}
1223+
let mut result = match name {
1224+
Some(name) => format!("{}{}{}{}:", attr_str, vis, name, type_annotation_spacing.0),
12321225
None => format!("{}{}", attr_str, vis),
12331226
};
12341227

1235-
let last_line_width = last_line_width(&result);
1228+
let type_offset = shape.indent.block_indent(context.config);
1229+
let rewrite_type_in_next_line = || {
1230+
let budget = try_opt!(context
1231+
.config
1232+
.max_width
1233+
.checked_sub(type_offset.width()));
1234+
self.ty
1235+
.rewrite(context, Shape::legacy(budget, type_offset))
1236+
};
1237+
1238+
let last_line_width = last_line_width(&result) + type_annotation_spacing.1.len();
12361239
let budget = try_opt!(shape.width.checked_sub(last_line_width));
1237-
let rewrite = try_opt!(self.ty
1238-
.rewrite(context,
1239-
Shape::legacy(budget,
1240-
shape.indent + last_line_width)));
1241-
Some(result + &rewrite)
1240+
let ty_rewritten = self.ty
1241+
.rewrite(context,
1242+
Shape::legacy(budget, shape.indent + last_line_width));
1243+
match ty_rewritten {
1244+
Some(ref ty) if ty.contains('\n') => {
1245+
let new_ty = rewrite_type_in_next_line();
1246+
match new_ty {
1247+
Some(ref new_ty) if !new_ty.contains('\n') &&
1248+
new_ty.len() + type_offset.width() <=
1249+
context.config.max_width => {
1250+
Some(format!("{}\n{}{}",
1251+
result,
1252+
type_offset.to_string(&context.config),
1253+
&new_ty))
1254+
}
1255+
_ => {
1256+
if name.is_some() {
1257+
result.push_str(type_annotation_spacing.1);
1258+
}
1259+
Some(result + &ty)
1260+
}
1261+
}
1262+
}
1263+
Some(ty) => {
1264+
if name.is_some() {
1265+
result.push_str(type_annotation_spacing.1);
1266+
}
1267+
Some(result + &ty)
1268+
}
1269+
None => {
1270+
let ty = try_opt!(rewrite_type_in_next_line());
1271+
Some(format!("{}\n{}{}",
1272+
result,
1273+
type_offset.to_string(&context.config),
1274+
&ty))
1275+
}
1276+
}
12421277
}
12431278
}
12441279

tests/source/structs.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,14 @@ struct Foo {
167167
}
168168
struct Foo { /* comment */ }
169169
struct Foo();
170+
171+
struct LongStruct {
172+
a: A,
173+
the_quick_brown_fox_jumps_over_the_lazy_dog:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA,
174+
}
175+
176+
struct Deep {
177+
deeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeep: node::Handle<IdRef<'id, Node<Key, Value>>,
178+
Type,
179+
NodeType>,
180+
}

tests/target/structs.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,8 @@ struct FieldsWithAttributes {
117117
}
118118

119119
struct Deep {
120-
deeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeep: node::Handle<IdRef<'id, Node<K, V>>,
121-
Type,
122-
NodeType>,
120+
deeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeep:
121+
node::Handle<IdRef<'id, Node<K, V>>, Type, NodeType>,
123122
}
124123

125124
struct Foo<T>(T);
@@ -172,3 +171,14 @@ struct Foo {
172171
}
173172
struct Foo { /* comment */ }
174173
struct Foo();
174+
175+
struct LongStruct {
176+
a: A,
177+
the_quick_brown_fox_jumps_over_the_lazy_dog:
178+
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA,
179+
}
180+
181+
struct Deep {
182+
deeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeep:
183+
node::Handle<IdRef<'id, Node<Key, Value>>, Type, NodeType>,
184+
}

0 commit comments

Comments
 (0)