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>,
21 pub overlaps_exactly: bool,
24 impl MultilineAnnotation {
25 pub fn increase_depth(&mut self) {
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
35 pub fn as_start(&self) -> Annotation {
37 start_col: self.start_col,
38 end_col: self.start_col + 1,
39 is_primary: self.is_primary,
41 annotation_type: AnnotationType::MultilineStart(self.depth)
45 pub fn as_end(&self) -> 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)
55 pub fn as_line(&self) -> Annotation {
59 is_primary: self.is_primary,
61 annotation_type: AnnotationType::MultilineLine(self.depth)
66 #[derive(Clone, Debug, PartialOrd, Ord, PartialEq, Eq)]
67 pub enum AnnotationType {
68 /// Annotation under a single line of code
71 /// Annotation enclosing the first and last character of a multiline span
72 Multiline(MultilineAnnotation),
74 // The Multiline type above is replaced with the following three in order
75 // to reuse the current label drawing code.
77 // Each of these corresponds to one part of the following diagram:
80 // | _________^ < MultilineStart
81 // x | | y), < MultilineLine
82 // | |______________^ label < MultilineEnd
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
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`.
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
101 pub start_col: usize,
103 /// End column within the line (exclusive)
106 /// Is this annotation derived from primary span
107 pub is_primary: bool,
109 /// Optional label to display adjacent to the annotation.
110 pub label: Option<String>,
112 /// Is this a single line, multiline or multiline span minimized down to a
114 pub annotation_type: AnnotationType,
118 /// Whether this annotation is a vertical line placeholder.
119 pub fn is_line(&self) -> bool {
120 if let AnnotationType::MultilineLine(_) = self.annotation_type {
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,
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
142 self.start_col - self.end_col
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:
151 // X | fn foo(x: u32) {
157 // Note that this would be the complete output users would see.
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,
175 pub struct StyledString {
180 #[derive(Copy, Clone, Debug, PartialEq, Hash, RustcEncodable, RustcDecodable)]