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