From 82a6cca010098faf08481a7449ef0d76230ae01a Mon Sep 17 00:00:00 2001 From: Sinh Pham Date: Sat, 17 Oct 2015 11:35:47 -0700 Subject: [PATCH] Refactor write_snippet --- src/missed_spans.rs | 41 ++++++++++------------------------------- 1 file changed, 10 insertions(+), 31 deletions(-) diff --git a/src/missed_spans.rs b/src/missed_spans.rs index f33b7a3014f..57b63b56790 100644 --- a/src/missed_spans.rs +++ b/src/missed_spans.rs @@ -59,43 +59,22 @@ fn format_missing_inner(&mut self, let span = codemap::mk_sp(start, end); let snippet = self.snippet(span); - self.write_snippet(&snippet, true, &process_last_snippet); + self.write_snippet(&snippet, &process_last_snippet); } fn write_snippet(&mut self, snippet: &str, - last_snippet: bool, process_last_snippet: F) { - // Trim whitespace from the right hand side of each line. - // Annoyingly, the library functions for splitting by lines etc. are not - // quite right, so we must do it ourselves. - let mut line_start = 0; - let mut last_wspace = None; - for (i, c) in snippet.char_indices() { - if c == '\n' { - if let Some(lw) = last_wspace { - self.buffer.push_str(&snippet[line_start..lw]); - self.buffer.push_str("\n"); - } else { - self.buffer.push_str(&snippet[line_start..i + 1]); - } - - line_start = i + 1; - last_wspace = None; - } else { - if c.is_whitespace() { - if last_wspace.is_none() { - last_wspace = Some(i); - } - } else { - last_wspace = None; - } - } - } - if last_snippet { - process_last_snippet(self, &snippet[line_start..], snippet); + let mut lines: Vec<&str> = snippet.lines().collect(); + let last_snippet = if snippet.ends_with("\n") { + "" } else { - self.buffer.push_str(&snippet[line_start..]); + lines.pop().unwrap() + }; + for line in lines.iter() { + self.buffer.push_str(line.trim_right()); + self.buffer.push_str("\n"); } + process_last_snippet(self, &last_snippet, snippet); } }