]> git.lizzy.rs Git - rust.git/commitdiff
Keep blank lines around comments with range
authorSeiichi Uchida <seuchida@gmail.com>
Tue, 5 Dec 2017 07:39:45 +0000 (16:39 +0900)
committerSeiichi Uchida <seuchida@gmail.com>
Tue, 5 Dec 2017 07:39:45 +0000 (16:39 +0900)
src/missed_spans.rs

index e71e08a00b30ab9ab37cc809388c1c680364b774..b25f15b48805512c9ef76e64bb4486883deb2434 100644 (file)
@@ -250,54 +250,64 @@ fn replace_chars<'a>(string: &'a str) -> Cow<'a, str> {
                 }
             }
 
-            // cur_line += newline_count;
-            // rewirte_next_comment = true;
-
-            for (mut i, c) in subslice.char_indices() {
-                i += offset;
-
-                if c == '\n' {
-                    if !self.config
-                        .file_lines()
-                        .contains_line(file_name, status.cur_line)
-                    {
+            let newline_count = count_newlines(&subslice);
+            if subslice.trim().is_empty() && newline_count > 0
+                && self.config.file_lines().intersects_range(
+                    file_name,
+                    status.cur_line,
+                    status.cur_line + newline_count,
+                ) {
+                self.push_vertical_spaces(newline_count);
+                status.cur_line += newline_count;
+                status.rewrite_next_comment = true;
+                status.line_start = offset + newline_count;
+            } else {
+                for (mut i, c) in subslice.char_indices() {
+                    i += offset;
+
+                    if c == '\n' {
+                        if !self.config
+                            .file_lines()
+                            .contains_line(file_name, status.cur_line)
+                        {
+                            status.last_wspace = None;
+                        }
+
+                        if let Some(lw) = status.last_wspace {
+                            self.buffer.push_str(&snippet[status.line_start..lw]);
+                            self.buffer.push_str("\n");
+                        } else {
+                            self.buffer.push_str(&snippet[status.line_start..i + 1]);
+                        }
+
+                        status.cur_line += 1;
+                        status.line_start = i + 1;
+                        status.last_wspace = None;
+                        status.rewrite_next_comment = true;
+                    } else if c.is_whitespace() {
+                        if status.last_wspace.is_none() {
+                            status.last_wspace = Some(i);
+                        }
+                    } else if c == ';' {
+                        if status.last_wspace.is_some() {
+                            status.line_start = i;
+                        }
+
+                        status.rewrite_next_comment = true;
                         status.last_wspace = None;
-                    }
-
-                    if let Some(lw) = status.last_wspace {
-                        self.buffer.push_str(&snippet[status.line_start..lw]);
-                        self.buffer.push_str("\n");
                     } else {
-                        self.buffer.push_str(&snippet[status.line_start..i + 1]);
-                    }
-
-                    status.cur_line += 1;
-                    status.line_start = i + 1;
-                    status.last_wspace = None;
-                    status.rewrite_next_comment = true;
-                } else if c.is_whitespace() {
-                    if status.last_wspace.is_none() {
-                        status.last_wspace = Some(i);
-                    }
-                } else if c == ';' {
-                    if status.last_wspace.is_some() {
-                        status.line_start = i;
+                        status.rewrite_next_comment = true;
+                        status.last_wspace = None;
                     }
+                }
 
+                let remaining = snippet[status.line_start..subslice.len() + offset].trim();
+                if !remaining.is_empty() {
+                    self.buffer.push_str(remaining);
+                    status.line_start = subslice.len() + offset;
                     status.rewrite_next_comment = true;
-                    status.last_wspace = None;
-                } else {
-                    status.rewrite_next_comment = true;
-                    status.last_wspace = None;
                 }
             }
-
-            let remaining = snippet[status.line_start..subslice.len() + offset].trim();
-            if !remaining.is_empty() {
-                self.buffer.push_str(remaining);
-                status.line_start = subslice.len() + offset;
-                status.rewrite_next_comment = true;
-            }
         }
 
         process_last_snippet(self, &snippet[status.line_start..], snippet);