diff --git a/src/config.rs b/src/config.rs index 7e1172e1b9a..3e251ad1e39 100644 --- a/src/config.rs +++ b/src/config.rs @@ -270,6 +270,7 @@ create_config! { newline_style: NewlineStyle, NewlineStyle::Unix, "Unix or Windows line endings"; fn_brace_style: BraceStyle, BraceStyle::SameLineWhere, "Brace style for functions"; item_brace_style: BraceStyle, BraceStyle::SameLineWhere, "Brace style for structs and enums"; + impl_empty_single_line: bool, true, "Put empty-body implementations on a single line"; fn_empty_single_line: bool, true, "Put empty-body functions on a single line"; fn_single_line: bool, false, "Put single-expression functions on a single line"; fn_return_indent: ReturnIndent, ReturnIndent::WithArgs, diff --git a/src/items.rs b/src/items.rs index d81ff776126..85fcb2bf7b1 100644 --- a/src/items.rs +++ b/src/items.rs @@ -26,6 +26,8 @@ use syntax::codemap; use syntax::{ast, abi}; use syntax::codemap::{Span, BytePos, mk_sp}; use syntax::parse::token; +use syntax::ast::ImplItem; +use syntax::ptr::P; // Statements of the form // let pat: ty = init; @@ -490,8 +492,18 @@ pub fn format_impl(context: &RewriteContext, item: &ast::Item, offset: Indent) - context.config.where_density, "{", None)); - if !where_clause_str.contains('\n') && - result.len() + where_clause_str.len() + offset.width() > context.config.max_width { + + if try_opt!(is_impl_single_line(context, &items, &result, &where_clause_str, &item)) { + result.push_str(&where_clause_str); + if where_clause_str.contains('\n') { + result.push_str("\n{\n}"); + } else { + result.push_str(" {}"); + } + return Some(result); + } + + if !where_clause_str.is_empty() && !where_clause_str.contains('\n') { result.push('\n'); let width = context.block_indent.width() + context.config.tab_spaces - 1; let where_indent = Indent::new(0, width); @@ -510,6 +522,7 @@ pub fn format_impl(context: &RewriteContext, item: &ast::Item, offset: Indent) - } } } + result.push('{'); let snippet = context.snippet(item.span); @@ -536,13 +549,31 @@ pub fn format_impl(context: &RewriteContext, item: &ast::Item, offset: Indent) - result.push_str(&outer_indent_str); } + if result.chars().last().unwrap() == '{' { + result.push('\n'); + } result.push('}'); + Some(result) } else { unreachable!(); } } +fn is_impl_single_line(context: &RewriteContext, + items: &Vec
>,
+ result: &str,
+ where_clause_str: &str,
+ item: &ast::Item)
+ -> Option