Skip to content

Commit 8f6b6c2

Browse files
authored
Merge pull request #2210 from topecongiro/issue-2178
Combine a short callee and a single argument
2 parents bd876b9 + 39d85b0 commit 8f6b6c2

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

src/expr.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1850,6 +1850,9 @@ where
18501850
};
18511851
let used_width = extra_offset(callee_str, shape);
18521852
let one_line_width = shape.width.checked_sub(used_width + 2 * paren_overhead)?;
1853+
// 1 = "("
1854+
let combine_arg_with_callee =
1855+
callee_str.len() + 1 <= context.config.tab_spaces() && args.len() == 1;
18531856

18541857
// 1 = "(" or ")"
18551858
let one_line_shape = shape
@@ -1874,6 +1877,7 @@ where
18741877
one_line_width,
18751878
args_max_width,
18761879
force_trailing_comma,
1880+
combine_arg_with_callee,
18771881
)?;
18781882

18791883
if !context.use_block_indent() && need_block_indent(&list_str, nested_shape) && !extendable {
@@ -1914,6 +1918,7 @@ fn rewrite_call_args<'a, T>(
19141918
one_line_width: usize,
19151919
args_max_width: usize,
19161920
force_trailing_comma: bool,
1921+
combine_arg_with_callee: bool,
19171922
) -> Option<(bool, String)>
19181923
where
19191924
T: Rewrite + Spanned + ToExpr + 'a,
@@ -1943,6 +1948,7 @@ where
19431948
nested_shape,
19441949
one_line_width,
19451950
args_max_width,
1951+
combine_arg_with_callee,
19461952
);
19471953

19481954
let fmt = ListFormatting {
@@ -1973,19 +1979,22 @@ fn try_overflow_last_arg<'a, T>(
19731979
nested_shape: Shape,
19741980
one_line_width: usize,
19751981
args_max_width: usize,
1982+
combine_arg_with_callee: bool,
19761983
) -> DefinitiveListTactic
19771984
where
19781985
T: Rewrite + Spanned + ToExpr + 'a,
19791986
{
1980-
let overflow_last = can_be_overflowed(context, args);
1987+
let overflow_last = combine_arg_with_callee || can_be_overflowed(context, args);
19811988

19821989
// Replace the last item with its first line to see if it fits with
19831990
// first arguments.
19841991
let placeholder = if overflow_last {
19851992
let mut context = context.clone();
1986-
if let Some(expr) = args[args.len() - 1].to_expr() {
1987-
if let ast::ExprKind::MethodCall(..) = expr.node {
1988-
context.force_one_line_chain = true;
1993+
if !combine_arg_with_callee {
1994+
if let Some(expr) = args[args.len() - 1].to_expr() {
1995+
if let ast::ExprKind::MethodCall(..) = expr.node {
1996+
context.force_one_line_chain = true;
1997+
}
19891998
}
19901999
}
19912000
last_arg_shape(args, item_vec, one_line_shape, args_max_width).and_then(|arg_shape| {

tests/source/expr.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,3 +355,7 @@ fn newlines_between_list_like_expr() {
355355
_ => bar(),
356356
};
357357
}
358+
359+
fn issue2178() {
360+
Ok(result.iter().map(|item| ls_util::rls_to_location(item)).collect())
361+
}

tests/target/chains-visual.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ fn main() {
1616
// Test case where first chain element isn't a path, but is shorter than
1717
// the size of a tab.
1818
x().y(|| match cond() {
19-
true => (),
20-
false => (),
21-
});
19+
true => (),
20+
false => (),
21+
});
2222

2323
loong_func().quux(move || if true { 1 } else { 2 });
2424

tests/target/expr.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,3 +408,10 @@ fn newlines_between_list_like_expr() {
408408
_ => bar(),
409409
};
410410
}
411+
412+
fn issue2178() {
413+
Ok(result
414+
.iter()
415+
.map(|item| ls_util::rls_to_location(item))
416+
.collect())
417+
}

0 commit comments

Comments
 (0)