2015-04-21 16:47:15 +12: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 21:01:19 +12:00
|
|
|
use visitor::FmtVisitor;
|
|
|
|
|
2015-10-19 21:40:00 +02:00
|
|
|
use syntax::codemap::{self, BytePos, Span};
|
|
|
|
use comment::{CharClasses, CodeCharKind};
|
2015-04-21 16:47:15 +12: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-09-24 11:01:01 +12:00
|
|
|
self.format_missing_inner(end,
|
|
|
|
|this, last_snippet, _| this.buffer.push_str(last_snippet))
|
2015-04-21 16:47:15 +12:00
|
|
|
}
|
|
|
|
|
2015-09-19 10:44:28 -07:00
|
|
|
pub fn format_missing_with_indent(&mut self, end: BytePos) {
|
|
|
|
let config = self.config;
|
2015-10-08 23:07:19 +02:00
|
|
|
self.format_missing_inner(end, |this, last_snippet, snippet| {
|
|
|
|
this.buffer.push_str(last_snippet.trim_right());
|
|
|
|
if last_snippet == snippet {
|
|
|
|
// No new lines in the snippet.
|
|
|
|
this.buffer.push_str("\n");
|
|
|
|
}
|
|
|
|
let indent = this.block_indent.to_string(config);
|
|
|
|
this.buffer.push_str(&indent);
|
|
|
|
})
|
2015-04-21 16:47:15 +12:00
|
|
|
}
|
|
|
|
|
2015-07-26 14:05:43 +02:00
|
|
|
fn format_missing_inner<F: Fn(&mut FmtVisitor, &str, &str)>(&mut self,
|
|
|
|
end: BytePos,
|
|
|
|
process_last_snippet: F) {
|
2015-04-21 16:47:15 +12:00
|
|
|
let start = self.last_pos;
|
|
|
|
|
|
|
|
if start == end {
|
2015-08-22 18:08:11 -04:00
|
|
|
// Do nothing if this is the beginning of the file.
|
2015-10-19 21:40:00 +02:00
|
|
|
if start != self.codemap.lookup_char_pos(start).file.start_pos {
|
|
|
|
process_last_snippet(self, "", "");
|
2015-08-22 18:08:11 -04:00
|
|
|
}
|
2015-04-21 16:47:15 +12: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 14:05:43 +02:00
|
|
|
let span = codemap::mk_sp(start, end);
|
2015-04-21 16:47:15 +12:00
|
|
|
|
2015-10-17 11:35:47 -07:00
|
|
|
self.write_snippet(&snippet, &process_last_snippet);
|
2015-04-21 20:40:36 +12:00
|
|
|
}
|
2015-04-21 16:47:15 +12:00
|
|
|
|
2015-07-26 14:05:43 +02:00
|
|
|
fn write_snippet<F: Fn(&mut FmtVisitor, &str, &str)>(&mut self,
|
|
|
|
snippet: &str,
|
|
|
|
process_last_snippet: F) {
|
2015-10-17 11:35:47 -07:00
|
|
|
let mut lines: Vec<&str> = snippet.lines().collect();
|
|
|
|
let last_snippet = if snippet.ends_with("\n") {
|
|
|
|
""
|
2015-04-21 20:40:36 +12:00
|
|
|
} else {
|
2015-10-17 11:35:47 -07:00
|
|
|
lines.pop().unwrap()
|
|
|
|
};
|
|
|
|
for line in lines.iter() {
|
|
|
|
self.buffer.push_str(line.trim_right());
|
|
|
|
self.buffer.push_str("\n");
|
2015-04-21 20:40:36 +12:00
|
|
|
}
|
2015-10-17 11:35:47 -07:00
|
|
|
process_last_snippet(self, &last_snippet, snippet);
|
2015-04-21 16:47:15 +12:00
|
|
|
}
|
|
|
|
}
|