Skip to content

Commit 000ea50

Browse files
committed
Merge pull request #243 from nrc/fn-decl-2
More function decl variety
2 parents 6ca2756 + fae93ab commit 000ea50

File tree

11 files changed

+144
-22
lines changed

11 files changed

+144
-22
lines changed

src/config.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ create_config! {
8989
fn_brace_style: BraceStyle,
9090
fn_return_indent: ReturnIndent,
9191
fn_args_paren_newline: bool,
92-
fn_args_layout: Density,
92+
fn_args_density: Density,
93+
fn_args_layout: StructLitStyle,
9394
fn_arg_indent: BlockIndentStyle,
9495
where_density: Density, // Should we at least try to put the where clause on the same line as
9596
// the rest of the function decl?
@@ -122,7 +123,8 @@ impl Default for Config {
122123
fn_brace_style: BraceStyle::SameLineWhere,
123124
fn_return_indent: ReturnIndent::WithArgs,
124125
fn_args_paren_newline: true,
125-
fn_args_layout: Density::Tall,
126+
fn_args_density: Density::Tall,
127+
fn_args_layout: StructLitStyle::Visual,
126128
fn_arg_indent: BlockIndentStyle::Visual,
127129
where_density: Density::Tall,
128130
where_indent: BlockIndentStyle::Tabbed,
@@ -131,7 +133,7 @@ impl Default for Config {
131133
generics_indent: BlockIndentStyle::Visual,
132134
struct_trailing_comma: SeparatorTactic::Vertical,
133135
struct_lit_trailing_comma: SeparatorTactic::Vertical,
134-
struct_lit_style: StructLitStyle::BlockIndent,
136+
struct_lit_style: StructLitStyle::Block,
135137
enum_trailing_comma: true,
136138
report_todo: ReportTactic::Always,
137139
report_fixme: ReportTactic::Never,

src/expr.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -940,10 +940,10 @@ fn rewrite_struct_lit<'a>(context: &RewriteContext,
940940
// Foo { a: Foo } - indent is +3, width is -5.
941941
let h_budget = try_opt!(width.checked_sub(path_str.len() + 5));
942942
let (indent, v_budget) = match context.config.struct_lit_style {
943-
StructLitStyle::VisualIndent => {
943+
StructLitStyle::Visual => {
944944
(offset + path_str.len() + 3, h_budget)
945945
}
946-
StructLitStyle::BlockIndent => {
946+
StructLitStyle::Block => {
947947
// If we are all on one line, then we'll ignore the indent, and we
948948
// have a smaller budget.
949949
let indent = context.block_indent + context.config.tab_spaces;
@@ -1012,15 +1012,15 @@ fn rewrite_struct_lit<'a>(context: &RewriteContext,
10121012
let fields_str = write_list(&items.collect::<Vec<_>>(), &fmt);
10131013

10141014
match context.config.struct_lit_style {
1015-
StructLitStyle::BlockIndent if fields_str.contains('\n') => {
1015+
StructLitStyle::Block if fields_str.contains('\n') => {
10161016
let inner_indent = make_indent(context.block_indent + context.config.tab_spaces);
10171017
let outer_indent = make_indent(context.block_indent);
10181018
Some(format!("{} {{\n{}{}\n{}}}", path_str, inner_indent, fields_str, outer_indent))
10191019
}
10201020
_ => Some(format!("{} {{ {} }}", path_str, fields_str)),
10211021
}
10221022

1023-
// FIXME if context.config.struct_lit_style == VisualIndent, but we run out
1023+
// FIXME if context.config.struct_lit_style == Visual, but we run out
10241024
// of space, we should fall back to BlockIndent.
10251025
}
10261026

src/items.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
// Formatting top-level items - functions, structs, enums, traits, impls.
1212

13-
use {ReturnIndent, BraceStyle};
13+
use {ReturnIndent, BraceStyle, StructLitStyle};
1414
use utils::{format_mutability, format_visibility, make_indent, contains_skip, span_after,
1515
end_typaram};
1616
use lists::{write_list, itemize_list, ListItem, ListFormatting, SeparatorTactic, ListTactic};
@@ -225,6 +225,10 @@ impl<'a> FmtVisitor<'a> {
225225
result.push_str("(\n");
226226
result.push_str(&make_indent(arg_indent));
227227
}
228+
} else if self.config.fn_args_layout == StructLitStyle::Block {
229+
arg_indent = indent + self.config.tab_spaces;
230+
result.push_str("(\n");
231+
result.push_str(&make_indent(arg_indent));
228232
} else {
229233
result.push('(');
230234
}
@@ -245,14 +249,20 @@ impl<'a> FmtVisitor<'a> {
245249
indent,
246250
arg_indent,
247251
args_span));
252+
if self.config.fn_args_layout == StructLitStyle::Block {
253+
result.push('\n');
254+
}
248255
result.push(')');
249256

250257
// Return type.
251258
if !ret_str.is_empty() {
252259
// If we've already gone multi-line, or the return type would push
253260
// over the max width, then put the return type on a new line.
254-
if result.contains("\n") ||
255-
result.len() + indent + ret_str.len() > self.config.max_width {
261+
// Unless we are formatting args like a block, in which case there
262+
// should always be room for the return type.
263+
if (result.contains("\n") ||
264+
result.len() + indent + ret_str.len() > self.config.max_width) &&
265+
self.config.fn_args_layout != StructLitStyle::Block {
256266
let indent = match self.config.fn_return_indent {
257267
ReturnIndent::WithWhereClause => indent + 4,
258268
// TODO we might want to check that using the arg indent doesn't
@@ -285,8 +295,11 @@ impl<'a> FmtVisitor<'a> {
285295
}
286296
}
287297

288-
let where_density = if self.config.where_density == Density::Compressed &&
289-
!result.contains('\n') {
298+
let where_density = if (self.config.where_density == Density::Compressed &&
299+
(!result.contains('\n') ||
300+
self.config.fn_args_layout == StructLitStyle::Block)) ||
301+
(self.config.fn_args_layout == StructLitStyle::Block &&
302+
ret_str.is_empty()) {
290303
Density::Compressed
291304
} else {
292305
Density::Tall
@@ -363,7 +376,7 @@ impl<'a> FmtVisitor<'a> {
363376
};
364377

365378
let fmt = ListFormatting {
366-
tactic: self.config.fn_args_layout.to_list_tactic(),
379+
tactic: self.config.fn_args_density.to_list_tactic(),
367380
separator: ",",
368381
trailing_separator: SeparatorTactic::Never,
369382
indent: indent,

src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,13 @@ impl_enum_decodable!(ReturnIndent, WithArgs, WithWhereClause);
133133
pub enum StructLitStyle {
134134
// First line on the same line as the opening brace, all lines aligned with
135135
// the first line.
136-
VisualIndent,
136+
Visual,
137137
// First line is on a new line and all lines align with block indent.
138-
BlockIndent,
138+
Block,
139139
// FIXME Maybe we should also have an option to align types.
140140
}
141141

142-
impl_enum_decodable!(StructLitStyle, VisualIndent, BlockIndent);
142+
impl_enum_decodable!(StructLitStyle, Visual, Block);
143143

144144
enum ErrorKind {
145145
// Line has exceeded character limit

tests/config/small_tabs.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ newline_style = "Unix"
66
fn_brace_style = "SameLineWhere"
77
fn_return_indent = "WithArgs"
88
fn_args_paren_newline = true
9-
fn_args_layout = "Tall"
9+
fn_args_density = "Tall"
10+
fn_args_layout = "Visual"
1011
fn_arg_indent = "Visual"
1112
where_density = "Tall"
1213
where_indent = "Tabbed"
@@ -15,7 +16,7 @@ where_pred_indent = "Visual"
1516
generics_indent = "Visual"
1617
struct_trailing_comma = "Vertical"
1718
struct_lit_trailing_comma = "Vertical"
18-
struct_lit_style = "BlockIndent"
19+
struct_lit_style = "Block"
1920
enum_trailing_comma = true
2021
report_todo = "Always"
2122
report_fixme = "Never"

tests/source/fn-custom-6.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// rustfmt-fn_args_layout: Block
2+
// rustfmt-where_indent: Inherit
3+
// rustfmt-fn_brace_style: PreferSameLine
4+
// Test different indents.
5+
6+
fn foo(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb) {
7+
foo();
8+
}
9+
10+
fn bar(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb, c: Cccccccccccccccccc, d: Dddddddddddddddd, e: Eeeeeeeeeeeeeee) {
11+
bar();
12+
}
13+
14+
fn foo(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb) -> String {
15+
foo();
16+
}
17+
18+
fn bar(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb, c: Cccccccccccccccccc, d: Dddddddddddddddd, e: Eeeeeeeeeeeeeee) -> String {
19+
bar();
20+
}
21+
22+
fn foo(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb) where T: UUUUUUUUUUU {
23+
foo();
24+
}
25+
26+
fn bar(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb, c: Cccccccccccccccccc, d: Dddddddddddddddd, e: Eeeeeeeeeeeeeee) where T: UUUUUUUUUUU {
27+
bar();
28+
}
29+
30+
fn foo(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb) -> String where T: UUUUUUUUUUU {
31+
foo();
32+
}
33+
34+
fn bar(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb, c: Cccccccccccccccccc, d: Dddddddddddddddd, e: Eeeeeeeeeeeeeee) -> String where T: UUUUUUUUUUU {
35+
bar();
36+
}

tests/source/fn-custom.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// rustfmt-fn_args_layout: Compressed
1+
// rustfmt-fn_args_density: Compressed
22
// Test some of the ways function signatures can be customised.
33

44
// Test compressed layout of args.

tests/source/struct_lits_visual.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// rustfmt-struct_lit_style: VisualIndent
1+
// rustfmt-struct_lit_style: Visual
22

33
// Struct literal expressions.
44

tests/target/fn-custom-6.rs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// rustfmt-fn_args_layout: Block
2+
// rustfmt-where_indent: Inherit
3+
// rustfmt-fn_brace_style: PreferSameLine
4+
// Test different indents.
5+
6+
fn foo(
7+
a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb
8+
) {
9+
foo();
10+
}
11+
12+
fn bar(
13+
a: Aaaaaaaaaaaaaa,
14+
b: Bbbbbbbbbbbbbb,
15+
c: Cccccccccccccccccc,
16+
d: Dddddddddddddddd,
17+
e: Eeeeeeeeeeeeeee
18+
) {
19+
bar();
20+
}
21+
22+
fn foo(
23+
a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb
24+
) -> String {
25+
foo();
26+
}
27+
28+
fn bar(
29+
a: Aaaaaaaaaaaaaa,
30+
b: Bbbbbbbbbbbbbb,
31+
c: Cccccccccccccccccc,
32+
d: Dddddddddddddddd,
33+
e: Eeeeeeeeeeeeeee
34+
) -> String {
35+
bar();
36+
}
37+
38+
fn foo(
39+
a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb
40+
) where T: UUUUUUUUUUU {
41+
foo();
42+
}
43+
44+
fn bar(
45+
a: Aaaaaaaaaaaaaa,
46+
b: Bbbbbbbbbbbbbb,
47+
c: Cccccccccccccccccc,
48+
d: Dddddddddddddddd,
49+
e: Eeeeeeeeeeeeeee
50+
) where T: UUUUUUUUUUU {
51+
bar();
52+
}
53+
54+
fn foo(
55+
a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb
56+
) -> String
57+
where T: UUUUUUUUUUU {
58+
foo();
59+
}
60+
61+
fn bar(
62+
a: Aaaaaaaaaaaaaa,
63+
b: Bbbbbbbbbbbbbb,
64+
c: Cccccccccccccccccc,
65+
d: Dddddddddddddddd,
66+
e: Eeeeeeeeeeeeeee
67+
) -> String
68+
where T: UUUUUUUUUUU {
69+
bar();
70+
}

tests/target/fn-custom.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// rustfmt-fn_args_layout: Compressed
1+
// rustfmt-fn_args_density: Compressed
22
// Test some of the ways function signatures can be customised.
33

44
// Test compressed layout of args.

tests/target/struct_lits_visual.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// rustfmt-struct_lit_style: VisualIndent
1+
// rustfmt-struct_lit_style: Visual
22

33
// Struct literal expressions.
44

0 commit comments

Comments
 (0)