Unify a bunch of option types into IndentStyle

This commit is contained in:
Nick Cameron 2017-03-20 11:42:20 +13:00
parent c986e895bb
commit 21ff1d43ba
19 changed files with 63 additions and 82 deletions

View File

@ -80,7 +80,7 @@ use Shape;
use rewrite::{Rewrite, RewriteContext};
use utils::{wrap_str, first_line_width, last_line_width};
use expr::rewrite_call;
use config::BlockIndentStyle;
use config::IndentStyle;
use macros::convert_try_mac;
use std::iter;
@ -134,8 +134,8 @@ pub fn rewrite_chain(expr: &ast::Expr, context: &RewriteContext, shape: Shape) -
let first_child_shape = if extend {
let mut shape = try_opt!(parent_shape.shrink_left(last_line_width(&parent_rewrite)));
match context.config.chain_indent {
BlockIndentStyle::Visual => other_child_shape,
BlockIndentStyle::Tabbed => {
IndentStyle::Visual => other_child_shape,
IndentStyle::Block => {
shape.offset = shape.offset.checked_sub(context.config.tab_spaces).unwrap_or(0);
shape.indent.block_indent += context.config.tab_spaces;
shape
@ -294,8 +294,8 @@ fn make_subexpr_list(expr: &ast::Expr, context: &RewriteContext) -> (ast::Expr,
fn chain_indent(context: &RewriteContext, shape: Shape) -> Shape {
match context.config.chain_indent {
BlockIndentStyle::Visual => shape.visual_indent(0),
BlockIndentStyle::Tabbed => shape.block_indent(context.config.tab_spaces),
IndentStyle::Visual => shape.visual_indent(0),
IndentStyle::Block => shape.block_indent(context.config.tab_spaces),
}
}

View File

@ -61,30 +61,12 @@ configuration_option_enum! { ReturnIndent:
WithWhereClause,
}
// How to style a struct literal.
configuration_option_enum! { StructLitStyle:
configuration_option_enum! { IndentStyle:
// First line on the same line as the opening brace, all lines aligned with
// the first line.
Visual,
// First line is on a new line and all lines align with block indent.
Block,
// 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,
}
configuration_option_enum! { BlockIndentStyle:
// One level deeper than parent.
Tabbed,
// Aligned with block open.
Visual,
}
configuration_option_enum! { Density:
@ -357,10 +339,10 @@ 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: FnArgLayoutStyle, FnArgLayoutStyle::Visual,
fn_args_layout: IndentStyle, IndentStyle::Visual,
"Layout of function arguments and tuple structs";
fn_arg_indent: BlockIndentStyle, BlockIndentStyle::Visual, "Indent on function arguments";
array_layout: FnArgLayoutStyle, FnArgLayoutStyle::Visual, "Indent on arrays";
fn_arg_indent: IndentStyle, IndentStyle::Visual, "Indent on function arguments";
array_layout: IndentStyle, IndentStyle::Visual, "Indent on arrays";
type_punctuation_density: TypeDensity, TypeDensity::Wide,
"Determines if '+' or '=' are wrapped in spaces in the punctuation of types";
where_style: Style, Style::Default, "Overall strategy for where clauses";
@ -368,20 +350,20 @@ create_config! {
// function decl?
where_density: Density, Density::CompressedIfEmpty, "Density of a where clause";
// Visual will be treated like Tabbed
where_indent: BlockIndentStyle, BlockIndentStyle::Tabbed, "Indentation of a where clause";
where_indent: IndentStyle, IndentStyle::Block, "Indentation of a where clause";
where_layout: ListTactic, ListTactic::Vertical, "Element layout inside a where clause";
where_pred_indent: BlockIndentStyle, BlockIndentStyle::Visual,
where_pred_indent: IndentStyle, IndentStyle::Visual,
"Indentation style of a where predicate";
generics_style: Style, Style::Default, "Overall strategy for generics";
generics_indent: BlockIndentStyle, BlockIndentStyle::Visual, "Indentation of generics";
struct_lit_style: StructLitStyle, StructLitStyle::Block, "Style of struct definition";
generics_indent: IndentStyle, IndentStyle::Visual, "Indentation of generics";
struct_lit_style: IndentStyle, IndentStyle::Block, "Style of struct definition";
struct_lit_multiline_style: MultilineStyle, MultilineStyle::PreferSingle,
"Multiline style on literal structs";
report_todo: ReportTactic, ReportTactic::Never,
"Report all, none or unnumbered occurrences of TODO in source file comments";
report_fixme: ReportTactic, ReportTactic::Never,
"Report all, none or unnumbered occurrences of FIXME in source file comments";
chain_indent: BlockIndentStyle, BlockIndentStyle::Tabbed, "Indentation of chain";
chain_indent: IndentStyle, IndentStyle::Block, "Indentation of chain";
chain_one_line_max: usize, 4, "Maximum number of elements in a chain to fit on a single line";
reorder_imports: bool, false, "Reorder import statements alphabetically";
reorder_imported_names: bool, false,

View File

@ -24,7 +24,7 @@ use string::{StringFormat, rewrite_string};
use utils::{extra_offset, last_line_width, wrap_str, binary_search, first_line_width,
semicolon_for_stmt, trimmed_last_line_width, left_most_sub_expr, stmt_expr};
use visitor::FmtVisitor;
use config::{Config, StructLitStyle, MultilineStyle, ControlBraceStyle, FnArgLayoutStyle};
use config::{Config, IndentStyle, MultilineStyle, ControlBraceStyle};
use comment::{FindUncommented, rewrite_comment, contains_comment, recover_comment_removed};
use types::{rewrite_path, PathContext};
use items::{span_lo_for_arg, span_hi_for_arg};
@ -331,8 +331,8 @@ pub fn rewrite_array<'a, I>(expr_iter: I,
};
let nested_shape = match context.config.array_layout {
FnArgLayoutStyle::Block => shape.block().block_indent(context.config.tab_spaces),
FnArgLayoutStyle::Visual => {
IndentStyle::Block => shape.block().block_indent(context.config.tab_spaces),
IndentStyle::Visual => {
try_opt!(shape.visual_indent(bracket_size).sub_width(bracket_size * 2))
}
};
@ -361,14 +361,14 @@ pub fn rewrite_array<'a, I>(expr_iter: I,
|acc, x| acc.and_then(|y| x.map(|x| x || y))));
let tactic = match context.config.array_layout {
FnArgLayoutStyle::Block => {
IndentStyle::Block => {
// TODO wrong shape in one-line case
match shape.width.checked_sub(2 * bracket_size) {
Some(width) => definitive_tactic(&items, ListTactic::HorizontalVertical, width),
None => DefinitiveListTactic::Vertical,
}
}
FnArgLayoutStyle::Visual => {
IndentStyle::Visual => {
if has_long_item || items.iter().any(ListItem::is_multiline) {
definitive_tactic(&items, ListTactic::HorizontalVertical, nested_shape.width)
} else {
@ -387,7 +387,7 @@ pub fn rewrite_array<'a, I>(expr_iter: I,
};
let list_str = try_opt!(write_list(&items, &fmt));
let result = if context.config.array_layout == FnArgLayoutStyle::Visual ||
let result = if context.config.array_layout == IndentStyle::Visual ||
tactic != DefinitiveListTactic::Vertical {
if context.config.spaces_within_square_brackets && list_str.len() > 0 {
format!("[ {} ]", list_str)
@ -1734,10 +1734,10 @@ fn rewrite_struct_lit<'a>(context: &RewriteContext,
// Foo { a: Foo } - indent is +3, width is -5.
let h_shape = shape.sub_width(path_str.len() + 5);
let v_shape = match context.config.struct_lit_style {
StructLitStyle::Visual => {
IndentStyle::Visual => {
try_opt!(try_opt!(shape.shrink_left(path_str.len() + 3)).sub_width(2))
}
StructLitStyle::Block => {
IndentStyle::Block => {
let shape = shape.block_indent(context.config.tab_spaces);
Shape {
width: try_opt!(context.config.max_width.checked_sub(shape.indent.width())),
@ -1784,7 +1784,7 @@ fn rewrite_struct_lit<'a>(context: &RewriteContext,
let tactic = if let Some(h_shape) = h_shape {
let mut prelim_tactic = match (context.config.struct_lit_style, fields.len()) {
(StructLitStyle::Visual, 1) => ListTactic::HorizontalVertical,
(IndentStyle::Visual, 1) => ListTactic::HorizontalVertical,
_ => context.config.struct_lit_multiline_style.to_list_tactic(),
};
@ -1802,7 +1802,7 @@ fn rewrite_struct_lit<'a>(context: &RewriteContext,
_ => v_shape,
};
let ends_with_newline = context.config.struct_lit_style != StructLitStyle::Visual &&
let ends_with_newline = context.config.struct_lit_style != IndentStyle::Visual &&
tactic == DefinitiveListTactic::Vertical;
let fmt = ListFormatting {
@ -1825,7 +1825,7 @@ fn rewrite_struct_lit<'a>(context: &RewriteContext,
}
// One liner or visual indent.
if context.config.struct_lit_style == StructLitStyle::Visual ||
if context.config.struct_lit_style == IndentStyle::Visual ||
(context.config.struct_lit_multiline_style != MultilineStyle::ForceMulti &&
!fields_str.contains('\n') &&
fields_str.len() <= h_shape.map(|s| s.width).unwrap_or(0)) {

View File

@ -21,7 +21,7 @@ use expr::{is_empty_block, is_simple_block_stmt, rewrite_assign_rhs, type_annota
use comment::{FindUncommented, contains_comment};
use visitor::FmtVisitor;
use rewrite::{Rewrite, RewriteContext};
use config::{Config, BlockIndentStyle, Density, ReturnIndent, BraceStyle, FnArgLayoutStyle, Style};
use config::{Config, IndentStyle, Density, ReturnIndent, BraceStyle, Style};
use itertools::Itertools;
use syntax::{ast, abi, codemap, ptr, symbol};
@ -779,9 +779,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 == FnArgLayoutStyle::Block)) ||
(context.config.fn_args_layout == FnArgLayoutStyle::Block && result.is_empty()) ||
(!result.contains('\n') || context.config.fn_args_layout == IndentStyle::Block)) ||
(context.config.fn_args_layout == IndentStyle::Block && result.is_empty()) ||
(context.config.where_density == Density::CompressedIfEmpty && !has_body &&
!result.contains('\n')) {
Density::Compressed
@ -1020,11 +1019,11 @@ fn format_tuple_struct(context: &RewriteContext,
};
let (tactic, item_indent) = match context.config.fn_args_layout {
FnArgLayoutStyle::Visual => {
IndentStyle::Visual => {
// 1 = `(`
(ListTactic::HorizontalVertical, offset.block_only() + result.len() + 1)
}
FnArgLayoutStyle::Block => {
IndentStyle::Block => {
(ListTactic::HorizontalVertical, offset.block_only().block_indent(&context.config))
}
};
@ -1056,7 +1055,7 @@ fn format_tuple_struct(context: &RewriteContext,
context.config,
tactic));
if context.config.fn_args_layout == FnArgLayoutStyle::Visual || !body.contains('\n') {
if context.config.fn_args_layout == IndentStyle::Visual || !body.contains('\n') {
result.push('(');
if context.config.spaces_within_parens && body.len() > 0 {
result.push(' ');
@ -1512,7 +1511,7 @@ fn rewrite_fn_base(context: &RewriteContext,
let (mut one_line_budget, mut multi_line_budget, mut arg_indent) =
try_opt!(compute_budgets_for_args(context, &result, indent, ret_str_len, newline_brace));
if context.config.fn_args_layout == FnArgLayoutStyle::Block {
if context.config.fn_args_layout == IndentStyle::Block {
arg_indent = indent.block_indent(context.config);
multi_line_budget = context.config.max_width - arg_indent.width();
}
@ -1566,7 +1565,7 @@ fn rewrite_fn_base(context: &RewriteContext,
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 || generics_str.contains('\n'),
IndentStyle::Block => multi_line_arg_str || generics_str.contains('\n'),
_ => false,
} && !fd.inputs.is_empty();
@ -1590,7 +1589,7 @@ fn rewrite_fn_base(context: &RewriteContext,
if !ret_str.is_empty() {
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,
IndentStyle::Block if put_args_in_block => 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. With the +1 for the signature
@ -1802,8 +1801,8 @@ fn rewrite_args(context: &RewriteContext,
}
let indent = match context.config.fn_arg_indent {
BlockIndentStyle::Tabbed => indent.block_indent(context.config),
BlockIndentStyle::Visual => arg_indent,
IndentStyle::Block => indent.block_indent(context.config),
IndentStyle::Visual => arg_indent,
};
let tactic = definitive_tactic(&arg_items,
@ -1817,7 +1816,7 @@ fn rewrite_args(context: &RewriteContext,
debug!("rewrite_args: budget: {}, tactic: {:?}", budget, tactic);
let (trailing_comma, end_with_newline) = match context.config.fn_args_layout {
FnArgLayoutStyle::Block => (SeparatorTactic::Vertical, true),
IndentStyle::Block => (SeparatorTactic::Vertical, true),
_ => (SeparatorTactic::Never, false),
};
@ -1909,9 +1908,9 @@ fn rewrite_generics(context: &RewriteContext,
}
let offset = match context.config.generics_indent {
BlockIndentStyle::Tabbed => shape.indent.block_indent(context.config),
IndentStyle::Block => shape.indent.block_indent(context.config),
// 1 = <
BlockIndentStyle::Visual => generics_offset + 1,
IndentStyle::Visual => generics_offset + 1,
};
let h_budget = try_opt!(shape.width.checked_sub(generics_offset.width() + 2));
@ -1945,7 +1944,7 @@ fn rewrite_generics(context: &RewriteContext,
let list_str =
try_opt!(format_item_list(items, Shape::legacy(h_budget, offset), context.config));
let result = if context.config.generics_indent != BlockIndentStyle::Visual &&
let result = if context.config.generics_indent != IndentStyle::Visual &&
list_str.contains('\n') {
format!("<\n{}{}\n{}>",
offset.to_string(context.config),
@ -2074,9 +2073,9 @@ fn rewrite_where_clause(context: &RewriteContext,
let extra_indent = Indent::new(context.config.tab_spaces, 0);
let offset = match context.config.where_pred_indent {
BlockIndentStyle::Tabbed => shape.indent + extra_indent.block_indent(context.config),
IndentStyle::Block => shape.indent + extra_indent.block_indent(context.config),
// 6 = "where ".len()
BlockIndentStyle::Visual => shape.indent + extra_indent + 6,
IndentStyle::Visual => shape.indent + extra_indent + 6,
};
// FIXME: if where_pred_indent != Visual, then the budgets below might
// be out by a char or two.

View File

@ -9,7 +9,7 @@ fn_args_density = "Tall"
fn_args_layout = "Visual"
fn_arg_indent = "Visual"
where_density = "Tall"
where_indent = "Tabbed"
where_indent = "Block"
where_layout = "Vertical"
where_pred_indent = "Visual"
generics_indent = "Visual"

View File

@ -1,4 +1,4 @@
// rustfmt-chain_indent: Tabbed
// rustfmt-chain_indent: Block
fn test() {
let x = my_long_function().my_even_longer_function().my_nested_function().some_random_name().another_function().do_it();

View File

@ -1,6 +1,6 @@
// rustfmt-fn_arg_indent: Tabbed
// rustfmt-generics_indent: Tabbed
// rustfmt-where_indent: Tabbed
// rustfmt-fn_arg_indent: Block
// rustfmt-generics_indent: Block
// rustfmt-where_indent: Block
// rustfmt-where_layout: Mixed
// Test different indents.

View File

@ -1,5 +1,5 @@
// rustfmt-fn_arg_indent: Tabbed
// rustfmt-generics_indent: Tabbed
// rustfmt-fn_arg_indent: Block
// rustfmt-generics_indent: Block
// rustfmt-where_layout: HorizontalVertical
// Test different indents.

View File

@ -1,4 +1,4 @@
// rustfmt-where_pred_indent: Tabbed
// rustfmt-where_pred_indent: Block
// rustfmt-where_density: Compressed
// Test different indents.

View File

@ -1,5 +1,5 @@
// rustfmt-fn_args_layout: Block
// rustfmt-where_indent: Tabbed
// rustfmt-where_indent: Block
// rustfmt-fn_brace_style: PreferSameLine
// Test different indents.

View File

@ -1,7 +1,7 @@
// rustfmt-normalize_comments: true
// rustfmt-fn_args_layout: Block
// rustfmt-fn_args_density: Vertical
// rustfmt-fn_arg_indent: Tabbed
// rustfmt-fn_arg_indent: Block
// rustfmt-fn_brace_style: AlwaysNextLine
// Case with only one variable.

View File

@ -1,5 +1,5 @@
// rustfmt-fn_args_layout: Block
// rustfmt-where_indent: Tabbed
// rustfmt-where_indent: Block
// rustfmt-fn_brace_style: PreferSameLine
// Test different indents.

View File

@ -1,4 +1,4 @@
// rustfmt-chain_indent: Tabbed
// rustfmt-chain_indent: Block
fn test() {
let x = my_long_function()

View File

@ -1,6 +1,6 @@
// rustfmt-fn_arg_indent: Tabbed
// rustfmt-generics_indent: Tabbed
// rustfmt-where_indent: Tabbed
// rustfmt-fn_arg_indent: Block
// rustfmt-generics_indent: Block
// rustfmt-where_indent: Block
// rustfmt-where_layout: Mixed
// Test different indents.

View File

@ -1,5 +1,5 @@
// rustfmt-fn_arg_indent: Tabbed
// rustfmt-generics_indent: Tabbed
// rustfmt-fn_arg_indent: Block
// rustfmt-generics_indent: Block
// rustfmt-where_layout: HorizontalVertical
// Test different indents.

View File

@ -1,4 +1,4 @@
// rustfmt-where_pred_indent: Tabbed
// rustfmt-where_pred_indent: Block
// rustfmt-where_density: Compressed
// Test different indents.

View File

@ -1,5 +1,5 @@
// rustfmt-fn_args_layout: Block
// rustfmt-where_indent: Tabbed
// rustfmt-where_indent: Block
// rustfmt-fn_brace_style: PreferSameLine
// Test different indents.

View File

@ -1,7 +1,7 @@
// rustfmt-normalize_comments: true
// rustfmt-fn_args_layout: Block
// rustfmt-fn_args_density: Vertical
// rustfmt-fn_arg_indent: Tabbed
// rustfmt-fn_arg_indent: Block
// rustfmt-fn_brace_style: AlwaysNextLine
// Case with only one variable.

View File

@ -1,5 +1,5 @@
// rustfmt-fn_args_layout: Block
// rustfmt-where_indent: Tabbed
// rustfmt-where_indent: Block
// rustfmt-fn_brace_style: PreferSameLine
// Test different indents.