]> git.lizzy.rs Git - rust.git/blob - src/librustc_errors/snippet.rs
concerning well-formed suggestions for unused shorthand field patterns
[rust.git] / src / librustc_errors / snippet.rs
1 // Copyright 2012-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 // Code for annotating snippets.
12
13 use syntax_pos::{Span, FileMap};
14 use CodeMapper;
15 use std::rc::Rc;
16 use Level;
17
18 #[derive(Clone)]
19 pub struct SnippetData {
20     codemap: Rc<CodeMapper>,
21     files: Vec<FileInfo>,
22 }
23
24 #[derive(Clone)]
25 pub struct FileInfo {
26     file: Rc<FileMap>,
27
28     /// The "primary file", if any, gets a `-->` marker instead of
29     /// `>>>`, and has a line-number/column printed and not just a
30     /// filename (other files are not guaranteed to have line numbers
31     /// or columns). It appears first in the listing. It is known to
32     /// contain at least one primary span, though primary spans (which
33     /// are designated with `^^^`) may also occur in other files.
34     primary_span: Option<Span>,
35
36     lines: Vec<Line>,
37 }
38
39 #[derive(Clone, Debug, PartialOrd, Ord, PartialEq, Eq)]
40 pub struct Line {
41     pub line_index: usize,
42     pub annotations: Vec<Annotation>,
43 }
44
45
46 #[derive(Clone, Debug, PartialOrd, Ord, PartialEq, Eq)]
47 pub struct MultilineAnnotation {
48     pub depth: usize,
49     pub line_start: usize,
50     pub line_end: usize,
51     pub start_col: usize,
52     pub end_col: usize,
53     pub is_primary: bool,
54     pub label: Option<String>,
55 }
56
57 impl MultilineAnnotation {
58     pub fn increase_depth(&mut self) {
59         self.depth += 1;
60     }
61
62     pub fn as_start(&self) -> Annotation {
63         Annotation {
64             start_col: self.start_col,
65             end_col: self.start_col + 1,
66             is_primary: self.is_primary,
67             label: None,
68             annotation_type: AnnotationType::MultilineStart(self.depth)
69         }
70     }
71
72     pub fn as_end(&self) -> Annotation {
73         Annotation {
74             start_col: self.end_col.saturating_sub(1),
75             end_col: self.end_col,
76             is_primary: self.is_primary,
77             label: self.label.clone(),
78             annotation_type: AnnotationType::MultilineEnd(self.depth)
79         }
80     }
81
82     pub fn as_line(&self) -> Annotation {
83         Annotation {
84             start_col: 0,
85             end_col: 0,
86             is_primary: self.is_primary,
87             label: None,
88             annotation_type: AnnotationType::MultilineLine(self.depth)
89         }
90     }
91 }
92
93 #[derive(Clone, Debug, PartialOrd, Ord, PartialEq, Eq)]
94 pub enum AnnotationType {
95     /// Annotation under a single line of code
96     Singleline,
97
98     /// Annotation enclosing the first and last character of a multiline span
99     Multiline(MultilineAnnotation),
100
101     // The Multiline type above is replaced with the following three in order
102     // to reuse the current label drawing code.
103     //
104     // Each of these corresponds to one part of the following diagram:
105     //
106     //     x |   foo(1 + bar(x,
107     //       |  _________^              < MultilineStart
108     //     x | |             y),        < MultilineLine
109     //       | |______________^ label   < MultilineEnd
110     //     x |       z);
111     /// Annotation marking the first character of a fully shown multiline span
112     MultilineStart(usize),
113     /// Annotation marking the last character of a fully shown multiline span
114     MultilineEnd(usize),
115     /// Line at the left enclosing the lines of a fully shown multiline span
116     // Just a placeholder for the drawing algorithm, to know that it shouldn't skip the first 4
117     // and last 2 lines of code. The actual line is drawn in `emit_message_default` and not in
118     // `draw_multiline_line`.
119     MultilineLine(usize),
120 }
121
122 #[derive(Clone, Debug, PartialOrd, Ord, PartialEq, Eq)]
123 pub struct Annotation {
124     /// Start column, 0-based indexing -- counting *characters*, not
125     /// utf-8 bytes. Note that it is important that this field goes
126     /// first, so that when we sort, we sort orderings by start
127     /// column.
128     pub start_col: usize,
129
130     /// End column within the line (exclusive)
131     pub end_col: usize,
132
133     /// Is this annotation derived from primary span
134     pub is_primary: bool,
135
136     /// Optional label to display adjacent to the annotation.
137     pub label: Option<String>,
138
139     /// Is this a single line, multiline or multiline span minimized down to a
140     /// smaller span.
141     pub annotation_type: AnnotationType,
142 }
143
144 impl Annotation {
145     /// Whether this annotation is a vertical line placeholder.
146     pub fn is_line(&self) -> bool {
147         if let AnnotationType::MultilineLine(_) = self.annotation_type {
148             true
149         } else {
150             false
151         }
152     }
153
154     pub fn is_multiline(&self) -> bool {
155         match self.annotation_type {
156             AnnotationType::Multiline(_) |
157             AnnotationType::MultilineStart(_) |
158             AnnotationType::MultilineLine(_) |
159             AnnotationType::MultilineEnd(_) => true,
160             _ => false,
161         }
162     }
163
164     pub fn len(&self) -> usize {
165         // Account for usize underflows
166         if self.end_col > self.start_col {
167             self.end_col - self.start_col
168         } else {
169             self.start_col - self.end_col
170         }
171     }
172
173     pub fn has_label(&self) -> bool {
174         if let Some(ref label) = self.label {
175             // Consider labels with no text as effectively not being there
176             // to avoid weird output with unnecessary vertical lines, like:
177             //
178             //     X | fn foo(x: u32) {
179             //       | -------^------
180             //       | |      |
181             //       | |
182             //       |
183             //
184             // Note that this would be the complete output users would see.
185             label.len() > 0
186         } else {
187             false
188         }
189     }
190
191     pub fn takes_space(&self) -> bool {
192         // Multiline annotations always have to keep vertical space.
193         match self.annotation_type {
194             AnnotationType::MultilineStart(_) |
195             AnnotationType::MultilineEnd(_) => true,
196             _ => false,
197         }
198     }
199 }
200
201 #[derive(Debug)]
202 pub struct StyledString {
203     pub text: String,
204     pub style: Style,
205 }
206
207 #[derive(Copy, Clone, Debug, PartialEq, Hash, RustcEncodable, RustcDecodable)]
208 pub enum Style {
209     HeaderMsg,
210     LineAndColumn,
211     LineNumber,
212     Quotation,
213     UnderlinePrimary,
214     UnderlineSecondary,
215     LabelPrimary,
216     LabelSecondary,
217     OldSchoolNoteText,
218     NoStyle,
219     Level(Level),
220     Highlight,
221 }