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.
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.
11 // Code for annotating snippets.
15 #[derive(Clone, Debug, PartialOrd, Ord, PartialEq, Eq)]
17 pub line_index: usize,
18 pub annotations: Vec<Annotation>,
22 #[derive(Clone, Debug, PartialOrd, Ord, PartialEq, Eq)]
23 pub struct MultilineAnnotation {
25 pub line_start: usize,
30 pub label: Option<String>,
33 impl MultilineAnnotation {
34 pub fn increase_depth(&mut self) {
38 pub fn as_start(&self) -> Annotation {
40 start_col: self.start_col,
41 end_col: self.start_col + 1,
42 is_primary: self.is_primary,
44 annotation_type: AnnotationType::MultilineStart(self.depth)
48 pub fn as_end(&self) -> 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)
58 pub fn as_line(&self) -> Annotation {
62 is_primary: self.is_primary,
64 annotation_type: AnnotationType::MultilineLine(self.depth)
69 #[derive(Clone, Debug, PartialOrd, Ord, PartialEq, Eq)]
70 pub enum AnnotationType {
71 /// Annotation under a single line of code
74 /// Annotation enclosing the first and last character of a multiline span
75 Multiline(MultilineAnnotation),
77 // The Multiline type above is replaced with the following three in order
78 // to reuse the current label drawing code.
80 // Each of these corresponds to one part of the following diagram:
83 // | _________^ < MultilineStart
84 // x | | y), < MultilineLine
85 // | |______________^ label < MultilineEnd
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
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`.
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
104 pub start_col: usize,
106 /// End column within the line (exclusive)
109 /// Is this annotation derived from primary span
110 pub is_primary: bool,
112 /// Optional label to display adjacent to the annotation.
113 pub label: Option<String>,
115 /// Is this a single line, multiline or multiline span minimized down to a
117 pub annotation_type: AnnotationType,
121 /// Whether this annotation is a vertical line placeholder.
122 pub fn is_line(&self) -> bool {
123 if let AnnotationType::MultilineLine(_) = self.annotation_type {
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,
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
145 self.start_col - self.end_col
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:
154 // X | fn foo(x: u32) {
160 // Note that this would be the complete output users would see.
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,
178 pub struct StyledString {
183 #[derive(Copy, Clone, Debug, PartialEq, Hash, RustcEncodable, RustcDecodable)]