Skip to content

Adding where_single_line option #2030

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Nov 5, 2017
1 change: 1 addition & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,7 @@ create_config! {
// function decl?
// 2. Currently options `Tall` and `Vertical` produce the same output.
where_density: Density, Density::Vertical, false, "Density of a where clause";
where_single_line: bool, false, false, "To force single line where layout";
Copy link
Contributor

@remram44 remram44 Mar 31, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both the name of the option (where single line do what?) and the "documentation" (where what is layout?) are extremely confusing. Configurations.md does not help; as a user I have no idea what this does.

[edit: filed #2580]

where_layout: ListTactic, ListTactic::Vertical, false, "Element layout inside a where clause";
where_pred_indent: IndentStyle, IndentStyle::Visual, false,
"Indentation style of a where predicate";
Expand Down
39 changes: 33 additions & 6 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,7 @@ pub fn format_impl(
where_span_end,
self_ty.span.hi(),
option,
false,
)?;

// If there is no where clause, we may have missing comments between the trait name and
Expand Down Expand Up @@ -960,6 +961,7 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent)
None,
pos_before_where,
option,
false,
)?;
// If the where clause cannot fit on the same line,
// put the where clause on a new line
Expand Down Expand Up @@ -1208,6 +1210,7 @@ fn format_tuple_struct(
None,
body_hi,
option,
false,
)?
}
None => "".to_owned(),
Expand Down Expand Up @@ -1297,6 +1300,7 @@ pub fn rewrite_type_alias(
Some(span.hi()),
generics.span.hi(),
option,
false,
)?;
result.push_str(&where_clause_str);
if where_clause_str.is_empty() {
Expand Down Expand Up @@ -1936,6 +1940,8 @@ fn rewrite_fn_base(
ast::FunctionRetTy::Ty(ref ty) => ty.span.hi(),
};

let is_args_multi_lined = arg_str.contains('\n');

if where_clause.predicates.len() == 1 && should_compress_where {
let budget = context.budget(last_line_used_width(&result, indent.width()));
if let Some(where_clause_str) = rewrite_where_clause(
Expand All @@ -1948,6 +1954,7 @@ fn rewrite_fn_base(
Some(span.hi()),
pos_before_where,
WhereClauseOption::compressed(),
is_args_multi_lined,
) {
result.push_str(&where_clause_str);
force_new_line_for_brace |= last_line_contains_single_line_comment(&result);
Expand All @@ -1966,6 +1973,7 @@ fn rewrite_fn_base(
Some(span.hi()),
pos_before_where,
option,
is_args_multi_lined,
)?;
// If there are neither where clause nor return type, we may be missing comments between
// args and `{`.
Expand All @@ -1989,6 +1997,7 @@ fn rewrite_fn_base(
result.push_str(&where_clause_str);

force_new_line_for_brace |= last_line_contains_single_line_comment(&result);
force_new_line_for_brace |= is_args_multi_lined && context.config.where_single_line();
Some((result, force_new_line_for_brace))
}

Expand Down Expand Up @@ -2246,11 +2255,16 @@ fn compute_budgets_for_args(
}

fn newline_for_brace(config: &Config, where_clause: &ast::WhereClause, has_body: bool) -> bool {
let predicate_count = where_clause.predicates.len();

if config.where_single_line() && predicate_count == 1 {
return false;
}
match (config.fn_brace_style(), config.where_density()) {
(BraceStyle::AlwaysNextLine, _) => true,
(_, Density::Compressed) if where_clause.predicates.len() == 1 => false,
(_, Density::CompressedIfEmpty) if where_clause.predicates.len() == 1 && !has_body => false,
(BraceStyle::SameLineWhere, _) if !where_clause.predicates.is_empty() => true,
(_, Density::Compressed) if predicate_count == 1 => false,
(_, Density::CompressedIfEmpty) if predicate_count == 1 && !has_body => false,
(BraceStyle::SameLineWhere, _) if predicate_count > 0 => true,
_ => false,
}
}
Expand Down Expand Up @@ -2426,6 +2440,7 @@ fn rewrite_where_clause_rfc_style(
span_end: Option<BytePos>,
span_end_before_where: BytePos,
where_clause_option: WhereClauseOption,
is_args_multi_line: bool,
) -> Option<String> {
let block_shape = shape.block().with_max_width(context.config);

Expand Down Expand Up @@ -2461,14 +2476,23 @@ fn rewrite_where_clause_rfc_style(
span_end,
false,
);
let comma_tactic = if where_clause_option.suppress_comma {
let where_single_line = context.config.where_single_line() && len == 1 && !is_args_multi_line;
let comma_tactic = if where_clause_option.suppress_comma || where_single_line {
SeparatorTactic::Never
} else {
context.config.trailing_comma()
};

// shape should be vertical only and only if we have `where_single_line` option enabled
// and the number of items of the where clause is equal to 1
let shape_tactic = if where_single_line {
DefinitiveListTactic::Horizontal
} else {
DefinitiveListTactic::Vertical
};

let fmt = ListFormatting {
tactic: DefinitiveListTactic::Vertical,
tactic: shape_tactic,
separator: ",",
trailing_separator: comma_tactic,
separator_place: SeparatorPlace::Back,
Expand All @@ -2490,7 +2514,7 @@ fn rewrite_where_clause_rfc_style(
// 6 = `where `
let clause_sep = if where_clause_option.compress_where && comment_before.is_empty()
&& comment_after.is_empty() && !preds_str.contains('\n')
&& 6 + preds_str.len() <= shape.width
&& 6 + preds_str.len() <= shape.width || where_single_line
{
String::from(" ")
} else {
Expand Down Expand Up @@ -2518,6 +2542,7 @@ fn rewrite_where_clause(
span_end: Option<BytePos>,
span_end_before_where: BytePos,
where_clause_option: WhereClauseOption,
is_args_multi_line: bool,
) -> Option<String> {
if where_clause.predicates.is_empty() {
return Some(String::new());
Expand All @@ -2532,6 +2557,7 @@ fn rewrite_where_clause(
span_end,
span_end_before_where,
where_clause_option,
is_args_multi_line,
);
}

Expand Down Expand Up @@ -2673,6 +2699,7 @@ fn format_generics(
Some(span.hi()),
generics.span.hi(),
option,
false,
)?;
result.push_str(&where_clause_str);
force_same_line_brace || brace_style == BraceStyle::PreferSameLine
Expand Down
24 changes: 24 additions & 0 deletions tests/source/configs-where_single_line.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// rustfmt-where_single_line: true
// Where style


fn lorem_two_items<Ipsum, Dolor, Sit, Amet>() -> T where Ipsum: Eq, Lorem: Eq {
// body
}

fn lorem_multi_line<Ipsum, Dolor, Sit, Amet>(
a: Aaaaaaaaaaaaaaa,
b: Bbbbbbbbbbbbbbbb,
c: Ccccccccccccccccc,
d: Ddddddddddddddddddddddddd,
e: Eeeeeeeeeeeeeeeeeee,
) -> T
where
Ipsum: Eq,
{
// body
}

fn lorem<Ipsum, Dolor, Sit, Amet>() -> T where Ipsum: Eq {
// body
}
29 changes: 29 additions & 0 deletions tests/target/configs-where_single_line.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// rustfmt-where_single_line: true
// Where style


fn lorem_two_items<Ipsum, Dolor, Sit, Amet>() -> T
where
Ipsum: Eq,
Lorem: Eq,
{
// body
}

fn lorem_multi_line<Ipsum, Dolor, Sit, Amet>(
a: Aaaaaaaaaaaaaaa,
b: Bbbbbbbbbbbbbbbb,
c: Ccccccccccccccccc,
d: Ddddddddddddddddddddddddd,
e: Eeeeeeeeeeeeeeeeeee,
) -> T
where
Ipsum: Eq,
{
// body
}

fn lorem<Ipsum, Dolor, Sit, Amet>() -> T
where Ipsum: Eq {
// body
}