Skip to content

stabilise fn_args_density #3581

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
Jun 3, 2019
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

## [Unreleased]

### Changed

- Change option `format_doc_comment` to `format_code_in_doc_comment`.
- `use_small_heuristics` changed to be an enum and stabilised. Configuration
options are now ready for 1.0.
- Stabilise `fn_args_density` configuration option and rename it to `fn_args_layout` #3581

## [1.2.2] 2019-04-24

Expand Down
6 changes: 3 additions & 3 deletions Configurations.md
Original file line number Diff line number Diff line change
Expand Up @@ -629,13 +629,13 @@ trailing whitespaces.
- **Possible values**: `true`, `false`
- **Stable**: No (tracking issue: #3392)

## `fn_args_density`
## `fn_args_layout`

Argument density in functions
Control the layout of arguments in a function

- **Default value**: `"Tall"`
- **Possible values**: `"Compressed"`, `"Tall"`, `"Vertical"`
- **Stable**: No (tracking issue: #3375)
- **Stable**: Yes

#### `"Tall"` (default):

Expand Down
1 change: 1 addition & 0 deletions Processes.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Open a pull request that closes the tracking issue. The tracking issue is listed

- Update the `Config` enum marking the option as stable.
- Update the the `Configuration.md` file marking the option as stable.
- Update `CHANGELOG.md` marking the option as stable.

## After the stabilisation

Expand Down
5 changes: 3 additions & 2 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ create_config! {
the same line with the pattern of arms";
force_multiline_blocks: bool, false, false,
"Force multiline closure bodies and match arms to be wrapped in a block";
fn_args_density: Density, Density::Tall, false, "Argument density in functions";
fn_args_layout: Density, Density::Tall, true,
"Control the layout of arguments in a function";
brace_style: BraceStyle, BraceStyle::SameLineWhere, false, "Brace style for items";
control_brace_style: ControlBraceStyle, ControlBraceStyle::AlwaysSameLine, false,
"Brace style for control flow constructs";
Expand Down Expand Up @@ -501,7 +502,7 @@ struct_field_align_threshold = 0
enum_discrim_align_threshold = 0
match_arm_blocks = true
force_multiline_blocks = false
fn_args_density = "Tall"
fn_args_layout = "Tall"
brace_style = "SameLineWhere"
control_brace_style = "AlwaysSameLine"
trailing_semicolon = true
Expand Down
3 changes: 2 additions & 1 deletion src/config/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ pub enum IndentStyle {

#[config_type]
/// How to place a list-like items.
/// FIXME: Issue-3581: this should be renamed to ItemsLayout when publishing 2.0
pub enum Density {
/// Fit as much on one line as possible.
Compressed,
/// Use more lines.
/// Items are placed horizontally if sufficient space, vertically otherwise.
Tall,
/// Place every item on a separate line.
Vertical,
Expand Down
28 changes: 12 additions & 16 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::comment::{
FindUncommented,
};
use crate::config::lists::*;
use crate::config::{BraceStyle, Config, Density, IndentStyle, Version};
use crate::config::{BraceStyle, Config, IndentStyle, Version};
use crate::expr::{
format_expr, is_empty_block, is_simple_block_stmt, rewrite_assign_rhs, rewrite_assign_rhs_with,
ExprType, RhsTactics,
Expand Down Expand Up @@ -703,7 +703,7 @@ pub(crate) fn format_impl(
&generics.where_clause,
context.config.brace_style(),
Shape::legacy(where_budget, offset.block_only()),
Density::Vertical,
false,
"{",
where_span_end,
self_ty.span.hi(),
Expand Down Expand Up @@ -1044,11 +1044,7 @@ pub(crate) fn format_trait(

// Rewrite where-clause.
if !generics.where_clause.predicates.is_empty() {
let where_density = if context.config.indent_style() == IndentStyle::Block {
Density::Compressed
} else {
Density::Tall
};
let where_on_new_line = context.config.indent_style() != IndentStyle::Block;

let where_budget = context.budget(last_line_width(&result));
let pos_before_where = if generic_bounds.is_empty() {
Expand All @@ -1062,7 +1058,7 @@ pub(crate) fn format_trait(
&generics.where_clause,
context.config.brace_style(),
Shape::legacy(where_budget, offset.block_only()),
where_density,
where_on_new_line,
"{",
None,
pos_before_where,
Expand Down Expand Up @@ -1171,7 +1167,7 @@ impl<'a> Rewrite for TraitAliasBounds<'a> {
&self.generics.where_clause,
context.config.brace_style(),
shape,
Density::Compressed,
false,
";",
None,
self.generics.where_clause.span.lo(),
Expand Down Expand Up @@ -1423,7 +1419,7 @@ fn format_tuple_struct(
&generics.where_clause,
context.config.brace_style(),
Shape::legacy(where_budget, offset.block_only()),
Density::Compressed,
false,
";",
None,
body_hi,
Expand Down Expand Up @@ -1499,7 +1495,7 @@ fn rewrite_type_prefix(
&generics.where_clause,
context.config.brace_style(),
Shape::legacy(where_budget, indent),
Density::Vertical,
false,
"=",
None,
generics.span.hi(),
Expand Down Expand Up @@ -2258,7 +2254,7 @@ fn rewrite_fn_base(
where_clause,
context.config.brace_style(),
Shape::indented(indent, context.config),
Density::Tall,
true,
"{",
Some(span.hi()),
pos_before_where,
Expand Down Expand Up @@ -2390,7 +2386,7 @@ fn rewrite_args(
&arg_items,
context
.config
.fn_args_density()
.fn_args_layout()
.to_list_tactic(arg_items.len()),
Separator::Comma,
one_line_budget,
Expand Down Expand Up @@ -2677,7 +2673,7 @@ fn rewrite_where_clause(
where_clause: &ast::WhereClause,
brace_style: BraceStyle,
shape: Shape,
density: Density,
on_new_line: bool,
terminator: &str,
span_end: Option<BytePos>,
span_end_before_where: BytePos,
Expand Down Expand Up @@ -2757,7 +2753,7 @@ fn rewrite_where_clause(
} else {
terminator.len()
};
if density == Density::Tall
if on_new_line
|| preds_str.contains('\n')
|| shape.indent.width() + " where ".len() + preds_str.len() + end_length > shape.width
{
Expand Down Expand Up @@ -2848,7 +2844,7 @@ fn format_generics(
&generics.where_clause,
brace_style,
Shape::legacy(budget, offset.block_only()),
Density::Tall,
true,
"{",
Some(span.hi()),
span_end_before_where,
Expand Down
5 changes: 1 addition & 4 deletions src/lists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,7 @@ where
let total_sep_len = sep.len() * sep_count.saturating_sub(1);
let real_total = total_width + total_sep_len;

if real_total <= limit
&& !pre_line_comments
&& !items.into_iter().any(|item| item.as_ref().is_multiline())
{
if real_total <= limit && !items.into_iter().any(|item| item.as_ref().is_multiline()) {
DefinitiveListTactic::Horizontal
} else {
match tactic {
Expand Down
2 changes: 1 addition & 1 deletion tests/config/small_tabs.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ comment_width = 80
tab_spaces = 2
newline_style = "Unix"
brace_style = "SameLineWhere"
fn_args_density = "Tall"
fn_args_layout = "Tall"
trailing_comma = "Vertical"
indent_style = "Block"
report_todo = "Always"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// rustfmt-fn_args_density: Compressed
// rustfmt-fn_args_layout: Compressed
// Function arguments density

trait Lorem {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// rustfmt-fn_args_density: Tall
// rustfmt-fn_args_layout: Tall
// Function arguments density

trait Lorem {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// rustfmt-fn_args_density: Vertical
// rustfmt-fn_args_layout: Vertical
// Function arguments density

trait Lorem {
Expand Down
2 changes: 1 addition & 1 deletion tests/source/fn-custom-7.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// rustfmt-normalize_comments: true
// rustfmt-fn_args_density: Vertical
// rustfmt-fn_args_layout: Vertical
// rustfmt-brace_style: AlwaysNextLine

// Case with only one variable.
Expand Down
2 changes: 1 addition & 1 deletion tests/source/fn-custom.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// rustfmt-fn_args_density: Compressed
// rustfmt-fn_args_layout: Compressed
// Test some of the ways function signatures can be customised.

// Test compressed layout of args.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// rustfmt-fn_args_density: Vertical
// rustfmt-fn_args_layout: Vertical

// Empty list should stay on one line.
fn do_bar(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// rustfmt-fn_args_density: Compressed
// rustfmt-fn_args_layout: Compressed
// Function arguments density

trait Lorem {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// rustfmt-fn_args_density: Tall
// rustfmt-fn_args_layout: Tall
// Function arguments density

trait Lorem {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// rustfmt-fn_args_density: Vertical
// rustfmt-fn_args_layout: Vertical
// Function arguments density

trait Lorem {
Expand Down
2 changes: 1 addition & 1 deletion tests/target/fn-custom-7.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// rustfmt-normalize_comments: true
// rustfmt-fn_args_density: Vertical
// rustfmt-fn_args_layout: Vertical
// rustfmt-brace_style: AlwaysNextLine

// Case with only one variable.
Expand Down
2 changes: 1 addition & 1 deletion tests/target/fn-custom.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// rustfmt-fn_args_density: Compressed
// rustfmt-fn_args_layout: Compressed
// Test some of the ways function signatures can be customised.

// Test compressed layout of args.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// rustfmt-fn_args_density: Vertical
// rustfmt-fn_args_layout: Vertical

// Empty list should stay on one line.
fn do_bar() -> u8 {
Expand Down