Add variant to fn_args_layout

This commit is contained in:
Erik Johnston 2016-04-07 20:01:16 +01:00
parent 615848339b
commit 4edc6f1a4e
8 changed files with 241 additions and 169 deletions

View File

@ -56,6 +56,17 @@ pub enum $e {
// 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,9 +320,8 @@ fn default() -> 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";
fn_arg_one_line: bool, false, "Keep arguments on one line if they fit";
type_punctuation_density: TypeDensity, TypeDensity::Wide,
"Determines if '+' or '=' are wrapped in spaces in the punctuation of types";
// Should we at least try to put the where clause on the same line as the rest of the

View File

@ -19,7 +19,7 @@
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 &&
@ -1293,7 +1293,8 @@ fn rewrite_fn_base(context: &RewriteContext,
let (mut one_line_budget, multi_line_budget, mut arg_indent) =
compute_budgets_for_args(context, &result, indent, ret_str_len, newline_brace);
if context.config.fn_args_layout == StructLitStyle::Block {
if context.config.fn_args_layout == FnArgLayoutStyle::Block ||
context.config.fn_args_layout == FnArgLayoutStyle::BlockAlways {
arg_indent = indent.block_indent(context.config);
}
@ -1339,11 +1340,13 @@ fn rewrite_fn_base(context: &RewriteContext,
let multi_line_arg_str = arg_str.contains('\n');
let should_put_args_in_block = context.config.fn_args_layout == StructLitStyle::Block &&
(multi_line_arg_str || !context.config.fn_arg_one_line) &&
fd.inputs.len() > 0;
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 should_put_args_in_block {
if put_args_in_block {
arg_indent = indent.block_indent(context.config);
result.push('\n');
result.push_str(&arg_indent.to_string(context.config));
@ -1358,14 +1361,17 @@ fn rewrite_fn_base(context: &RewriteContext,
// Return type.
if !ret_str.is_empty() {
let ret_should_indent = match context.config.fn_args_layout {
FnArgLayoutStyle::Block if put_args_in_block => false,
FnArgLayoutStyle::BlockAlways => false,
_ => {
result.contains("\n") || multi_line_ret_str ||
result.len() + indent.width() + ret_str_len > context.config.max_width
}
};
// 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_indent = if ret_should_indent {
let indent = match context.config.fn_return_indent {
ReturnIndent::WithWhereClause => indent + 4,
// Aligning with non-existent args looks silly.
@ -1416,13 +1422,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 where_compressed = 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 where_compressed {
Density::Compressed
} else {
Density::Tall
@ -1564,7 +1570,7 @@ enum ArgumentKind<'a> {
};
let end_with_newline = match context.config.fn_args_layout {
StructLitStyle::Block => true,
FnArgLayoutStyle::Block | FnArgLayoutStyle::BlockAlways => true,
_ => false,
};

View File

@ -1,48 +0,0 @@
// rustfmt-fn_args_layout: Block
// rustfmt-fn_arg_one_line: true
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();
}
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();
}
trait Test {
fn foo(a: u8) {}
fn bar(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb, c: Cccccccccccccccccc, d: Dddddddddddddddd, e: Eeeeeeeeeeeeeee) -> String {}
}

View File

@ -20,8 +20,54 @@ 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();
}
fn foo<L: Loooooooooooooooooooong, G: Geeeeeeeeeeneric, I: iiiiiiiiis, L: Loooooooooooooooong>(a: Aaaaaaaaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbbbbb, c: Cccccccccccccccccc, d: Dddddddddddddddd) {
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 {}
}

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,85 +0,0 @@
// rustfmt-fn_args_layout: Block
// rustfmt-fn_arg_one_line: true
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();
}
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();
}
trait Test {
fn foo(a: u8) {}
fn bar(
a: Aaaaaaaaaaaaaa,
b: Bbbbbbbbbbbbbb,
c: Cccccccccccccccccc,
d: Dddddddddddddddd,
e: Eeeeeeeeeeeeeee
) -> String {
}
}

View File

@ -4,9 +4,7 @@ fn foo() {
foo();
}
fn foo(
a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb
) {
fn foo(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb) {
foo();
}
@ -20,9 +18,7 @@ fn bar(
bar();
}
fn foo(
a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb
) -> String {
fn foo(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb) -> String {
foo();
}
@ -36,14 +32,85 @@ 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();
}
fn foo<L: Loooooooooooooooooooong, G: Geeeeeeeeeeneric, I: iiiiiiiiis, L: Loooooooooooooooong>(a: Aaaaaaaaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbbbbb, c: Cccccccccccccccccc, d: Dddddddddddddddd) {
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 {
}
}

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 {
}
}