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