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