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