Refactor missing spans and fix bug with trailing whitespace

This commit is contained in:
Nick Cameron 2015-04-21 20:40:36 +12:00
parent 6e082f7aad
commit 8ca3dc063e
3 changed files with 51 additions and 43 deletions

View file

@ -224,7 +224,7 @@ impl<'a> FmtVisitor<'a> {
let fmt = ListFormatting {
tactic: ListTactic::Vertical,
separator: ",",
trailing_separator: SeparatorTactic::Always,
trailing_separator: SeparatorTactic::Never,
indent: indent + 10,
h_width: budget,
v_width: budget,

View file

@ -9,32 +9,30 @@
// except according to those terms.
use {FmtVisitor, make_indent};
use syntax::codemap::{self, Span, BytePos};
use syntax::codemap::{self, BytePos};
impl<'a> FmtVisitor<'a> {
// TODO these format_missing methods are ugly. Refactor and add unit tests
// for the central whitespace stripping loop.
pub fn format_missing(&mut self, end: BytePos) {
self.format_missing_inner(end, |this, last_snippet, span, _| {
this.changes.push_str_span(span, last_snippet)
self.format_missing_inner(end, |this, last_snippet, file_name, _| {
this.changes.push_str(file_name, last_snippet)
})
}
pub fn format_missing_with_indent(&mut self, end: BytePos) {
self.format_missing_inner(end, |this, last_snippet, span, snippet| {
self.format_missing_inner(end, |this, last_snippet, file_name, snippet| {
this.changes.push_str(file_name, last_snippet.trim_right());
if last_snippet == snippet {
// No new lines
this.changes.push_str_span(span, last_snippet);
this.changes.push_str_span(span, "\n");
} else {
this.changes.push_str_span(span, last_snippet.trim_right());
// No new lines in the snippet.
this.changes.push_str(file_name, "\n");
}
let indent = make_indent(this.block_indent);
this.changes.push_str_span(span, &indent);
this.changes.push_str(file_name, &indent);
})
}
fn format_missing_inner<F: Fn(&mut FmtVisitor, &str, Span, &str)>(&mut self,
fn format_missing_inner<F: Fn(&mut FmtVisitor, &str, &str, &str)>(&mut self,
end: BytePos,
process_last_snippet: F)
{
@ -52,44 +50,55 @@ impl<'a> FmtVisitor<'a> {
self.codemap.lookup_char_pos(start),
self.codemap.lookup_char_pos(end));
self.last_pos = end;
let spans = self.changes.filespans_for_span(start, end);
for (i, &(start, end)) in spans.iter().enumerate() {
let span = codemap::mk_sp(BytePos(start), BytePos(end));
let file_name = &self.codemap.span_to_filename(span);
let snippet = self.snippet(span);
// 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.changes.push_str_span(span, &snippet[line_start..lw]);
self.changes.push_str_span(span, "\n");
} else {
self.changes.push_str_span(span, &snippet[line_start..i+1]);
}
self.write_snippet(&snippet,
file_name,
i == spans.len() - 1,
&process_last_snippet);
}
}
line_start = i + 1;
last_wspace = None;
fn write_snippet<F: Fn(&mut FmtVisitor, &str, &str, &str)>(&mut self,
snippet: &str,
file_name: &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.changes.push_str(file_name, &snippet[line_start..lw]);
self.changes.push_str(file_name, "\n");
} else {
if c.is_whitespace() {
if last_wspace.is_none() {
last_wspace = Some(i);
}
} else {
last_wspace = None;
self.changes.push_str(file_name, &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 i == spans.len() - 1 {
process_last_snippet(self, &snippet[line_start..], span, &snippet);
} else {
self.changes.push_str_span(span, &snippet[line_start..]);
}
}
if last_snippet {
process_last_snippet(self, &snippet[line_start..], file_name, snippet);
} else {
self.changes.push_str(file_name, &snippet[line_start..]);
}
}
}

View file

@ -24,7 +24,6 @@
// dead spans
//
// Smoke testing till we can use it
// end of multi-line string has wspace
// no newline at the end of doc.rs
#[macro_use]
@ -192,7 +191,6 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
self.block_indent -= TAB_SPACES;
// TODO we should compress any newlines here to just one
// TODO somewhere here we are preserving bogus whitespace
self.format_missing_with_indent(b.span.hi - BytePos(1));
self.changes.push_str_span(b.span, "}");
self.last_pos = b.span.hi;
@ -557,7 +555,9 @@ impl<'a> FmtVisitor<'a> {
ast::Expr_::ExprLit(ref l) => {
match l.node {
ast::Lit_::LitStr(ref is, _) => {
return self.rewrite_string_lit(&is, l.span, width, offset);
let result = self.rewrite_string_lit(&is, l.span, width, offset);
debug!("string lit: `{}`", result);
return result;
}
_ => {}
}
@ -569,7 +569,6 @@ impl<'a> FmtVisitor<'a> {
}
let result = self.snippet(expr.span);
debug!("snippet: {}", result);
result
}
}