about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorKevin Yeh <kevinyeah@utexas.edu>2015-11-23 22:54:44 -0600
committerKevin Yeh <kevinyeah@utexas.edu>2015-11-24 12:55:32 -0600
commitb6dc8a3f764e2fec67268c5611ca15fc2c005380 (patch)
treed8bcd5e02fe70bfb4b24de3367d946e06088641d /src
parent7483c2a8f90952bc702fa281d7eca55ced6021e4 (diff)
Keep comments in empty impl bodies
Diffstat (limited to 'src')
-rw-r--r--src/items.rs24
1 files changed, 14 insertions, 10 deletions
diff --git a/src/items.rs b/src/items.rs
index 6ca82721689..51f064d92a1 100644
--- a/src/items.rs
+++ b/src/items.rs
@@ -16,7 +16,7 @@ use utils::{format_mutability, format_visibility, contains_skip, span_after, end
 use lists::{write_list, itemize_list, ListItem, ListFormatting, SeparatorTactic,
             DefinitiveListTactic, definitive_tactic, format_item_list};
 use expr::{is_empty_block, is_simple_block_stmt, rewrite_assign_rhs};
-use comment::FindUncommented;
+use comment::{FindUncommented, contains_comment};
 use visitor::FmtVisitor;
 use rewrite::{Rewrite, RewriteContext};
 use config::{Config, BlockIndentStyle, Density, ReturnIndent, BraceStyle, StructLitStyle};
@@ -505,27 +505,31 @@ pub fn format_impl(context: &RewriteContext, item: &ast::Item, offset: Indent) -
         }
         result.push('{');
 
-        if !items.is_empty() {
+        let snippet = context.snippet(item.span);
+        let open_pos = try_opt!(snippet.find_uncommented("{")) + 1;
+
+        if !items.is_empty() || contains_comment(&snippet[open_pos..]) {
             let mut visitor = FmtVisitor::from_codemap(context.parse_session, context.config, None);
             visitor.block_indent = context.block_indent.block_indent(context.config);
-
-            let snippet = context.snippet(item.span);
-            let open_pos = try_opt!(snippet.find_uncommented("{")) + 1;
             visitor.last_pos = item.span.lo + BytePos(open_pos as u32);
 
             for item in items {
                 visitor.visit_impl_item(&item);
             }
 
+            visitor.format_missing(item.span.hi - BytePos(1));
+
+            let inner_indent_str = visitor.block_indent.to_string(context.config);
+            let outer_indent_str = context.block_indent.to_string(context.config);
+
             result.push('\n');
-            result.push_str(trim_newlines(&visitor.buffer.to_string()));
+            result.push_str(&inner_indent_str);
+            result.push_str(&trim_newlines(&visitor.buffer.to_string().trim()));
             result.push('\n');
-
-            let indent_str = context.block_indent.to_string(context.config);
-            result.push_str(&indent_str);
+            result.push_str(&outer_indent_str);
         }
-        result.push('}');
 
+        result.push('}');
         Some(result)
     } else {
         unreachable!();