mirror of
https://github.com/rust-lang/rust.git
synced 2025-06-04 19:29:07 +00:00
commit
000ea50123
@ -89,7 +89,8 @@ create_config! {
|
|||||||
fn_brace_style: BraceStyle,
|
fn_brace_style: BraceStyle,
|
||||||
fn_return_indent: ReturnIndent,
|
fn_return_indent: ReturnIndent,
|
||||||
fn_args_paren_newline: bool,
|
fn_args_paren_newline: bool,
|
||||||
fn_args_layout: Density,
|
fn_args_density: Density,
|
||||||
|
fn_args_layout: StructLitStyle,
|
||||||
fn_arg_indent: BlockIndentStyle,
|
fn_arg_indent: BlockIndentStyle,
|
||||||
where_density: Density, // Should we at least try to put the where clause on the same line as
|
where_density: Density, // Should we at least try to put the where clause on the same line as
|
||||||
// the rest of the function decl?
|
// the rest of the function decl?
|
||||||
@ -122,7 +123,8 @@ impl Default for Config {
|
|||||||
fn_brace_style: BraceStyle::SameLineWhere,
|
fn_brace_style: BraceStyle::SameLineWhere,
|
||||||
fn_return_indent: ReturnIndent::WithArgs,
|
fn_return_indent: ReturnIndent::WithArgs,
|
||||||
fn_args_paren_newline: true,
|
fn_args_paren_newline: true,
|
||||||
fn_args_layout: Density::Tall,
|
fn_args_density: Density::Tall,
|
||||||
|
fn_args_layout: StructLitStyle::Visual,
|
||||||
fn_arg_indent: BlockIndentStyle::Visual,
|
fn_arg_indent: BlockIndentStyle::Visual,
|
||||||
where_density: Density::Tall,
|
where_density: Density::Tall,
|
||||||
where_indent: BlockIndentStyle::Tabbed,
|
where_indent: BlockIndentStyle::Tabbed,
|
||||||
@ -131,7 +133,7 @@ impl Default for Config {
|
|||||||
generics_indent: BlockIndentStyle::Visual,
|
generics_indent: BlockIndentStyle::Visual,
|
||||||
struct_trailing_comma: SeparatorTactic::Vertical,
|
struct_trailing_comma: SeparatorTactic::Vertical,
|
||||||
struct_lit_trailing_comma: SeparatorTactic::Vertical,
|
struct_lit_trailing_comma: SeparatorTactic::Vertical,
|
||||||
struct_lit_style: StructLitStyle::BlockIndent,
|
struct_lit_style: StructLitStyle::Block,
|
||||||
enum_trailing_comma: true,
|
enum_trailing_comma: true,
|
||||||
report_todo: ReportTactic::Always,
|
report_todo: ReportTactic::Always,
|
||||||
report_fixme: ReportTactic::Never,
|
report_fixme: ReportTactic::Never,
|
||||||
|
@ -940,10 +940,10 @@ fn rewrite_struct_lit<'a>(context: &RewriteContext,
|
|||||||
// Foo { a: Foo } - indent is +3, width is -5.
|
// Foo { a: Foo } - indent is +3, width is -5.
|
||||||
let h_budget = try_opt!(width.checked_sub(path_str.len() + 5));
|
let h_budget = try_opt!(width.checked_sub(path_str.len() + 5));
|
||||||
let (indent, v_budget) = match context.config.struct_lit_style {
|
let (indent, v_budget) = match context.config.struct_lit_style {
|
||||||
StructLitStyle::VisualIndent => {
|
StructLitStyle::Visual => {
|
||||||
(offset + path_str.len() + 3, h_budget)
|
(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
|
// If we are all on one line, then we'll ignore the indent, and we
|
||||||
// have a smaller budget.
|
// have a smaller budget.
|
||||||
let indent = context.block_indent + context.config.tab_spaces;
|
let indent = context.block_indent + context.config.tab_spaces;
|
||||||
@ -1012,7 +1012,7 @@ fn rewrite_struct_lit<'a>(context: &RewriteContext,
|
|||||||
let fields_str = write_list(&items.collect::<Vec<_>>(), &fmt);
|
let fields_str = write_list(&items.collect::<Vec<_>>(), &fmt);
|
||||||
|
|
||||||
match context.config.struct_lit_style {
|
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 inner_indent = make_indent(context.block_indent + context.config.tab_spaces);
|
||||||
let outer_indent = make_indent(context.block_indent);
|
let outer_indent = make_indent(context.block_indent);
|
||||||
Some(format!("{} {{\n{}{}\n{}}}", path_str, inner_indent, fields_str, outer_indent))
|
Some(format!("{} {{\n{}{}\n{}}}", path_str, inner_indent, fields_str, outer_indent))
|
||||||
@ -1020,7 +1020,7 @@ fn rewrite_struct_lit<'a>(context: &RewriteContext,
|
|||||||
_ => Some(format!("{} {{ {} }}", path_str, fields_str)),
|
_ => 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.
|
// of space, we should fall back to BlockIndent.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
25
src/items.rs
25
src/items.rs
@ -10,7 +10,7 @@
|
|||||||
|
|
||||||
// Formatting top-level items - functions, structs, enums, traits, impls.
|
// 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,
|
use utils::{format_mutability, format_visibility, make_indent, contains_skip, span_after,
|
||||||
end_typaram};
|
end_typaram};
|
||||||
use lists::{write_list, itemize_list, ListItem, ListFormatting, SeparatorTactic, ListTactic};
|
use lists::{write_list, itemize_list, ListItem, ListFormatting, SeparatorTactic, ListTactic};
|
||||||
@ -225,6 +225,10 @@ impl<'a> FmtVisitor<'a> {
|
|||||||
result.push_str("(\n");
|
result.push_str("(\n");
|
||||||
result.push_str(&make_indent(arg_indent));
|
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 {
|
} else {
|
||||||
result.push('(');
|
result.push('(');
|
||||||
}
|
}
|
||||||
@ -245,14 +249,20 @@ impl<'a> FmtVisitor<'a> {
|
|||||||
indent,
|
indent,
|
||||||
arg_indent,
|
arg_indent,
|
||||||
args_span));
|
args_span));
|
||||||
|
if self.config.fn_args_layout == StructLitStyle::Block {
|
||||||
|
result.push('\n');
|
||||||
|
}
|
||||||
result.push(')');
|
result.push(')');
|
||||||
|
|
||||||
// Return type.
|
// Return type.
|
||||||
if !ret_str.is_empty() {
|
if !ret_str.is_empty() {
|
||||||
// If we've already gone multi-line, or the return type would push
|
// 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.
|
// over the max width, then put the return type on a new line.
|
||||||
if result.contains("\n") ||
|
// Unless we are formatting args like a block, in which case there
|
||||||
result.len() + indent + ret_str.len() > self.config.max_width {
|
// 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 {
|
let indent = match self.config.fn_return_indent {
|
||||||
ReturnIndent::WithWhereClause => indent + 4,
|
ReturnIndent::WithWhereClause => indent + 4,
|
||||||
// TODO we might want to check that using the arg indent doesn't
|
// TODO we might want to check that using the arg indent doesn't
|
||||||
@ -285,8 +295,11 @@ impl<'a> FmtVisitor<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let where_density = if self.config.where_density == Density::Compressed &&
|
let where_density = if (self.config.where_density == Density::Compressed &&
|
||||||
!result.contains('\n') {
|
(!result.contains('\n') ||
|
||||||
|
self.config.fn_args_layout == StructLitStyle::Block)) ||
|
||||||
|
(self.config.fn_args_layout == StructLitStyle::Block &&
|
||||||
|
ret_str.is_empty()) {
|
||||||
Density::Compressed
|
Density::Compressed
|
||||||
} else {
|
} else {
|
||||||
Density::Tall
|
Density::Tall
|
||||||
@ -363,7 +376,7 @@ impl<'a> FmtVisitor<'a> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let fmt = ListFormatting {
|
let fmt = ListFormatting {
|
||||||
tactic: self.config.fn_args_layout.to_list_tactic(),
|
tactic: self.config.fn_args_density.to_list_tactic(),
|
||||||
separator: ",",
|
separator: ",",
|
||||||
trailing_separator: SeparatorTactic::Never,
|
trailing_separator: SeparatorTactic::Never,
|
||||||
indent: indent,
|
indent: indent,
|
||||||
|
@ -133,13 +133,13 @@ impl_enum_decodable!(ReturnIndent, WithArgs, WithWhereClause);
|
|||||||
pub enum StructLitStyle {
|
pub enum StructLitStyle {
|
||||||
// First line on the same line as the opening brace, all lines aligned with
|
// First line on the same line as the opening brace, all lines aligned with
|
||||||
// the first line.
|
// the first line.
|
||||||
VisualIndent,
|
Visual,
|
||||||
// First line is on a new line and all lines align with block indent.
|
// 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.
|
// 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 {
|
enum ErrorKind {
|
||||||
// Line has exceeded character limit
|
// Line has exceeded character limit
|
||||||
|
@ -6,7 +6,8 @@ newline_style = "Unix"
|
|||||||
fn_brace_style = "SameLineWhere"
|
fn_brace_style = "SameLineWhere"
|
||||||
fn_return_indent = "WithArgs"
|
fn_return_indent = "WithArgs"
|
||||||
fn_args_paren_newline = true
|
fn_args_paren_newline = true
|
||||||
fn_args_layout = "Tall"
|
fn_args_density = "Tall"
|
||||||
|
fn_args_layout = "Visual"
|
||||||
fn_arg_indent = "Visual"
|
fn_arg_indent = "Visual"
|
||||||
where_density = "Tall"
|
where_density = "Tall"
|
||||||
where_indent = "Tabbed"
|
where_indent = "Tabbed"
|
||||||
@ -15,7 +16,7 @@ where_pred_indent = "Visual"
|
|||||||
generics_indent = "Visual"
|
generics_indent = "Visual"
|
||||||
struct_trailing_comma = "Vertical"
|
struct_trailing_comma = "Vertical"
|
||||||
struct_lit_trailing_comma = "Vertical"
|
struct_lit_trailing_comma = "Vertical"
|
||||||
struct_lit_style = "BlockIndent"
|
struct_lit_style = "Block"
|
||||||
enum_trailing_comma = true
|
enum_trailing_comma = true
|
||||||
report_todo = "Always"
|
report_todo = "Always"
|
||||||
report_fixme = "Never"
|
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-fn_args_layout: Compressed
|
// rustfmt-fn_args_density: Compressed
|
||||||
// Test some of the ways function signatures can be customised.
|
// Test some of the ways function signatures can be customised.
|
||||||
|
|
||||||
// Test compressed layout of args.
|
// Test compressed layout of args.
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
// rustfmt-struct_lit_style: VisualIndent
|
// rustfmt-struct_lit_style: Visual
|
||||||
|
|
||||||
// Struct literal expressions.
|
// 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-fn_args_layout: Compressed
|
// rustfmt-fn_args_density: Compressed
|
||||||
// Test some of the ways function signatures can be customised.
|
// Test some of the ways function signatures can be customised.
|
||||||
|
|
||||||
// Test compressed layout of args.
|
// Test compressed layout of args.
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
// rustfmt-struct_lit_style: VisualIndent
|
// rustfmt-struct_lit_style: Visual
|
||||||
|
|
||||||
// Struct literal expressions.
|
// Struct literal expressions.
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user