1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use visitor::FmtVisitor;
13 use syntax::codemap::{self, BytePos};
15 impl<'a> FmtVisitor<'a> {
16 // TODO these format_missing methods are ugly. Refactor and add unit tests
17 // for the central whitespace stripping loop.
18 pub fn format_missing(&mut self, end: BytePos) {
19 self.format_missing_inner(end, |this, last_snippet, _| this.buffer.push_str(last_snippet))
22 pub fn format_missing_with_indent(&mut self, end: BytePos) {
23 let config = self.config;
24 self.format_missing_inner(end,
25 |this, last_snippet, snippet| {
26 this.buffer.push_str(last_snippet.trim_right());
27 if last_snippet == snippet {
28 // No new lines in the snippet.
29 this.buffer.push_str("\n");
31 let indent = this.block_indent.to_string(config);
32 this.buffer.push_str(&indent);
36 fn format_missing_inner<F: Fn(&mut FmtVisitor, &str, &str)>(&mut self,
38 process_last_snippet: F) {
39 let start = self.last_pos;
40 debug!("format_missing_inner: {:?} to {:?}",
41 self.codemap.lookup_char_pos(start),
42 self.codemap.lookup_char_pos(end));
45 // Do nothing if this is the beginning of the file.
46 if start == self.codemap.lookup_char_pos(start).file.start_pos {
49 process_last_snippet(self, "", "");
54 "Request to format inverted span: {:?} to {:?}",
55 self.codemap.lookup_char_pos(start),
56 self.codemap.lookup_char_pos(end));
59 let span = codemap::mk_sp(start, end);
60 let snippet = self.snippet(span);
62 self.write_snippet(&snippet, true, &process_last_snippet);
65 fn write_snippet<F: Fn(&mut FmtVisitor, &str, &str)>(&mut self,
68 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() {
76 if let Some(lw) = last_wspace {
77 self.buffer.push_str(&snippet[line_start..lw]);
78 self.buffer.push_str("\n");
80 self.buffer.push_str(&snippet[line_start..i+1]);
86 if c.is_whitespace() {
87 if last_wspace.is_none() {
88 last_wspace = Some(i);
96 process_last_snippet(self, &snippet[line_start..], snippet);
98 self.buffer.push_str(&snippet[line_start..]);