]> git.lizzy.rs Git - rust.git/commitdiff
Remove unnecessary blank lines at the start and the end of a block
authorsinkuu <sinkuu@sinkuu.xyz>
Fri, 11 Aug 2017 07:15:28 +0000 (16:15 +0900)
committersinkuu <sinkuu@sinkuu.xyz>
Fri, 11 Aug 2017 08:44:16 +0000 (17:44 +0900)
src/config.rs
src/visitor.rs
tests/source/remove_blank_lines.rs [new file with mode: 0644]
tests/target/remove_blank_lines.rs [new file with mode: 0644]

index fbba5e19490fdf718632680004421107badd021e..11d64dc073fb04aeb3456a8780691bad126685f2 100644 (file)
@@ -606,7 +606,9 @@ pub fn get_toml_path(dir: &Path) -> Result<Option<PathBuf>, Error> {
                                               tuple patterns";
     combine_control_expr: bool, true, "Combine control expressions with funciton calls.";
     struct_field_align_threshold: usize, 0, "Align struct fields if their diffs fits within \
-                                             threshold."
+                                             threshold.";
+    remove_blank_lines_at_start_or_end_of_block: bool, true,
+        "Remove blank lines at start or end of a block";
 }
 
 #[cfg(test)]
index 4ef89979c3ff159a6060d5abb725bc639972497d..b455e569e435dd618f93fa35ea14e9f41958368e 100644 (file)
@@ -17,7 +17,7 @@
 
 use {Indent, Shape, Spanned};
 use codemap::{LineRangeUtils, SpanUtils};
-use comment::{contains_comment, FindUncommented};
+use comment::{contains_comment, CodeCharKind, CommentCodeSlices, FindUncommented};
 use comment::rewrite_comment;
 use config::{BraceStyle, Config};
 use expr::{format_expr, ExprType};
@@ -131,6 +131,27 @@ pub fn visit_block(&mut self, b: &ast::Block, inner_attrs: Option<&[ast::Attribu
         self.block_indent = self.block_indent.block_indent(self.config);
         self.buffer.push_str("{");
 
+        if self.config.remove_blank_lines_at_start_or_end_of_block() {
+            if let Some(stmt) = b.stmts.first() {
+                let snippet = self.snippet(mk_sp(self.last_pos, stmt.span.lo));
+                let len = CommentCodeSlices::new(&snippet)
+                    .nth(0)
+                    .and_then(|(kind, _, s)| {
+                        if kind == CodeCharKind::Normal {
+                            // There may be inner attributes
+                            let s = &s[..s.len() -
+                                           s.trim_left_matches(&[' ', '\t', '\r', '\n'][..]).len()];
+                            s.rfind('\n')
+                        } else {
+                            None
+                        }
+                    });
+                if let Some(len) = len {
+                    self.last_pos = self.last_pos + BytePos(len as u32);
+                }
+            }
+        }
+
         // Format inner attributes if available.
         if let Some(attrs) = inner_attrs {
             self.visit_attrs(attrs, ast::AttrStyle::Inner);
@@ -148,9 +169,31 @@ pub fn visit_block(&mut self, b: &ast::Block, inner_attrs: Option<&[ast::Attribu
             }
         }
 
+        let mut remove_len = BytePos(0);
+        if self.config.remove_blank_lines_at_start_or_end_of_block() {
+            if let Some(stmt) = b.stmts.last() {
+                let snippet = self.snippet(mk_sp(
+                    stmt.span.hi,
+                    source!(self, b.span).hi - brace_compensation,
+                ));
+                let len = CommentCodeSlices::new(&snippet)
+                    .last()
+                    .and_then(|(kind, _, s)| {
+                        if kind == CodeCharKind::Normal && s.trim().is_empty() {
+                            Some(s.len())
+                        } else {
+                            None
+                        }
+                    });
+                if let Some(len) = len {
+                    remove_len = BytePos(len as u32);
+                }
+            }
+        }
+
         let mut unindent_comment = self.is_if_else_block && !b.stmts.is_empty();
         if unindent_comment {
-            let end_pos = source!(self, b.span).hi - brace_compensation;
+            let end_pos = source!(self, b.span).hi - brace_compensation - remove_len;
             let snippet = self.get_context().snippet(mk_sp(self.last_pos, end_pos));
             unindent_comment = snippet.contains("//") || snippet.contains("/*");
         }
@@ -158,7 +201,7 @@ pub fn visit_block(&mut self, b: &ast::Block, inner_attrs: Option<&[ast::Attribu
         if unindent_comment {
             self.block_indent = self.block_indent.block_unindent(self.config);
         }
-        self.format_missing_with_indent(source!(self, b.span).hi - brace_compensation);
+        self.format_missing_with_indent(source!(self, b.span).hi - brace_compensation - remove_len);
         if unindent_comment {
             self.block_indent = self.block_indent.block_indent(self.config);
         }
diff --git a/tests/source/remove_blank_lines.rs b/tests/source/remove_blank_lines.rs
new file mode 100644 (file)
index 0000000..377843c
--- /dev/null
@@ -0,0 +1,22 @@
+fn main() {
+
+
+
+
+    let x = 1;
+
+
+
+
+}
+
+fn foo() {
+
+    #![attribute]
+
+    let x = 1;
+
+    // comment
+
+
+}
diff --git a/tests/target/remove_blank_lines.rs b/tests/target/remove_blank_lines.rs
new file mode 100644 (file)
index 0000000..00de4a8
--- /dev/null
@@ -0,0 +1,11 @@
+fn main() {
+    let x = 1;
+}
+
+fn foo() {
+    #![attribute]
+
+    let x = 1;
+
+    // comment
+}