1 // Code for annotating snippets.
5 #[derive(Clone, Debug, PartialOrd, Ord, PartialEq, Eq)]
8 pub annotations: Vec<Annotation>,
12 #[derive(Clone, Debug, PartialOrd, Ord, PartialEq, Eq)]
13 pub struct MultilineAnnotation {
15 pub line_start: usize,
20 pub label: Option<String>,
23 impl MultilineAnnotation {
24 pub fn increase_depth(&mut self) {
28 pub fn as_start(&self) -> Annotation {
30 start_col: self.start_col,
31 end_col: self.start_col + 1,
32 is_primary: self.is_primary,
34 annotation_type: AnnotationType::MultilineStart(self.depth)
38 pub fn as_end(&self) -> 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)
48 pub fn as_line(&self) -> Annotation {
52 is_primary: self.is_primary,
54 annotation_type: AnnotationType::MultilineLine(self.depth)
59 #[derive(Clone, Debug, PartialOrd, Ord, PartialEq, Eq)]
60 pub enum AnnotationType {
61 /// Annotation under a single line of code
64 /// Annotation enclosing the first and last character of a multiline span
65 Multiline(MultilineAnnotation),
67 // The Multiline type above is replaced with the following three in order
68 // to reuse the current label drawing code.
70 // Each of these corresponds to one part of the following diagram:
73 // | _________^ < MultilineStart
74 // x | | y), < MultilineLine
75 // | |______________^ label < MultilineEnd
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
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`.
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
96 /// End column within the line (exclusive)
99 /// Is this annotation derived from primary span
100 pub is_primary: bool,
102 /// Optional label to display adjacent to the annotation.
103 pub label: Option<String>,
105 /// Is this a single line, multiline or multiline span minimized down to a
107 pub annotation_type: AnnotationType,
111 /// Whether this annotation is a vertical line placeholder.
112 pub fn is_line(&self) -> bool {
113 if let AnnotationType::MultilineLine(_) = self.annotation_type {
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,
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
135 self.start_col - self.end_col
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:
144 // X | fn foo(x: u32) {
150 // Note that this would be the complete output users would see.
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,
168 pub struct StyledString {
173 #[derive(Copy, Clone, Debug, PartialEq, Hash, RustcEncodable, RustcDecodable)]