2015-04-20 23:47:15 -05:00
|
|
|
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2015-04-21 04:01:19 -05:00
|
|
|
use utils::make_indent;
|
|
|
|
use visitor::FmtVisitor;
|
|
|
|
|
2015-04-21 03:40:36 -05:00
|
|
|
use syntax::codemap::{self, BytePos};
|
2015-04-20 23:47:15 -05:00
|
|
|
|
|
|
|
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) {
|
2015-07-26 07:05:43 -05:00
|
|
|
self.format_missing_inner(end, |this, last_snippet, _| {
|
|
|
|
this.buffer.push_str(last_snippet)
|
2015-04-20 23:47:15 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn format_missing_with_indent(&mut self, end: BytePos) {
|
2015-07-26 07:05:43 -05:00
|
|
|
self.format_missing_inner(end, |this, last_snippet, snippet| {
|
|
|
|
this.buffer.push_str(last_snippet.trim_right());
|
2015-04-20 23:47:15 -05:00
|
|
|
if last_snippet == snippet {
|
2015-04-21 03:40:36 -05:00
|
|
|
// No new lines in the snippet.
|
2015-07-26 07:05:43 -05:00
|
|
|
this.buffer.push_str("\n");
|
2015-04-20 23:47:15 -05:00
|
|
|
}
|
|
|
|
let indent = make_indent(this.block_indent);
|
2015-07-26 07:05:43 -05:00
|
|
|
this.buffer.push_str(&indent);
|
2015-04-20 23:47:15 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-07-26 07:05:43 -05:00
|
|
|
fn format_missing_inner<F: Fn(&mut FmtVisitor, &str, &str)>(&mut self,
|
|
|
|
end: BytePos,
|
|
|
|
process_last_snippet: F) {
|
2015-04-20 23:47:15 -05:00
|
|
|
let start = self.last_pos;
|
|
|
|
debug!("format_missing_inner: {:?} to {:?}",
|
|
|
|
self.codemap.lookup_char_pos(start),
|
|
|
|
self.codemap.lookup_char_pos(end));
|
|
|
|
|
|
|
|
if start == end {
|
2015-08-22 17:08:11 -05:00
|
|
|
// Do nothing if this is the beginning of the file.
|
2015-08-24 15:01:01 -05:00
|
|
|
if start == self.codemap.lookup_char_pos(start).file.start_pos {
|
2015-08-22 17:08:11 -05:00
|
|
|
return;
|
|
|
|
}
|
2015-07-26 07:05:43 -05:00
|
|
|
process_last_snippet(self, "", "");
|
2015-04-20 23:47:15 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert!(start < end,
|
|
|
|
"Request to format inverted span: {:?} to {:?}",
|
|
|
|
self.codemap.lookup_char_pos(start),
|
|
|
|
self.codemap.lookup_char_pos(end));
|
|
|
|
|
|
|
|
self.last_pos = end;
|
2015-07-26 07:05:43 -05:00
|
|
|
let span = codemap::mk_sp(start, end);
|
|
|
|
let snippet = self.snippet(span);
|
2015-04-20 23:47:15 -05:00
|
|
|
|
2015-07-26 07:05:43 -05:00
|
|
|
self.write_snippet(&snippet,
|
2015-08-01 08:02:59 -05:00
|
|
|
true,
|
|
|
|
&process_last_snippet);
|
2015-04-21 03:40:36 -05:00
|
|
|
}
|
2015-04-20 23:47:15 -05:00
|
|
|
|
2015-07-26 07:05:43 -05:00
|
|
|
fn write_snippet<F: Fn(&mut FmtVisitor, &str, &str)>(&mut self,
|
|
|
|
snippet: &str,
|
|
|
|
last_snippet: bool,
|
|
|
|
process_last_snippet: F) {
|
2015-04-21 03:40:36 -05:00
|
|
|
// 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 {
|
2015-07-26 07:05:43 -05:00
|
|
|
self.buffer.push_str(&snippet[line_start..lw]);
|
|
|
|
self.buffer.push_str("\n");
|
2015-04-20 23:47:15 -05:00
|
|
|
} else {
|
2015-07-26 07:05:43 -05:00
|
|
|
self.buffer.push_str(&snippet[line_start..i+1]);
|
2015-04-20 23:47:15 -05:00
|
|
|
}
|
2015-04-21 03:40:36 -05:00
|
|
|
|
|
|
|
line_start = i + 1;
|
|
|
|
last_wspace = None;
|
2015-04-20 23:47:15 -05:00
|
|
|
} else {
|
2015-04-21 03:40:36 -05:00
|
|
|
if c.is_whitespace() {
|
|
|
|
if last_wspace.is_none() {
|
|
|
|
last_wspace = Some(i);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
last_wspace = None;
|
|
|
|
}
|
2015-04-20 23:47:15 -05:00
|
|
|
}
|
|
|
|
}
|
2015-04-21 03:40:36 -05:00
|
|
|
if last_snippet {
|
2015-07-26 07:05:43 -05:00
|
|
|
process_last_snippet(self, &snippet[line_start..], snippet);
|
2015-04-21 03:40:36 -05:00
|
|
|
} else {
|
2015-07-26 07:05:43 -05:00
|
|
|
self.buffer.push_str(&snippet[line_start..]);
|
2015-04-21 03:40:36 -05:00
|
|
|
}
|
2015-04-20 23:47:15 -05:00
|
|
|
}
|
|
|
|
}
|