]> git.lizzy.rs Git - rust.git/commitdiff
Modify the placement of the opening brace of trait
authorSeiichi Uchida <seuchida@gmail.com>
Sat, 10 Mar 2018 05:55:30 +0000 (14:55 +0900)
committertopecongiro <seuchida@gmail.com>
Sun, 11 Mar 2018 22:54:12 +0000 (07:54 +0900)
Put the opening brace on the next line if

1. putting it one the current line exceeds max width.
2. trait bounds uses multiple lines.

src/items.rs

index 2484756fc56aff1b61bd134bc92fdb0bd22051bf..1d02ba653ce62092187423569b3c3e94049d4996 100644 (file)
@@ -934,45 +934,45 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent)
             )?;
         }
 
-        let where_density =
-            if context.config.indent_style() == IndentStyle::Block && result.is_empty() {
+        // Rewrite where clause.
+        if !generics.where_clause.predicates.is_empty() {
+            let where_density = if context.config.indent_style() == IndentStyle::Block {
                 Density::Compressed
             } else {
                 Density::Tall
             };
 
-        let where_budget = context.budget(last_line_width(&result));
-        let pos_before_where = if type_param_bounds.is_empty() {
-            generics.where_clause.span.lo()
+            let where_budget = context.budget(last_line_width(&result));
+            let pos_before_where = if type_param_bounds.is_empty() {
+                generics.where_clause.span.lo()
+            } else {
+                type_param_bounds[type_param_bounds.len() - 1].span().hi()
+            };
+            let option = WhereClauseOption::snuggled(&generics_str);
+            let where_clause_str = rewrite_where_clause(
+                context,
+                &generics.where_clause,
+                context.config.brace_style(),
+                Shape::legacy(where_budget, offset.block_only()),
+                where_density,
+                "{",
+                None,
+                pos_before_where,
+                option,
+                false,
+            )?;
+            // If the where clause cannot fit on the same line,
+            // put the where clause on a new line
+            if !where_clause_str.contains('\n')
+                && last_line_width(&result) + where_clause_str.len() + offset.width()
+                    > context.config.comment_width()
+            {
+                let width = offset.block_indent + context.config.tab_spaces() - 1;
+                let where_indent = Indent::new(0, width);
+                result.push_str(&where_indent.to_string_with_newline(context.config));
+            }
+            result.push_str(&where_clause_str);
         } else {
-            type_param_bounds[type_param_bounds.len() - 1].span().hi()
-        };
-        let option = WhereClauseOption::snuggled(&generics_str);
-        let where_clause_str = rewrite_where_clause(
-            context,
-            &generics.where_clause,
-            context.config.brace_style(),
-            Shape::legacy(where_budget, offset.block_only()),
-            where_density,
-            "{",
-            None,
-            pos_before_where,
-            option,
-            false,
-        )?;
-        // If the where clause cannot fit on the same line,
-        // put the where clause on a new line
-        if !where_clause_str.contains('\n')
-            && last_line_width(&result) + where_clause_str.len() + offset.width()
-                > context.config.comment_width()
-        {
-            let width = offset.block_indent + context.config.tab_spaces() - 1;
-            let where_indent = Indent::new(0, width);
-            result.push_str(&where_indent.to_string_with_newline(context.config));
-        }
-        result.push_str(&where_clause_str);
-
-        if generics.where_clause.predicates.is_empty() {
             let item_snippet = context.snippet(item.span);
             if let Some(lo) = item_snippet.chars().position(|c| c == '/') {
                 // 1 = `{`
@@ -995,7 +995,9 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent)
         }
 
         match context.config.brace_style() {
-            _ if last_line_contains_single_line_comment(&result) => {
+            _ if last_line_contains_single_line_comment(&result)
+                || last_line_width(&result) + 2 > context.budget(offset.width()) =>
+            {
                 result.push_str(&offset.to_string_with_newline(context.config));
             }
             BraceStyle::AlwaysNextLine => {
@@ -1003,8 +1005,8 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent)
             }
             BraceStyle::PreferSameLine => result.push(' '),
             BraceStyle::SameLineWhere => {
-                if !where_clause_str.is_empty()
-                    && (!trait_items.is_empty() || result.contains('\n'))
+                if result.contains('\n')
+                    || (!generics.where_clause.predicates.is_empty() && !trait_items.is_empty())
                 {
                     result.push_str(&offset.to_string_with_newline(context.config));
                 } else {