Skip to content

Commit 617bff7

Browse files
committed
Add fn_call_layout configuration option
Closes 5218 The `fn_call_layout` was added to give users more control over how function calls are formatted. The values are identical to those for `fn_args_layout`, which is a stable option that controls how function declarations are laid out.
1 parent c03e184 commit 617bff7

File tree

14 files changed

+217
-1
lines changed

14 files changed

+217
-1
lines changed

Configurations.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,75 @@ trait Lorem {
753753
}
754754
```
755755

756+
## `fn_call_layout`
757+
758+
Control the layout of arguments in function calls
759+
760+
- **Default value**: `"Tall"`
761+
- **Possible values**: `"Compressed"`, `"Tall"`, `"Vertical"`
762+
- **Stable**: No (tracking issue: N/A)
763+
764+
#### `"Tall"` (default):
765+
766+
```rust
767+
fn main() {
768+
lorem(ipsum, dolor, sit, amet);
769+
ipsum(
770+
dolor,
771+
sit,
772+
amet,
773+
consectetur,
774+
adipiscing,
775+
elit,
776+
vivamus,
777+
ipsum,
778+
orci,
779+
rhoncus,
780+
vel,
781+
imperdiet,
782+
);
783+
}
784+
```
785+
786+
#### `"Compressed"`:
787+
788+
```rust
789+
fn main() {
790+
lorem(ipsum, dolor, sit, amet);
791+
ipsum(
792+
dolor, sit, amet, consectetur, adipiscing, elit, vivamus, ipsum, orci, rhoncus, vel,
793+
imperdiet,
794+
);
795+
}
796+
```
797+
798+
#### `"Vertical"`:
799+
800+
```rust
801+
fn main() {
802+
lorem(
803+
ipsum,
804+
dolor,
805+
sit,
806+
amet,
807+
);
808+
ipsum(
809+
dolor,
810+
sit,
811+
amet,
812+
consectetur,
813+
adipiscing,
814+
elit,
815+
vivamus,
816+
ipsum,
817+
orci,
818+
rhoncus,
819+
vel,
820+
imperdiet,
821+
);
822+
}
823+
```
824+
756825
## `fn_call_width`
757826

758827
Maximum width of the args of a function call before falling back to vertical formatting.

src/attr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ impl Rewrite for ast::MetaItem {
309309
} else {
310310
SeparatorTactic::Never
311311
}),
312+
None,
312313
)?
313314
}
314315
ast::MetaItemKind::NameValue(ref literal) => {

src/config/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ create_config! {
122122
"Force multiline closure bodies and match arms to be wrapped in a block";
123123
fn_args_layout: Density, Density::Tall, true,
124124
"Control the layout of arguments in a function";
125+
fn_call_layout: Density, Density::Tall, false,
126+
"Control the layout of arguments in a function call";
125127
brace_style: BraceStyle, BraceStyle::SameLineWhere, false, "Brace style for items";
126128
control_brace_style: ControlBraceStyle, ControlBraceStyle::AlwaysSameLine, false,
127129
"Brace style for control flow constructs";
@@ -601,6 +603,7 @@ match_arm_blocks = true
601603
match_arm_leading_pipes = "Never"
602604
force_multiline_blocks = false
603605
fn_args_layout = "Tall"
606+
fn_call_layout = "Tall"
604607
brace_style = "SameLineWhere"
605608
control_brace_style = "AlwaysSameLine"
606609
trailing_semicolon = true

src/expr.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,6 +1312,12 @@ pub(crate) fn rewrite_call(
13121312
span: Span,
13131313
shape: Shape,
13141314
) -> Option<String> {
1315+
let force_list_tactic = if context.config.was_set().fn_call_layout() {
1316+
Some(context.config.fn_call_layout().to_list_tactic(args.len()))
1317+
} else {
1318+
None
1319+
};
1320+
13151321
overflow::rewrite_with_parens(
13161322
context,
13171323
callee,
@@ -1320,6 +1326,7 @@ pub(crate) fn rewrite_call(
13201326
span,
13211327
context.config.fn_call_width(),
13221328
choose_separator_tactic(context, span),
1329+
force_list_tactic,
13231330
)
13241331
}
13251332

@@ -1857,6 +1864,7 @@ pub(crate) fn rewrite_tuple<'a, T: 'a + IntoOverflowableItem<'a>>(
18571864
span,
18581865
context.config.fn_call_width(),
18591866
force_tactic,
1867+
None,
18601868
)
18611869
} else {
18621870
rewrite_tuple_in_visual_indent_style(context, items, span, shape, is_singleton_tuple)

src/items.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1485,6 +1485,7 @@ fn format_tuple_struct(
14851485
mk_sp(lo, span.hi()),
14861486
context.config.fn_call_width(),
14871487
None,
1488+
None,
14881489
)?;
14891490
}
14901491

src/macros.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ fn rewrite_macro_inner(
279279
} else {
280280
Some(SeparatorTactic::Never)
281281
},
282+
None,
282283
)
283284
.map(|rw| match position {
284285
MacroPosition::Item => format!("{};", rw),

src/overflow.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ pub(crate) fn rewrite_with_parens<'a, T: 'a + IntoOverflowableItem<'a>>(
252252
span: Span,
253253
item_max_width: usize,
254254
force_separator_tactic: Option<SeparatorTactic>,
255+
force_list_tactic: Option<ListTactic>,
255256
) -> Option<String> {
256257
Context::new(
257258
context,
@@ -263,6 +264,7 @@ pub(crate) fn rewrite_with_parens<'a, T: 'a + IntoOverflowableItem<'a>>(
263264
")",
264265
item_max_width,
265266
force_separator_tactic,
267+
force_list_tactic,
266268
None,
267269
)
268270
.rewrite(shape)
@@ -286,6 +288,7 @@ pub(crate) fn rewrite_with_angle_brackets<'a, T: 'a + IntoOverflowableItem<'a>>(
286288
context.config.max_width(),
287289
None,
288290
None,
291+
None,
289292
)
290293
.rewrite(shape)
291294
}
@@ -314,6 +317,7 @@ pub(crate) fn rewrite_with_square_brackets<'a, T: 'a + IntoOverflowableItem<'a>>
314317
rhs,
315318
context.config.array_width(),
316319
force_separator_tactic,
320+
None,
317321
Some(("[", "]")),
318322
)
319323
.rewrite(shape)
@@ -331,6 +335,7 @@ struct Context<'a> {
331335
item_max_width: usize,
332336
one_line_width: usize,
333337
force_separator_tactic: Option<SeparatorTactic>,
338+
force_list_tactic: Option<ListTactic>,
334339
custom_delims: Option<(&'a str, &'a str)>,
335340
}
336341

@@ -345,6 +350,7 @@ impl<'a> Context<'a> {
345350
suffix: &'static str,
346351
item_max_width: usize,
347352
force_separator_tactic: Option<SeparatorTactic>,
353+
force_list_tactic: Option<ListTactic>,
348354
custom_delims: Option<(&'a str, &'a str)>,
349355
) -> Context<'a> {
350356
let used_width = extra_offset(ident, shape);
@@ -369,6 +375,7 @@ impl<'a> Context<'a> {
369375
item_max_width,
370376
one_line_width,
371377
force_separator_tactic,
378+
force_list_tactic,
372379
custom_delims,
373380
}
374381
}
@@ -493,6 +500,8 @@ impl<'a> Context<'a> {
493500
None
494501
};
495502

503+
let simple_expr = is_every_expr_simple(&self.items);
504+
496505
let mut tactic = definitive_tactic(
497506
&*list_items,
498507
ListTactic::LimitedHorizontalVertical(self.item_max_width),
@@ -570,7 +579,7 @@ impl<'a> Context<'a> {
570579
if one_line {
571580
tactic = DefinitiveListTactic::SpecialMacro(num_args_before);
572581
};
573-
} else if is_every_expr_simple(&self.items)
582+
} else if simple_expr
574583
&& no_long_items(
575584
list_items,
576585
self.context.config.short_array_element_width_threshold(),
@@ -584,6 +593,23 @@ impl<'a> Context<'a> {
584593
_ => (),
585594
}
586595

596+
match self.force_list_tactic {
597+
Some(list_tactic) if list_tactic == ListTactic::Mixed && simple_expr => {
598+
if tactic != DefinitiveListTactic::Horizontal {
599+
tactic = DefinitiveListTactic::Mixed
600+
}
601+
}
602+
Some(list_tactic) if list_tactic != ListTactic::Mixed => {
603+
tactic = definitive_tactic(
604+
&*list_items,
605+
list_tactic,
606+
Separator::Comma,
607+
self.one_line_width,
608+
)
609+
}
610+
_ => {}
611+
};
612+
587613
tactic
588614
}
589615

src/patterns.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,7 @@ fn rewrite_tuple_pat(
494494
} else {
495495
None
496496
},
497+
None,
497498
)
498499
}
499500

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// rustfmt-fn_call_layout:Compressed
2+
3+
fn main() {
4+
lorem(ipsum, dolor, sit, amet);
5+
lorem(ipsum, // some inine comment
6+
dolor, sit, amet);
7+
lorem(ipsum, /* some inine comment */
8+
dolor, sit, amet);
9+
ipsum(dolor, sit, amet, consectetur, adipiscing, elit, vivamus, ipsum, orci, rhoncus, vel, imperdiet);
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// rustfmt-fn_call_layout:Tall
2+
3+
fn main() {
4+
lorem(ipsum, dolor, sit, amet);
5+
lorem(ipsum, // some inine comment
6+
dolor, sit, amet);
7+
lorem(ipsum, /* some inine comment */
8+
dolor, sit, amet);
9+
ipsum(dolor, sit, amet, consectetur, adipiscing, elit, vivamus, ipsum, orci, rhoncus, vel, imperdiet);
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// rustfmt-fn_call_layout:Vertical
2+
3+
fn main() {
4+
lorem(ipsum, dolor, sit, amet);
5+
lorem(ipsum, // some inine comment
6+
dolor, sit, amet);
7+
lorem(ipsum, /* some inine comment */
8+
dolor, sit, amet);
9+
ipsum(dolor, sit, amet, consectetur, adipiscing, elit, vivamus, ipsum, orci, rhoncus, vel, imperdiet);
10+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// rustfmt-fn_call_layout:Compressed
2+
3+
fn main() {
4+
lorem(ipsum, dolor, sit, amet);
5+
lorem(
6+
ipsum, // some inine comment
7+
dolor, sit, amet,
8+
);
9+
lorem(ipsum /* some inine comment */, dolor, sit, amet);
10+
ipsum(
11+
dolor, sit, amet, consectetur, adipiscing, elit, vivamus, ipsum, orci, rhoncus, vel,
12+
imperdiet,
13+
);
14+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// rustfmt-fn_call_layout:Tall
2+
3+
fn main() {
4+
lorem(ipsum, dolor, sit, amet);
5+
lorem(
6+
ipsum, // some inine comment
7+
dolor,
8+
sit,
9+
amet,
10+
);
11+
lorem(ipsum /* some inine comment */, dolor, sit, amet);
12+
ipsum(
13+
dolor,
14+
sit,
15+
amet,
16+
consectetur,
17+
adipiscing,
18+
elit,
19+
vivamus,
20+
ipsum,
21+
orci,
22+
rhoncus,
23+
vel,
24+
imperdiet,
25+
);
26+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// rustfmt-fn_call_layout:Vertical
2+
3+
fn main() {
4+
lorem(
5+
ipsum,
6+
dolor,
7+
sit,
8+
amet,
9+
);
10+
lorem(
11+
ipsum, // some inine comment
12+
dolor,
13+
sit,
14+
amet,
15+
);
16+
lorem(
17+
ipsum, /* some inine comment */
18+
dolor,
19+
sit,
20+
amet,
21+
);
22+
ipsum(
23+
dolor,
24+
sit,
25+
amet,
26+
consectetur,
27+
adipiscing,
28+
elit,
29+
vivamus,
30+
ipsum,
31+
orci,
32+
rhoncus,
33+
vel,
34+
imperdiet,
35+
);
36+
}

0 commit comments

Comments
 (0)