]> git.lizzy.rs Git - rust.git/blob - src/missed_spans.rs
5c5565c65969e25acd99ab5c918df42de49c73f5
[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 visitor::FmtVisitor;
12
13 use syntax::codemap::{self, BytePos};
14
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,
20                                   |this, last_snippet, _| this.buffer.push_str(last_snippet))
21     }
22
23     pub fn format_missing_with_indent(&mut self, end: BytePos) {
24         let config = self.config;
25         self.format_missing_inner(end,
26                                   |this, last_snippet, snippet| {
27                                       this.buffer.push_str(last_snippet.trim_right());
28                                       if last_snippet == snippet {
29                                           // No new lines in the snippet.
30                                           this.buffer.push_str("\n");
31                                       }
32                                       let indent = this.block_indent.to_string(config);
33                                       this.buffer.push_str(&indent);
34                                   })
35     }
36
37     fn format_missing_inner<F: Fn(&mut FmtVisitor, &str, &str)>(&mut self,
38                                                                 end: BytePos,
39                                                                 process_last_snippet: F) {
40         let start = self.last_pos;
41         debug!("format_missing_inner: {:?} to {:?}",
42                self.codemap.lookup_char_pos(start),
43                self.codemap.lookup_char_pos(end));
44
45         if start == end {
46             // Do nothing if this is the beginning of the file.
47             if start == self.codemap.lookup_char_pos(start).file.start_pos {
48                 return;
49             }
50             process_last_snippet(self, "", "");
51             return;
52         }
53
54         assert!(start < end,
55                 "Request to format inverted span: {:?} to {:?}",
56                 self.codemap.lookup_char_pos(start),
57                 self.codemap.lookup_char_pos(end));
58
59         self.last_pos = end;
60         let span = codemap::mk_sp(start, end);
61         let snippet = self.snippet(span);
62
63         self.write_snippet(&snippet, true, &process_last_snippet);
64     }
65
66     fn write_snippet<F: Fn(&mut FmtVisitor, &str, &str)>(&mut self,
67                                                          snippet: &str,
68                                                          last_snippet: bool,
69                                                          process_last_snippet: F) {
70         // Trim whitespace from the right hand side of each line.
71         // Annoyingly, the library functions for splitting by lines etc. are not
72         // quite right, so we must do it ourselves.
73         let mut line_start = 0;
74         let mut last_wspace = None;
75         for (i, c) in snippet.char_indices() {
76             if c == '\n' {
77                 if let Some(lw) = last_wspace {
78                     self.buffer.push_str(&snippet[line_start..lw]);
79                     self.buffer.push_str("\n");
80                 } else {
81                     self.buffer.push_str(&snippet[line_start..i + 1]);
82                 }
83
84                 line_start = i + 1;
85                 last_wspace = None;
86             } else {
87                 if c.is_whitespace() {
88                     if last_wspace.is_none() {
89                         last_wspace = Some(i);
90                     }
91                 } else {
92                     last_wspace = None;
93                 }
94             }
95         }
96         if last_snippet {
97             process_last_snippet(self, &snippet[line_start..], snippet);
98         } else {
99             self.buffer.push_str(&snippet[line_start..]);
100         }
101     }
102 }