diff options
| author | Marcus Klaas de Vries <mail@marcusklaas.nl> | 2015-10-22 22:33:24 +0200 |
|---|---|---|
| committer | Marcus Klaas de Vries <mail@marcusklaas.nl> | 2015-10-22 22:33:24 +0200 |
| commit | 7e76fad1f55945c196b32e6629ebaecc4ebfda74 (patch) | |
| tree | 85efc67ea1bfa81ac8a64d461206843fc706ae43 /src | |
| parent | 032e6ae8332dd3a4bc10617e53ebd71835bb713d (diff) | |
| parent | 2d267b16d1f28e7ab14cd1cc9953ae21fe1856c6 (diff) | |
Merge pull request #485 from marcusklaas/floaters2
Deal with floaters
Diffstat (limited to 'src')
| -rw-r--r-- | src/chains.rs | 45 | ||||
| -rw-r--r-- | src/config.rs | 5 |
2 files changed, 40 insertions, 10 deletions
diff --git a/src/chains.rs b/src/chains.rs index 6122aa14614..4b6824a9073 100644 --- a/src/chains.rs +++ b/src/chains.rs @@ -23,6 +23,7 @@ use Indent; use rewrite::{Rewrite, RewriteContext}; use utils::first_line_width; use expr::rewrite_call; +use config::BlockIndentStyle; use syntax::{ast, ptr}; use syntax::codemap::{mk_sp, Span}; @@ -41,17 +42,24 @@ pub fn rewrite_chain(mut expr: &ast::Expr, expr = subexpr; } + let parent_block_indent = match context.config.chain_base_indent { + BlockIndentStyle::Visual => offset, + BlockIndentStyle::Inherit => context.block_indent, + BlockIndentStyle::Tabbed => context.block_indent.block_indent(context.config), + }; + let parent_context = &RewriteContext { block_indent: parent_block_indent, ..*context }; let parent = subexpr_list.pop().unwrap(); - let parent_rewrite = try_opt!(expr.rewrite(context, width, offset)); - let (extra_indent, extend) = if !parent_rewrite.contains('\n') && is_continuable(parent) || - parent_rewrite.len() <= context.config.tab_spaces { - (Indent::new(0, parent_rewrite.len()), true) + let parent_rewrite = try_opt!(expr.rewrite(parent_context, width, offset)); + let (indent, extend) = if !parent_rewrite.contains('\n') && is_continuable(parent) || + parent_rewrite.len() <= context.config.tab_spaces { + (offset + Indent::new(0, parent_rewrite.len()), true) + } else if is_block_expr(parent, &parent_rewrite) { + (parent_block_indent, false) } else { - (Indent::new(context.config.tab_spaces, 0), false) + (offset + Indent::new(context.config.tab_spaces, 0), false) }; - let indent = offset + extra_indent; - let max_width = try_opt!(width.checked_sub(extra_indent.width())); + let max_width = try_opt!((width + offset.width()).checked_sub(indent.width())); let mut rewrites = try_opt!(subexpr_list.iter() .rev() .map(|e| { @@ -114,7 +122,7 @@ pub fn rewrite_chain(mut expr: &ast::Expr, _ => total_width <= width && rewrites.iter().all(|s| !s.contains('\n')), }; - let connector = if fits_single_line { + let connector = if fits_single_line && !parent_rewrite.contains('\n') { String::new() } else { format!("\n{}", indent.to_string(context.config)) @@ -132,6 +140,27 @@ pub fn rewrite_chain(mut expr: &ast::Expr, rewrites.join(&connector))) } +// States whether an expression's last line exclusively consists of closing +// parens, braces and brackets in its idiomatic formatting. +fn is_block_expr(expr: &ast::Expr, repr: &str) -> bool { + match expr.node { + ast::Expr_::ExprStruct(..) | + ast::Expr_::ExprWhile(..) | + ast::Expr_::ExprWhileLet(..) | + ast::Expr_::ExprIf(..) | + ast::Expr_::ExprIfLet(..) | + ast::Expr_::ExprBlock(..) | + ast::Expr_::ExprLoop(..) | + ast::Expr_::ExprForLoop(..) | + ast::Expr_::ExprMatch(..) => repr.contains('\n'), + ast::Expr_::ExprParen(ref expr) | + ast::Expr_::ExprBinary(_, _, ref expr) | + ast::Expr_::ExprIndex(_, ref expr) | + ast::Expr_::ExprUnary(_, ref expr) => is_block_expr(expr, repr), + _ => false, + } +} + fn pop_expr_chain<'a>(expr: &'a ast::Expr) -> Option<&'a ast::Expr> { match expr.node { ast::Expr_::ExprMethodCall(_, _, ref expressions) => { diff --git a/src/config.rs b/src/config.rs index 6a7e686dc11..d788b17cfe7 100644 --- a/src/config.rs +++ b/src/config.rs @@ -290,9 +290,10 @@ create_config! { "Multiline style on literal structs"; enum_trailing_comma: bool, true, "Put a trailing comma on enum declarations"; report_todo: ReportTactic, ReportTactic::Always, - "Report all occurrences of TODO in source file comments"; + "Report all, none or unnumbered occurrences of TODO in source file comments"; report_fixme: ReportTactic, ReportTactic::Never, - "Report all occurrences of FIXME in source file comments"; + "Report all, none or unnumbered occurrences of FIXME in source file comments"; + chain_base_indent: BlockIndentStyle, BlockIndentStyle::Visual, "Indent on chain base"; // Alphabetically, case sensitive. reorder_imports: bool, false, "Reorder import statements alphabetically"; single_line_if_else: bool, false, "Put else on same line as closing brace for if statements"; |
