From 840af4f84d32934d210f254a8d710aae972948b1 Mon Sep 17 00:00:00 2001 From: Nick Cameron Date: Mon, 28 Sep 2015 08:46:05 +1300 Subject: [PATCH] Option for putting the where clause on the same line as the function if the body is empty. --- src/config.rs | 6 ++++-- src/items.rs | 12 +++++++++--- tests/target/trait.rs | 3 +-- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/config.rs b/src/config.rs index b21fe3d2a07..7460f581205 100644 --- a/src/config.rs +++ b/src/config.rs @@ -74,6 +74,8 @@ pub enum Density { Compressed, // Use more lines. Tall, + // Try to compress if the body is empty. + CompressedIfEmpty, } impl_enum_decodable!(Density, Compressed, Tall); @@ -82,7 +84,7 @@ impl Density { pub fn to_list_tactic(self) -> ListTactic { match self { Density::Compressed => ListTactic::Mixed, - Density::Tall => ListTactic::HorizontalVertical, + Density::Tall | Density::CompressedIfEmpty => ListTactic::HorizontalVertical, } } } @@ -267,7 +269,7 @@ impl Default for Config { fn_args_density: Density::Tall, fn_args_layout: StructLitStyle::Visual, fn_arg_indent: BlockIndentStyle::Visual, - where_density: Density::Tall, + where_density: Density::CompressedIfEmpty, where_indent: BlockIndentStyle::Tabbed, where_layout: ListTactic::Vertical, where_pred_indent: BlockIndentStyle::Visual, diff --git a/src/items.rs b/src/items.rs index 754d98793b9..e6a334d9ad3 100644 --- a/src/items.rs +++ b/src/items.rs @@ -144,6 +144,7 @@ impl<'a> FmtVisitor<'a> { abi::Abi::Rust, ast::Visibility::Inherited, span, + false, false); match rewrite { @@ -210,7 +211,8 @@ impl<'a> FmtVisitor<'a> { abi, vis, span, - newline_brace)); + newline_brace, + true)); if self.config.fn_brace_style != BraceStyle::AlwaysNextLine && !result.contains('\n') { newline_brace = false; @@ -250,6 +252,7 @@ impl<'a> FmtVisitor<'a> { sig.abi, ast::Visibility::Inherited, span, + false, false)); // Re-attach semicolon @@ -269,7 +272,8 @@ impl<'a> FmtVisitor<'a> { abi: abi::Abi, vis: ast::Visibility, span: Span, - newline_brace: bool) + newline_brace: bool, + has_body: bool) -> Option { // FIXME we'll lose any comments in between parts of the function decl, but anyone // who comments there probably deserves what they get. @@ -407,7 +411,9 @@ impl<'a> FmtVisitor<'a> { (!result.contains('\n') || self.config.fn_args_layout == StructLitStyle::Block)) || (self.config.fn_args_layout == StructLitStyle::Block && - ret_str.is_empty()) { + ret_str.is_empty()) || + (self.config.where_density == Density::CompressedIfEmpty && + !has_body) { Density::Compressed } else { Density::Tall diff --git a/tests/target/trait.rs b/tests/target/trait.rs index 5b69c46b18d..3e0a7ed692a 100644 --- a/tests/target/trait.rs +++ b/tests/target/trait.rs @@ -15,8 +15,7 @@ trait Foo { fn increment(&mut self, x: i32); - fn read(&mut self, x: BufReader /* Used to be MemReader */) - where R: Read; + fn read(&mut self, x: BufReader /* Used to be MemReader */) where R: Read; } pub trait WriteMessage {