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.
13 use syntax_pos::{Span, FileMap};
19 pub struct SnippetData {
20 codemap: Rc<CodeMapper>,
28 /// The "primary file", if any, gets a `-->` marker instead of
29 /// `>>>`, and has a line-number/column printed and not just a
30 /// filename. It appears first in the listing. It is known to
31 /// contain at least one primary span, though primary spans (which
32 /// are designated with `^^^`) may also occur in other files.
33 primary_span: Option<Span>,
38 #[derive(Clone, Debug, PartialOrd, Ord, PartialEq, Eq)]
40 pub line_index: usize,
41 pub annotations: Vec<Annotation>,
45 #[derive(Clone, Debug, PartialOrd, Ord, PartialEq, Eq)]
46 pub struct MultilineAnnotation {
48 pub line_start: usize,
53 pub label: Option<String>,
56 impl MultilineAnnotation {
57 pub fn increase_depth(&mut self) {
61 pub fn as_start(&self) -> Annotation {
63 start_col: self.start_col,
64 end_col: self.start_col + 1,
65 is_primary: self.is_primary,
67 annotation_type: AnnotationType::MultilineStart(self.depth)
71 pub fn as_end(&self) -> Annotation {
73 start_col: self.end_col - 1,
74 end_col: self.end_col,
75 is_primary: self.is_primary,
76 label: self.label.clone(),
77 annotation_type: AnnotationType::MultilineEnd(self.depth)
81 pub fn as_line(&self) -> Annotation {
85 is_primary: self.is_primary,
87 annotation_type: AnnotationType::MultilineLine(self.depth)
92 #[derive(Clone, Debug, PartialOrd, Ord, PartialEq, Eq)]
93 pub enum AnnotationType {
94 /// Annotation under a single line of code
97 /// Annotation enclosing the first and last character of a multiline span
98 Multiline(MultilineAnnotation),
100 // The Multiline type above is replaced with the following three in order
101 // to reuse the current label drawing code.
103 // Each of these corresponds to one part of the following diagram:
105 // x | foo(1 + bar(x,
106 // | _________^ < MultilineStart
107 // x | | y), < MultilineLine
108 // | |______________^ label < MultilineEnd
110 /// Annotation marking the first character of a fully shown multiline span
111 MultilineStart(usize),
112 /// Annotation marking the last character of a fully shown multiline span
114 /// Line at the left enclosing the lines of a fully shown multiline span
115 // Just a placeholder for the drawing algorithm, to know that it shouldn't skip the first 4
116 // and last 2 lines of code. The actual line is drawn in `emit_message_default` and not in
117 // `draw_multiline_line`.
118 MultilineLine(usize),
121 #[derive(Clone, Debug, PartialOrd, Ord, PartialEq, Eq)]
122 pub struct Annotation {
123 /// Start column, 0-based indexing -- counting *characters*, not
124 /// utf-8 bytes. Note that it is important that this field goes
125 /// first, so that when we sort, we sort orderings by start
127 pub start_col: usize,
129 /// End column within the line (exclusive)
132 /// Is this annotation derived from primary span
133 pub is_primary: bool,
135 /// Optional label to display adjacent to the annotation.
136 pub label: Option<String>,
138 /// Is this a single line, multiline or multiline span minimized down to a
140 pub annotation_type: AnnotationType,
144 /// Wether this annotation is a vertical line placeholder.
145 pub fn is_line(&self) -> bool {
146 if let AnnotationType::MultilineLine(_) = self.annotation_type {
153 pub fn is_multiline(&self) -> bool {
154 match self.annotation_type {
155 AnnotationType::Multiline(_) |
156 AnnotationType::MultilineStart(_) |
157 AnnotationType::MultilineLine(_) |
158 AnnotationType::MultilineEnd(_) => true,
163 pub fn len(&self) -> usize {
164 // Account for usize underflows
165 if self.end_col > self.start_col {
166 self.end_col - self.start_col
168 self.start_col - self.end_col
172 pub fn has_label(&self) -> bool {
173 if let Some(ref label) = self.label {
174 // Consider labels with no text as effectively not being there
175 // to avoid weird output with unnecessary vertical lines, like:
177 // X | fn foo(x: u32) {
183 // Note that this would be the complete output users would see.
190 pub fn takes_space(&self) -> bool {
191 // Multiline annotations always have to keep vertical space.
192 match self.annotation_type {
193 AnnotationType::MultilineStart(_) |
194 AnnotationType::MultilineEnd(_) => true,
201 pub struct StyledString {
206 #[derive(Copy, Clone, Debug, PartialEq, RustcEncodable, RustcDecodable)]