Merge pull request #896 from erikjohnston/fn_arg_one_line

Add fn_arg_one_line option
This commit is contained in:
Nick Cameron 2016-04-12 10:44:01 +12:00
commit ce97bc08be
12 changed files with 415 additions and 44 deletions

View file

@ -56,6 +56,17 @@ configuration_option_enum! { StructLitStyle:
// FIXME Maybe we should also have an option to align types.
}
// How to style fn args.
configuration_option_enum! { FnArgLayoutStyle:
// First line on the same line as the opening brace, all lines aligned with
// the first line.
Visual,
// Put args on one line if they fit, or start a new line with block indent.
Block,
// First line is on a new line and all lines align with block indent.
BlockAlways,
}
configuration_option_enum! { BlockIndentStyle:
// Same level as parent.
Inherit,
@ -309,7 +320,7 @@ create_config! {
"Location of return type in function declaration";
fn_args_paren_newline: bool, true, "If function argument parenthesis goes on a newline";
fn_args_density: Density, Density::Tall, "Argument density in functions";
fn_args_layout: StructLitStyle, StructLitStyle::Visual, "Layout of function arguments";
fn_args_layout: FnArgLayoutStyle, FnArgLayoutStyle::Visual, "Layout of function arguments";
fn_arg_indent: BlockIndentStyle, BlockIndentStyle::Visual, "Indent on function arguments";
type_punctuation_density: TypeDensity, TypeDensity::Wide,
"Determines if '+' or '=' are wrapped in spaces in the punctuation of types";

View file

@ -19,7 +19,7 @@ use expr::{is_empty_block, is_simple_block_stmt, rewrite_assign_rhs};
use comment::{FindUncommented, contains_comment};
use visitor::FmtVisitor;
use rewrite::{Rewrite, RewriteContext};
use config::{Config, BlockIndentStyle, Density, ReturnIndent, BraceStyle, StructLitStyle};
use config::{Config, BlockIndentStyle, Density, ReturnIndent, BraceStyle, FnArgLayoutStyle};
use syntax::{ast, abi, ptr, codemap};
use syntax::codemap::{Span, BytePos, mk_sp};
@ -651,8 +651,8 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent)
let where_density = if (context.config.where_density == Density::Compressed &&
(!result.contains('\n') ||
context.config.fn_args_layout == StructLitStyle::Block)) ||
(context.config.fn_args_layout == StructLitStyle::Block &&
context.config.fn_args_layout == FnArgLayoutStyle::Block)) ||
(context.config.fn_args_layout == FnArgLayoutStyle::Block &&
result.is_empty()) ||
(context.config.where_density == Density::CompressedIfEmpty &&
!has_body &&
@ -1294,9 +1294,15 @@ fn rewrite_fn_base(context: &RewriteContext,
};
// Args.
let (mut one_line_budget, multi_line_budget, mut arg_indent) =
let (mut one_line_budget, mut multi_line_budget, mut arg_indent) =
compute_budgets_for_args(context, &result, indent, ret_str_len, newline_brace);
if context.config.fn_args_layout == FnArgLayoutStyle::Block ||
context.config.fn_args_layout == FnArgLayoutStyle::BlockAlways {
arg_indent = indent.block_indent(context.config);
multi_line_budget = context.config.max_width - arg_indent.width();
}
debug!("rewrite_fn: one_line_budget: {}, multi_line_budget: {}, arg_indent: {:?}",
one_line_budget,
multi_line_budget,
@ -1313,10 +1319,6 @@ fn rewrite_fn_base(context: &RewriteContext,
result.push_str("(\n");
result.push_str(&arg_indent.to_string(context.config));
}
} else if context.config.fn_args_layout == StructLitStyle::Block {
arg_indent = indent.block_indent(context.config);
result.push_str("(\n");
result.push_str(&arg_indent.to_string(context.config));
} else {
result.push('(');
}
@ -1340,23 +1342,42 @@ fn rewrite_fn_base(context: &RewriteContext,
arg_indent,
args_span,
fd.variadic));
result.push_str(&arg_str);
if context.config.fn_args_layout == StructLitStyle::Block {
let multi_line_arg_str = arg_str.contains('\n');
let put_args_in_block = match context.config.fn_args_layout {
FnArgLayoutStyle::Block => multi_line_arg_str,
FnArgLayoutStyle::BlockAlways => true,
_ => false,
} && fd.inputs.len() > 0;
if put_args_in_block {
arg_indent = indent.block_indent(context.config);
result.push('\n');
result.push_str(&arg_indent.to_string(context.config));
result.push_str(&arg_str);
result.push('\n');
result.push_str(&indent.to_string(context.config));
result.push(')');
} else {
result.push_str(&arg_str);
result.push(')');
}
result.push(')');
// Return type.
if !ret_str.is_empty() {
// If we've already gone multi-line, or the return type would push
// over the max width, then put the return type on a new line.
// Unless we are formatting args like a block, in which case there
// should always be room for the return type.
let ret_indent = if (result.contains("\n") || multi_line_ret_str ||
result.len() + indent.width() + ret_str_len >
context.config.max_width) &&
context.config.fn_args_layout != StructLitStyle::Block {
let ret_should_indent = match context.config.fn_args_layout {
// If our args are block layout then we surely must have space.
FnArgLayoutStyle::Block if put_args_in_block => false,
FnArgLayoutStyle::BlockAlways => false,
_ => {
// If we've already gone multi-line, or the return type would push
// over the max width, then put the return type on a new line.
result.contains("\n") || multi_line_ret_str ||
result.len() + indent.width() + ret_str_len > context.config.max_width
}
};
let ret_indent = if ret_should_indent {
let indent = match context.config.fn_return_indent {
ReturnIndent::WithWhereClause => indent + 4,
// Aligning with non-existent args looks silly.
@ -1407,13 +1428,13 @@ fn rewrite_fn_base(context: &RewriteContext,
}
}
let where_density = if (context.config.where_density == Density::Compressed &&
(!result.contains('\n') ||
context.config.fn_args_layout == StructLitStyle::Block)) ||
(context.config.fn_args_layout == StructLitStyle::Block &&
ret_str.is_empty()) ||
(context.config.where_density == Density::CompressedIfEmpty &&
!has_body && !result.contains('\n')) {
let should_compress_where = match context.config.where_density {
Density::Compressed => !result.contains('\n') || put_args_in_block,
Density::CompressedIfEmpty => !has_body && !result.contains('\n'),
_ => false,
} || (put_args_in_block && ret_str.is_empty());
let where_density = if should_compress_where {
Density::Compressed
} else {
Density::Tall
@ -1554,8 +1575,10 @@ fn rewrite_args(context: &RewriteContext,
_ => multi_line_budget,
};
debug!("rewrite_args: budget: {}, tactic: {:?}", budget, tactic);
let end_with_newline = match context.config.fn_args_layout {
StructLitStyle::Block => true,
FnArgLayoutStyle::Block | FnArgLayoutStyle::BlockAlways => true,
_ => false,
};

View file

@ -1,4 +1,4 @@
// rustfmt-fn_args_layout: Block
// rustfmt-fn_args_layout: BlockAlways
// rustfmt-where_indent: Inherit
// rustfmt-fn_brace_style: PreferSameLine
// Test different indents.

View file

@ -1,4 +1,4 @@
// rustfmt-fn_args_layout: Block
// rustfmt-fn_args_layout: BlockAlways
// rustfmt-fn_args_density: Vertical
// rustfmt-fn_arg_indent: Tabbed
// rustfmt-fn_brace_style: AlwaysNextLine

View file

@ -0,0 +1,50 @@
// rustfmt-fn_args_layout: Block
// rustfmt-where_indent: Inherit
// rustfmt-fn_brace_style: PreferSameLine
// Test different indents.
fn foo(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb) {
foo();
}
fn bar(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb, c: Cccccccccccccccccc, d: Dddddddddddddddd, e: Eeeeeeeeeeeeeee) {
bar();
}
fn foo(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb) -> String {
foo();
}
fn bar(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb, c: Cccccccccccccccccc, d: Dddddddddddddddd, e: Eeeeeeeeeeeeeee) -> String {
bar();
}
fn foo(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb) where T: UUUUUUUUUUU {
foo();
}
fn bar(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb, c: Cccccccccccccccccc, d: Dddddddddddddddd, e: Eeeeeeeeeeeeeee) where T: UUUUUUUUUUU {
bar();
}
fn foo(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb) -> String where T: UUUUUUUUUUU {
foo();
}
fn bar(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb, c: Cccccccccccccccccc, d: Dddddddddddddddd, e: Eeeeeeeeeeeeeee) -> String where T: UUUUUUUUUUU {
bar();
}
trait Test {
fn foo(
a: u8) {
}
fn bar(a: u8)
-> String {
}
fn bar(a: u8) -> String where Foo: foooo, Bar: barrr {}
}

View file

@ -1,5 +1,8 @@
// rustfmt-fn_args_layout: Block
fn foo() {
foo();
}
fn foo(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb) {
foo();
@ -17,8 +20,58 @@ fn bar(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb, c: Cccccccccccccccccc, d: Ddddddddd
bar();
}
fn foo(a: u8 /* Comment 1 */, b: u8 /* Comment 2 */) -> u8 {
bar()
}
fn foo(a: u8 /* Comment 1 */, b: Bbbbbbbbbbbbbb, c: Cccccccccccccccccc, d: Dddddddddddddddd, e: Eeeeeeeeeeeeeee /* Comment 2 */) -> u8 {
bar()
}
fn bar(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb, c: Cccccccccccccccccc, d: Dddddddddddddddd, e: Eeeeeeeeeeeeeee) -> String where X: Fooooo, Y: Baaar {
bar();
}
fn foo() -> T {
foo();
}
fn foo() -> T where X: Foooo, Y: Baaar {
foo();
}
fn foo() where X: Foooo {
}
fn foo() where X: Foooo, Y: Baaar {
}
fn foo() -> (Loooooooooooooooooooooong, Reeeeeeeeeeeeeeeeeeeeeeeeturn, iiiiiiiiis, Looooooooooooooooong) {
foo();
}
fn foo<g: G>() {
foo();
}
fn foo<L: Loooooooooooooooooooooong, G: Geeeeeeeeeeeneric, I: iiiiiiiiis, L: Looooooooooooooooong>() {
foo();
}
fn foo<L: Loooooooooooooooooooong, G: Geeeeeeeeeeneric, I: iiiiiiiiis, L: Loooooooooooooooong>() {
foo();
}
trait Test {
fn foo(a: u8) {}
fn bar(a: u8) -> String {}
fn bar(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb, c: Cccccccccccccccccc, d: Dddddddddddddddd, e: Eeeeeeeeeeeeeee) -> String {}
}
fn foo<L: Loooooooooooooooooooong, G: Geeeeeeeeeeneric, I: iiiiiiiiis, L: Loooooooooooooooong>(a: Aaaaaaaaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbbbbb, c: Cccccccccccccccccc, d: Dddddddddddddddd) {
foo();
}
fn foo() -> (Looooooooooooooooooooooooooong, Reeeeeeeeeeeeeeeeeeeeeeeeeeeeeturn, iiiiiiiiiiiiiis, Loooooooooooooooooooooong) {
foo();
}

View file

@ -0,0 +1,27 @@
// rustfmt-fn_args_layout: BlockAlways
fn foo() {
foo();
}
fn foo(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb) {
foo();
}
fn bar(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb, c: Cccccccccccccccccc, d: Dddddddddddddddd, e: Eeeeeeeeeeeeeee) {
bar();
}
fn foo(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb) -> String {
foo();
}
fn bar(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb, c: Cccccccccccccccccc, d: Dddddddddddddddd, e: Eeeeeeeeeeeeeee) -> String {
bar();
}
trait Test {
fn foo(a: u8) {}
fn bar(a: u8) -> String {}
}

View file

@ -1,4 +1,4 @@
// rustfmt-fn_args_layout: Block
// rustfmt-fn_args_layout: BlockAlways
// rustfmt-where_indent: Inherit
// rustfmt-fn_brace_style: PreferSameLine
// Test different indents.

View file

@ -1,4 +1,4 @@
// rustfmt-fn_args_layout: Block
// rustfmt-fn_args_layout: BlockAlways
// rustfmt-fn_args_density: Vertical
// rustfmt-fn_arg_indent: Tabbed
// rustfmt-fn_brace_style: AlwaysNextLine

View file

@ -0,0 +1,74 @@
// rustfmt-fn_args_layout: Block
// rustfmt-where_indent: Inherit
// rustfmt-fn_brace_style: PreferSameLine
// Test different indents.
fn foo(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb) {
foo();
}
fn bar(
a: Aaaaaaaaaaaaaa,
b: Bbbbbbbbbbbbbb,
c: Cccccccccccccccccc,
d: Dddddddddddddddd,
e: Eeeeeeeeeeeeeee
) {
bar();
}
fn foo(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb) -> String {
foo();
}
fn bar(
a: Aaaaaaaaaaaaaa,
b: Bbbbbbbbbbbbbb,
c: Cccccccccccccccccc,
d: Dddddddddddddddd,
e: Eeeeeeeeeeeeeee
) -> String {
bar();
}
fn foo(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb)
where T: UUUUUUUUUUU {
foo();
}
fn bar(
a: Aaaaaaaaaaaaaa,
b: Bbbbbbbbbbbbbb,
c: Cccccccccccccccccc,
d: Dddddddddddddddd,
e: Eeeeeeeeeeeeeee
) where T: UUUUUUUUUUU {
bar();
}
fn foo(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb) -> String
where T: UUUUUUUUUUU {
foo();
}
fn bar(
a: Aaaaaaaaaaaaaa,
b: Bbbbbbbbbbbbbb,
c: Cccccccccccccccccc,
d: Dddddddddddddddd,
e: Eeeeeeeeeeeeeee
) -> String
where T: UUUUUUUUUUU {
bar();
}
trait Test {
fn foo(a: u8) {}
fn bar(a: u8) -> String {}
fn bar(a: u8) -> String
where Foo: foooo,
Bar: barrr {
}
}

View file

@ -1,9 +1,10 @@
// rustfmt-fn_args_layout: Block
fn foo() {
foo();
}
fn foo(
a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb
) {
fn foo(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb) {
foo();
}
@ -17,9 +18,7 @@ fn bar(
bar();
}
fn foo(
a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb
) -> String {
fn foo(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb) -> String {
foo();
}
@ -33,14 +32,99 @@ fn bar(
bar();
}
fn foo(a: u8 /* Comment 1 */, b: u8 /* Comment 2 */) -> u8 {
bar()
}
fn foo(
a: u8, // Comment 1
b: Bbbbbbbbbbbbbb,
c: Cccccccccccccccccc,
d: Dddddddddddddddd,
e: Eeeeeeeeeeeeeee // Comment 2
) -> u8 {
bar()
}
fn bar(
a: Aaaaaaaaaaaaaa,
b: Bbbbbbbbbbbbbb,
c: Cccccccccccccccccc,
d: Dddddddddddddddd,
e: Eeeeeeeeeeeeeee
) -> String
where X: Fooooo,
Y: Baaar
{
bar();
}
fn foo() -> T {
foo();
}
fn foo() -> T
where X: Foooo,
Y: Baaar
{
foo();
}
fn foo() where X: Foooo {}
fn foo()
where X: Foooo,
Y: Baaar
{
}
fn foo
()
-> (Loooooooooooooooooooooong, Reeeeeeeeeeeeeeeeeeeeeeeeturn, iiiiiiiiis, Looooooooooooooooong)
{
foo();
}
fn foo<g: G>() {
foo();
}
fn foo<L: Loooooooooooooooooooooong, G: Geeeeeeeeeeeneric, I: iiiiiiiiis, L: Looooooooooooooooong>
() {
foo();
}
fn foo<L: Loooooooooooooooooooong, G: Geeeeeeeeeeneric, I: iiiiiiiiis, L: Loooooooooooooooong>() {
foo();
}
trait Test {
fn foo(
a: u8
) {
}
fn foo(a: u8) {}
fn bar(
a: u8
a: Aaaaaaaaaaaaaa,
b: Bbbbbbbbbbbbbb,
c: Cccccccccccccccccc,
d: Dddddddddddddddd,
e: Eeeeeeeeeeeeeee
) -> String {
}
}
fn foo<L: Loooooooooooooooooooong, G: Geeeeeeeeeeneric, I: iiiiiiiiis, L: Loooooooooooooooong>(
a: Aaaaaaaaaaaaaaaaaaaa,
b: Bbbbbbbbbbbbbbbbb,
c: Cccccccccccccccccc,
d: Dddddddddddddddd
) {
foo();
}
fn foo()
-> (Looooooooooooooooooooooooooong,
Reeeeeeeeeeeeeeeeeeeeeeeeeeeeeturn,
iiiiiiiiiiiiiis,
Loooooooooooooooooooooong)
{
foo();
}

View file

@ -0,0 +1,49 @@
// rustfmt-fn_args_layout: BlockAlways
fn foo() {
foo();
}
fn foo(
a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb
) {
foo();
}
fn bar(
a: Aaaaaaaaaaaaaa,
b: Bbbbbbbbbbbbbb,
c: Cccccccccccccccccc,
d: Dddddddddddddddd,
e: Eeeeeeeeeeeeeee
) {
bar();
}
fn foo(
a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb
) -> String {
foo();
}
fn bar(
a: Aaaaaaaaaaaaaa,
b: Bbbbbbbbbbbbbb,
c: Cccccccccccccccccc,
d: Dddddddddddddddd,
e: Eeeeeeeeeeeeeee
) -> String {
bar();
}
trait Test {
fn foo(
a: u8
) {
}
fn bar(
a: u8
) -> String {
}
}