]> git.lizzy.rs Git - rust.git/blob - src/missed_spans.rs
f39480f5450f876a1b2b34b85a8ef13ad8153337
[rust.git] / src / missed_spans.rs
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.
4 //
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.
10
11 use config::Config;
12 use visitor::FmtVisitor;
13
14 use syntax::codemap::{self, BytePos};
15
16 impl<'a> FmtVisitor<'a> {
17     // TODO these format_missing methods are ugly. Refactor and add unit tests
18     // for the central whitespace stripping loop.
19     pub fn format_missing(&mut self, end: BytePos) {
20         self.format_missing_inner(end, |this, last_snippet, _| this.buffer.push_str(last_snippet))
21     }
22
23     pub fn format_missing_with_indent(&mut self, end: BytePos, config: &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");
30                                       }
31                                       let indent = this.block_indent.to_string(config);
32                                       this.buffer.push_str(&indent);
33                                   })
34     }
35
36     fn format_missing_inner<F: Fn(&mut FmtVisitor, &str, &str)>(&mut self,
37                                                                 end: BytePos,
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));
43
44         if start == end {
45             // Do nothing if this is the beginning of the file.
46             if start == self.codemap.lookup_char_pos(start).file.start_pos {
47                 return;
48             }
49             process_last_snippet(self, "", "");
50             return;
51         }
52
53         assert!(start < end,
54                 "Request to format inverted span: {:?} to {:?}",
55                 self.codemap.lookup_char_pos(start),
56                 self.codemap.lookup_char_pos(end));
57
58         self.last_pos = end;
59         let span = codemap::mk_sp(start, end);
60         let snippet = self.snippet(span);
61
62         self.write_snippet(&snippet, true, &process_last_snippet);
63     }
64
65     fn write_snippet<F: Fn(&mut FmtVisitor, &str, &str)>(&mut self,
66                                                          snippet: &str,
67                                                          last_snippet: bool,
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() {
75             if c == '\n' {
76                 if let Some(lw) = last_wspace {
77                     self.buffer.push_str(&snippet[line_start..lw]);
78                     self.buffer.push_str("\n");
79                 } else {
80                     self.buffer.push_str(&snippet[line_start..i+1]);
81                 }
82
83                 line_start = i + 1;
84                 last_wspace = None;
85             } else {
86                 if c.is_whitespace() {
87                     if last_wspace.is_none() {
88                         last_wspace = Some(i);
89                     }
90                 } else {
91                     last_wspace = None;
92                 }
93             }
94         }
95         if last_snippet {
96             process_last_snippet(self, &snippet[line_start..], snippet);
97         } else {
98             self.buffer.push_str(&snippet[line_start..]);
99         }
100     }
101 }