Skip to content

support closure block indent_style #3867

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 5 commits into from
Oct 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions src/closures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use syntax::{ast, ptr};

use crate::attr::get_attrs_from_stmt;
use crate::config::lists::*;
use crate::config::Version;
use crate::config::{IndentStyle, SeparatorTactic, Version};
use crate::expr::{block_contains_comment, is_simple_block, is_unsafe_block, rewrite_cond};
use crate::items::{span_hi_for_param, span_lo_for_param};
use crate::lists::{definitive_tactic, itemize_list, write_list, ListFormatting, Separator};
Expand Down Expand Up @@ -238,9 +238,16 @@ fn rewrite_closure_fn_decl(
.shrink_left(is_async.len() + mover.len() + immovable.len())?
.sub_width(4)?;

let indent_style = context.config.indent_style();

// 1 = |
let param_offset = nested_shape.indent + 1;
let param_shape = nested_shape.offset_left(1)?.visual_indent(0);
let param_shape = match indent_style {
IndentStyle::Block => {
Shape::indented(shape.indent.block_indent(context.config), context.config)
}
IndentStyle::Visual => nested_shape.offset_left(1)?.visual_indent(0),
};
let ret_str = fn_decl.output.rewrite(context, param_shape)?;

let param_items = itemize_list(
Expand Down Expand Up @@ -273,10 +280,31 @@ fn rewrite_closure_fn_decl(
.tactic(tactic)
.preserve_newline(true);
let list_str = write_list(&item_vec, &fmt)?;
let mut prefix = format!("{}{}{}|{}|", is_async, immovable, mover, list_str);
let one_line_budget = context.budget(param_shape.indent.width());
let multi_line_params = match indent_style {
IndentStyle::Block => list_str.contains('\n') || list_str.len() > one_line_budget,
_ => false,
};
let put_params_in_block = multi_line_params && !item_vec.is_empty();
let param_str = if put_params_in_block {
let trailing_comma = match context.config.trailing_comma() {
SeparatorTactic::Never => "",
_ => ",",
};
format!(
"{}{}{}{}",
param_shape.indent.to_string_with_newline(context.config),
&list_str,
trailing_comma,
shape.indent.to_string_with_newline(context.config)
)
} else {
list_str
};
let mut prefix = format!("{}{}{}|{}|", is_async, immovable, mover, param_str);

if !ret_str.is_empty() {
if prefix.contains('\n') {
if prefix.contains('\n') && !put_params_in_block {
prefix.push('\n');
prefix.push_str(&param_offset.to_string(context.config));
} else {
Expand Down
13 changes: 13 additions & 0 deletions tests/source/closure.rs → tests/source/closure_block_style.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// rustfmt-indent_style: Block
// rustfmt-normalize_comments: true
// Closures

Expand Down Expand Up @@ -211,3 +212,15 @@ fn issue2262() {
slave: None,
})?;
}

fn issue_3865() {
{
let write_status = |status: &mut Vec<ansi_term::ANSIString>,
diff: &Diff,
heading: &str,
color: &Style,
show_hints: bool,
hints: &[&str]|-> Option<bool> { Some(true) };}
let baz = |foo: bool| -> Option<bool> { Some(true) };
let write_status = |status: &mut Vec<ansi_term::ANSIString>,diff: &Diff,heading: &str,color: &Style,| -> Option<bool> { Some(true) };
}
17 changes: 17 additions & 0 deletions tests/source/closure_block_style_no_comma.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// rustfmt-indent_style: Block
// rustfmt-trailing_comma: Never

fn foo() {
{
let write_status = |status: &mut Vec<ansi_term::ANSIString>,
diff: &Diff,
heading: &str,
color: &Style,
show_hints: bool,
hints: &[&str]|-> Option<bool> { Some(true) };}
}

fn bar() {
let write_status = |status: &mut Vec<ansi_term::ANSIString>,diff: &Diff,heading: &str,color: &Style,| -> Option<bool> { Some(true) };
let baz = |foo: bool| -> Option<bool> { Some(true) };
}
70 changes: 70 additions & 0 deletions tests/source/closure_visual_style.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// rustfmt-indent_style: Visual
// rustfmt-normalize_comments: true

fn foo() {
{
let write_status = |status: &mut Vec<ansi_term::ANSIString>,
diff: &Diff,
heading: &str,
color: &Style,
show_hints: bool,
hints: &[&str]|-> Option<bool> { Some(true) };}
}

fn bar() {
let write_status = |status: &mut Vec<ansi_term::ANSIString>,diff: &Diff,heading: &str,color: &Style,| -> Option<bool> { Some(true) };
let baz = |foo: bool| -> Option<bool> { Some(true) };
}

fn main() {
let square = ( |i: i32 | i * i );

let commented = |// first
a, // argument
// second
b: WithType, // argument
// ignored
_| {
(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb)
};

let commented2 = |/* first */ a /*argument*/, /* second*/ b: WithType /* argument*/, /* ignored */ _ |
(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);

let block_body = move |xxxxxxxxxxxxxxxxxxxxxxxxxxxxx, ref yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy| {
xxxxxxxxxxxxxxxxxxxxxxxxxxxxx + yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
};

let loooooooooooooong_name = |field| {
// format comments.
if field.node.attrs.len() > 0 { field.node.attrs[0].span.lo()
} else {
field.span.lo()
}};

let unblock_me = |trivial| {
closure()
};

let empty = |arg| {};

let simple = |arg| { /* comment formatting */ foo(arg) };

let test = | | { do_something(); do_something_else(); };

let arg_test = |big_argument_name, test123| looooooooooooooooooong_function_naaaaaaaaaaaaaaaaame();

let arg_test = |big_argument_name, test123| {looooooooooooooooooong_function_naaaaaaaaaaaaaaaaame()};

let simple_closure = move || -> () {};

let closure = |input: Ty| -> Option<String> {
foo()
};

let closure_with_return_type = |aaaaaaaaaaaaaaaaaaaaaaarg1, aaaaaaaaaaaaaaaaaaaaaaarg2| -> Strong { "sup".to_owned() };

|arg1, arg2, _, _, arg3, arg4| { let temp = arg4 + arg3;
arg2 * arg1 - temp };

}
43 changes: 33 additions & 10 deletions tests/target/closure.rs → tests/target/closure_block_style.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
// rustfmt-indent_style: Block
// rustfmt-normalize_comments: true
// Closures

fn main() {
let square = (|i: i32| i * i);

let commented = |// first
a, // argument
// second
b: WithType, // argument
// ignored
_| {
let commented = |
// first
a, // argument
// second
b: WithType, // argument
// ignored
_,
| {
(
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
)
};

let block_body = move |xxxxxxxxxxxxxxxxxxxxxxxxxxxxx,
ref yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy| {
xxxxxxxxxxxxxxxxxxxxxxxxxxxxx + yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
};
let block_body = move |
xxxxxxxxxxxxxxxxxxxxxxxxxxxxx,
ref yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy,
| { xxxxxxxxxxxxxxxxxxxxxxxxxxxxx + yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy };

let loooooooooooooong_name = |field| {
// format comments.
Expand Down Expand Up @@ -248,3 +251,23 @@ fn issue2262() {
slave: None,
})?;
}

fn issue_3865() {
{
let write_status = |
status: &mut Vec<ansi_term::ANSIString>,
diff: &Diff,
heading: &str,
color: &Style,
show_hints: bool,
hints: &[&str],
| -> Option<bool> { Some(true) };
}
let baz = |foo: bool| -> Option<bool> { Some(true) };
let write_status = |
status: &mut Vec<ansi_term::ANSIString>,
diff: &Diff,
heading: &str,
color: &Style,
| -> Option<bool> { Some(true) };
}
25 changes: 25 additions & 0 deletions tests/target/closure_block_style_no_comma.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// rustfmt-indent_style: Block
// rustfmt-trailing_comma: Never

fn foo() {
{
let write_status = |
status: &mut Vec<ansi_term::ANSIString>,
diff: &Diff,
heading: &str,
color: &Style,
show_hints: bool,
hints: &[&str]
| -> Option<bool> { Some(true) };
}
}

fn bar() {
let write_status = |
status: &mut Vec<ansi_term::ANSIString>,
diff: &Diff,
heading: &str,
color: &Style
| -> Option<bool> { Some(true) };
let baz = |foo: bool| -> Option<bool> { Some(true) };
}
90 changes: 90 additions & 0 deletions tests/target/closure_visual_style.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// rustfmt-indent_style: Visual
// rustfmt-normalize_comments: true

fn foo() {
{
let write_status = |status: &mut Vec<ansi_term::ANSIString>,
diff: &Diff,
heading: &str,
color: &Style,
show_hints: bool,
hints: &[&str]|
-> Option<bool> { Some(true) };
}
}

fn bar() {
let write_status = |status: &mut Vec<ansi_term::ANSIString>,
diff: &Diff,
heading: &str,
color: &Style|
-> Option<bool> { Some(true) };
let baz = |foo: bool| -> Option<bool> { Some(true) };
}

fn main() {
let square = (|i: i32| i * i);

let commented = |// first
a, // argument
// second
b: WithType, // argument
// ignored
_| {
(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb)
};

let commented2 =
|// first
a, // argument
// second
b: WithType, // argument
// ignored
_| (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);

let block_body = move |xxxxxxxxxxxxxxxxxxxxxxxxxxxxx,
ref yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy| {
xxxxxxxxxxxxxxxxxxxxxxxxxxxxx + yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
};

let loooooooooooooong_name = |field| {
// format comments.
if field.node.attrs.len() > 0 {
field.node.attrs[0].span.lo()
} else {
field.span.lo()
}
};

let unblock_me = |trivial| closure();

let empty = |arg| {};

let simple = |arg| {
// comment formatting
foo(arg)
};

let test = || {
do_something();
do_something_else();
};

let arg_test =
|big_argument_name, test123| looooooooooooooooooong_function_naaaaaaaaaaaaaaaaame();

let arg_test =
|big_argument_name, test123| looooooooooooooooooong_function_naaaaaaaaaaaaaaaaame();

let simple_closure = move || -> () {};

let closure = |input: Ty| -> Option<String> { foo() };

let closure_with_return_type =
|aaaaaaaaaaaaaaaaaaaaaaarg1, aaaaaaaaaaaaaaaaaaaaaaarg2| -> Strong { "sup".to_owned() };

|arg1, arg2, _, _, arg3, arg4| {
let temp = arg4 + arg3;
arg2 * arg1 - temp
};
}
12 changes: 7 additions & 5 deletions tests/target/fn-param-attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ impl MutStruct {
}

fn main() {
let c = |#[allow(C)] a: u32,
#[cfg(something)] b: i32,
#[cfg_attr(something, cfg(nothing))]
#[deny(C)]
c: i32| {};
let c = |
#[allow(C)] a: u32,
#[cfg(something)] b: i32,
#[cfg_attr(something, cfg(nothing))]
#[deny(C)]
c: i32,
| {};
let _ = c(1, 2);
}

Expand Down
4 changes: 2 additions & 2 deletions tests/target/mulit-file.rs → tests/target/multi-file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
// crash.

#[cfg(all(foo))]
#[path = "closure.rs"]
#[path = "closure_block_style.rs"]
pub mod imp;

#[cfg(all(bar))]
#[path = "closure.rs"]
#[path = "closure_block_style.rs"]
pub mod imp;