Support struct-like layouts for fn args
This commit is contained in:
parent
f81485d9d6
commit
fae93abbda
@ -90,6 +90,7 @@ pub fn override_value(&mut self, key: &str, val: &str) {
|
||||
fn_return_indent: ReturnIndent,
|
||||
fn_args_paren_newline: bool,
|
||||
fn_args_density: Density,
|
||||
fn_args_layout: StructLitStyle,
|
||||
fn_arg_indent: BlockIndentStyle,
|
||||
where_density: Density, // Should we at least try to put the where clause on the same line as
|
||||
// the rest of the function decl?
|
||||
@ -123,6 +124,7 @@ fn default() -> Config {
|
||||
fn_return_indent: ReturnIndent::WithArgs,
|
||||
fn_args_paren_newline: true,
|
||||
fn_args_density: Density::Tall,
|
||||
fn_args_layout: StructLitStyle::Visual,
|
||||
fn_arg_indent: BlockIndentStyle::Visual,
|
||||
where_density: Density::Tall,
|
||||
where_indent: BlockIndentStyle::Tabbed,
|
||||
@ -131,7 +133,7 @@ fn default() -> Config {
|
||||
generics_indent: BlockIndentStyle::Visual,
|
||||
struct_trailing_comma: SeparatorTactic::Vertical,
|
||||
struct_lit_trailing_comma: SeparatorTactic::Vertical,
|
||||
struct_lit_style: StructLitStyle::BlockIndent,
|
||||
struct_lit_style: StructLitStyle::Block,
|
||||
enum_trailing_comma: true,
|
||||
report_todo: ReportTactic::Always,
|
||||
report_fixme: ReportTactic::Never,
|
||||
|
@ -940,10 +940,10 @@ enum StructLitField<'a> {
|
||||
// Foo { a: Foo } - indent is +3, width is -5.
|
||||
let h_budget = try_opt!(width.checked_sub(path_str.len() + 5));
|
||||
let (indent, v_budget) = match context.config.struct_lit_style {
|
||||
StructLitStyle::VisualIndent => {
|
||||
StructLitStyle::Visual => {
|
||||
(offset + path_str.len() + 3, h_budget)
|
||||
}
|
||||
StructLitStyle::BlockIndent => {
|
||||
StructLitStyle::Block => {
|
||||
// If we are all on one line, then we'll ignore the indent, and we
|
||||
// have a smaller budget.
|
||||
let indent = context.block_indent + context.config.tab_spaces;
|
||||
@ -1012,7 +1012,7 @@ enum StructLitField<'a> {
|
||||
let fields_str = write_list(&items.collect::<Vec<_>>(), &fmt);
|
||||
|
||||
match context.config.struct_lit_style {
|
||||
StructLitStyle::BlockIndent if fields_str.contains('\n') => {
|
||||
StructLitStyle::Block if fields_str.contains('\n') => {
|
||||
let inner_indent = make_indent(context.block_indent + context.config.tab_spaces);
|
||||
let outer_indent = make_indent(context.block_indent);
|
||||
Some(format!("{} {{\n{}{}\n{}}}", path_str, inner_indent, fields_str, outer_indent))
|
||||
@ -1020,7 +1020,7 @@ enum StructLitField<'a> {
|
||||
_ => Some(format!("{} {{ {} }}", path_str, fields_str)),
|
||||
}
|
||||
|
||||
// FIXME if context.config.struct_lit_style == VisualIndent, but we run out
|
||||
// FIXME if context.config.struct_lit_style == Visual, but we run out
|
||||
// of space, we should fall back to BlockIndent.
|
||||
}
|
||||
|
||||
|
23
src/items.rs
23
src/items.rs
@ -10,7 +10,7 @@
|
||||
|
||||
// Formatting top-level items - functions, structs, enums, traits, impls.
|
||||
|
||||
use {ReturnIndent, BraceStyle};
|
||||
use {ReturnIndent, BraceStyle, StructLitStyle};
|
||||
use utils::{format_mutability, format_visibility, make_indent, contains_skip, span_after,
|
||||
end_typaram};
|
||||
use lists::{write_list, itemize_list, ListItem, ListFormatting, SeparatorTactic, ListTactic};
|
||||
@ -225,6 +225,10 @@ fn rewrite_fn_base(&mut self,
|
||||
result.push_str("(\n");
|
||||
result.push_str(&make_indent(arg_indent));
|
||||
}
|
||||
} else if self.config.fn_args_layout == StructLitStyle::Block {
|
||||
arg_indent = indent + self.config.tab_spaces;
|
||||
result.push_str("(\n");
|
||||
result.push_str(&make_indent(arg_indent));
|
||||
} else {
|
||||
result.push('(');
|
||||
}
|
||||
@ -245,14 +249,20 @@ fn rewrite_fn_base(&mut self,
|
||||
indent,
|
||||
arg_indent,
|
||||
args_span));
|
||||
if self.config.fn_args_layout == StructLitStyle::Block {
|
||||
result.push('\n');
|
||||
}
|
||||
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.
|
||||
if result.contains("\n") ||
|
||||
result.len() + indent + ret_str.len() > self.config.max_width {
|
||||
// Unless we are formatting args like a block, in which case there
|
||||
// should always be room for the return type.
|
||||
if (result.contains("\n") ||
|
||||
result.len() + indent + ret_str.len() > self.config.max_width) &&
|
||||
self.config.fn_args_layout != StructLitStyle::Block {
|
||||
let indent = match self.config.fn_return_indent {
|
||||
ReturnIndent::WithWhereClause => indent + 4,
|
||||
// TODO we might want to check that using the arg indent doesn't
|
||||
@ -285,8 +295,11 @@ fn rewrite_fn_base(&mut self,
|
||||
}
|
||||
}
|
||||
|
||||
let where_density = if self.config.where_density == Density::Compressed &&
|
||||
!result.contains('\n') {
|
||||
let where_density = if (self.config.where_density == Density::Compressed &&
|
||||
(!result.contains('\n') ||
|
||||
self.config.fn_args_layout == StructLitStyle::Block)) ||
|
||||
(self.config.fn_args_layout == StructLitStyle::Block &&
|
||||
ret_str.is_empty()) {
|
||||
Density::Compressed
|
||||
} else {
|
||||
Density::Tall
|
||||
|
@ -133,13 +133,13 @@ pub enum ReturnIndent {
|
||||
pub enum StructLitStyle {
|
||||
// First line on the same line as the opening brace, all lines aligned with
|
||||
// the first line.
|
||||
VisualIndent,
|
||||
Visual,
|
||||
// First line is on a new line and all lines align with block indent.
|
||||
BlockIndent,
|
||||
Block,
|
||||
// FIXME Maybe we should also have an option to align types.
|
||||
}
|
||||
|
||||
impl_enum_decodable!(StructLitStyle, VisualIndent, BlockIndent);
|
||||
impl_enum_decodable!(StructLitStyle, Visual, Block);
|
||||
|
||||
enum ErrorKind {
|
||||
// Line has exceeded character limit
|
||||
|
@ -7,6 +7,7 @@ fn_brace_style = "SameLineWhere"
|
||||
fn_return_indent = "WithArgs"
|
||||
fn_args_paren_newline = true
|
||||
fn_args_density = "Tall"
|
||||
fn_args_layout = "Visual"
|
||||
fn_arg_indent = "Visual"
|
||||
where_density = "Tall"
|
||||
where_indent = "Tabbed"
|
||||
@ -15,7 +16,7 @@ where_pred_indent = "Visual"
|
||||
generics_indent = "Visual"
|
||||
struct_trailing_comma = "Vertical"
|
||||
struct_lit_trailing_comma = "Vertical"
|
||||
struct_lit_style = "BlockIndent"
|
||||
struct_lit_style = "Block"
|
||||
enum_trailing_comma = true
|
||||
report_todo = "Always"
|
||||
report_fixme = "Never"
|
||||
|
36
tests/source/fn-custom-6.rs
Normal file
36
tests/source/fn-custom-6.rs
Normal file
@ -0,0 +1,36 @@
|
||||
// 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();
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
// rustfmt-struct_lit_style: VisualIndent
|
||||
// rustfmt-struct_lit_style: Visual
|
||||
|
||||
// Struct literal expressions.
|
||||
|
||||
|
70
tests/target/fn-custom-6.rs
Normal file
70
tests/target/fn-custom-6.rs
Normal file
@ -0,0 +1,70 @@
|
||||
// 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();
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
// rustfmt-struct_lit_style: VisualIndent
|
||||
// rustfmt-struct_lit_style: Visual
|
||||
|
||||
// Struct literal expressions.
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user