Skip to content

Commit 36c9a3a

Browse files
committed
Merge pull request #482 from sinhpham/refactor_write_snippet
Refactor write_snippet
2 parents 00aa232 + 82a6cca commit 36c9a3a

File tree

1 file changed

+10
-31
lines changed

1 file changed

+10
-31
lines changed

src/missed_spans.rs

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -59,43 +59,22 @@ impl<'a> FmtVisitor<'a> {
5959
let span = codemap::mk_sp(start, end);
6060
let snippet = self.snippet(span);
6161

62-
self.write_snippet(&snippet, true, &process_last_snippet);
62+
self.write_snippet(&snippet, &process_last_snippet);
6363
}
6464

6565
fn write_snippet<F: Fn(&mut FmtVisitor, &str, &str)>(&mut self,
6666
snippet: &str,
67-
last_snippet: bool,
6867
process_last_snippet: F) {
69-
// Trim whitespace from the right hand side of each line.
70-
// Annoyingly, the library functions for splitting by lines etc. are not
71-
// quite right, so we must do it ourselves.
72-
let mut line_start = 0;
73-
let mut last_wspace = None;
74-
for (i, c) in snippet.char_indices() {
75-
if c == '\n' {
76-
if let Some(lw) = last_wspace {
77-
self.buffer.push_str(&snippet[line_start..lw]);
78-
self.buffer.push_str("\n");
79-
} else {
80-
self.buffer.push_str(&snippet[line_start..i + 1]);
81-
}
82-
83-
line_start = i + 1;
84-
last_wspace = None;
85-
} else {
86-
if c.is_whitespace() {
87-
if last_wspace.is_none() {
88-
last_wspace = Some(i);
89-
}
90-
} else {
91-
last_wspace = None;
92-
}
93-
}
94-
}
95-
if last_snippet {
96-
process_last_snippet(self, &snippet[line_start..], snippet);
68+
let mut lines: Vec<&str> = snippet.lines().collect();
69+
let last_snippet = if snippet.ends_with("\n") {
70+
""
9771
} else {
98-
self.buffer.push_str(&snippet[line_start..]);
72+
lines.pop().unwrap()
73+
};
74+
for line in lines.iter() {
75+
self.buffer.push_str(line.trim_right());
76+
self.buffer.push_str("\n");
9977
}
78+
process_last_snippet(self, &last_snippet, snippet);
10079
}
10180
}

0 commit comments

Comments
 (0)