swc_common/errors/
snippet.rs1use super::Level;
14
15#[derive(Clone, Debug, PartialOrd, Ord, PartialEq, Eq)]
16pub struct Line {
17 pub line_index: usize,
18 pub annotations: Vec<Annotation>,
19}
20
21#[derive(Clone, Debug, PartialOrd, Ord, PartialEq, Eq)]
22pub struct MultilineAnnotation {
23 pub depth: usize,
24 pub line_start: usize,
25 pub line_end: usize,
26 pub start_col: usize,
27 pub end_col: usize,
28 pub is_primary: bool,
29 pub label: Option<String>,
30}
31
32impl MultilineAnnotation {
33 pub fn increase_depth(&mut self) {
34 self.depth += 1;
35 }
36
37 pub fn as_start(&self) -> Annotation {
38 Annotation {
39 start_col: self.start_col,
40 end_col: self.start_col + 1,
41 is_primary: self.is_primary,
42 label: None,
43 annotation_type: AnnotationType::MultilineStart(self.depth),
44 }
45 }
46
47 pub fn as_end(&self) -> Annotation {
48 Annotation {
49 start_col: self.end_col.saturating_sub(1),
50 end_col: self.end_col,
51 is_primary: self.is_primary,
52 label: self.label.clone(),
53 annotation_type: AnnotationType::MultilineEnd(self.depth),
54 }
55 }
56
57 pub fn as_line(&self) -> Annotation {
58 Annotation {
59 start_col: 0,
60 end_col: 0,
61 is_primary: self.is_primary,
62 label: None,
63 annotation_type: AnnotationType::MultilineLine(self.depth),
64 }
65 }
66}
67
68#[derive(Clone, Debug, PartialOrd, Ord, PartialEq, Eq)]
69pub enum AnnotationType {
70 Singleline,
72
73 Multiline(MultilineAnnotation),
75
76 MultilineStart(usize),
88 MultilineEnd(usize),
90 MultilineLine(usize),
95}
96
97#[derive(Clone, Debug, PartialOrd, Ord, PartialEq, Eq)]
98pub struct Annotation {
99 pub start_col: usize,
104
105 pub end_col: usize,
107
108 pub is_primary: bool,
110
111 pub label: Option<String>,
113
114 pub annotation_type: AnnotationType,
117}
118
119impl Annotation {
120 pub fn is_line(&self) -> bool {
122 matches!(self.annotation_type, AnnotationType::MultilineLine(_))
123 }
124
125 pub fn is_multiline(&self) -> bool {
126 matches!(
127 self.annotation_type,
128 AnnotationType::Multiline(_)
129 | AnnotationType::MultilineStart(_)
130 | AnnotationType::MultilineLine(_)
131 | AnnotationType::MultilineEnd(_)
132 )
133 }
134
135 pub fn len(&self) -> usize {
136 self.end_col.abs_diff(self.start_col)
138 }
139
140 pub fn has_label(&self) -> bool {
141 if let Some(ref label) = self.label {
142 !label.is_empty()
153 } else {
154 false
155 }
156 }
157
158 pub fn takes_space(&self) -> bool {
159 matches!(
161 self.annotation_type,
162 AnnotationType::MultilineStart(_) | AnnotationType::MultilineEnd(_)
163 )
164 }
165}
166
167#[derive(Debug)]
168pub struct StyledString {
169 pub text: String,
170 pub style: Style,
171}
172
173#[allow(clippy::enum_variant_names)]
174#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
175#[cfg_attr(
176 feature = "diagnostic-serde",
177 derive(serde::Serialize, serde::Deserialize)
178)]
179#[cfg_attr(
180 any(feature = "rkyv-impl"),
181 derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
182)]
183#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))]
184#[cfg_attr(feature = "rkyv-impl", repr(u32))]
185pub enum Style {
186 MainHeaderMsg,
187 HeaderMsg,
188 LineAndColumn,
189 LineNumber,
190 Quotation,
191 UnderlinePrimary,
192 UnderlineSecondary,
193 LabelPrimary,
194 LabelSecondary,
195 OldSchoolNoteText,
196 NoStyle,
197 Level(Level),
198 Highlight,
199}