Skip to content

Commit 2a254a0

Browse files
author
Zibi Braniecki
committed
Add footer
1 parent 48cd91b commit 2a254a0

File tree

10 files changed

+249
-169
lines changed

10 files changed

+249
-169
lines changed

examples/footer.rs

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
extern crate annotate_snippets;
2+
3+
use annotate_snippets::display_list::DisplayList;
4+
use annotate_snippets::snippet::{SourceAnnotation, AnnotationType, Slice, Snippet, Annotation};
5+
6+
fn main() {
7+
let snippet = Snippet {
8+
title: Some(Annotation {
9+
label: Some("mismatched types".to_string()),
10+
id: Some("E0308".to_string()),
11+
annotation_type: AnnotationType::Error,
12+
}),
13+
footer: Some(Annotation {
14+
label: Some("foo".to_string()),
15+
id: None,
16+
annotation_type: AnnotationType::Note,
17+
}),
18+
slices: vec![
19+
Slice {
20+
source: " slices: vec![\"A\",".to_string(),
21+
line_start: 13,
22+
origin: Some("src/multislice.rs".to_string()),
23+
fold: false,
24+
annotations: vec![
25+
SourceAnnotation {
26+
label: "expected struct `annotate_snippets::snippet::Slice`, found reference".to_string(),
27+
annotation_type: AnnotationType::Error,
28+
range: (22, 25),
29+
},
30+
],
31+
},
32+
],
33+
};
34+
35+
println!("{}", DisplayList::from(snippet));
36+
}

examples/format.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
extern crate annotate_snippets;
22

33
use annotate_snippets::display_list::DisplayList;
4-
use annotate_snippets::snippet::{Annotation, AnnotationType, Slice, Snippet, TitleAnnotation};
4+
use annotate_snippets::snippet::{SourceAnnotation, AnnotationType, Slice, Snippet, Annotation};
55

66
fn main() {
77
let snippet = Snippet {
@@ -32,23 +32,24 @@ fn main() {
3232
origin: Some("src/format.rs".to_string()),
3333
fold: true,
3434
annotations: vec![
35-
Annotation {
35+
SourceAnnotation {
3636
label: "expected `Option<String>` because of return type".to_string(),
3737
annotation_type: AnnotationType::Warning,
3838
range: (6, 20),
3939
},
40-
Annotation {
40+
SourceAnnotation {
4141
label: "expected enum `std::option::Option".to_string(),
4242
annotation_type: AnnotationType::Error,
4343
range: (23, 746),
4444
},
4545
],
4646
}],
47-
title: Some(TitleAnnotation {
47+
title: Some(Annotation {
4848
label: Some("mismatched types".to_string()),
4949
id: Some("E0308".to_string()),
5050
annotation_type: AnnotationType::Error,
5151
}),
52+
footer: None,
5253
};
5354

5455
println!("{}", DisplayList::from(snippet));

examples/multislice.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
extern crate annotate_snippets;
22

33
use annotate_snippets::display_list::DisplayList;
4-
use annotate_snippets::snippet::{AnnotationType, Slice, Snippet, TitleAnnotation};
4+
use annotate_snippets::snippet::{AnnotationType, Slice, Snippet, Annotation};
55

66
fn main() {
77
let snippet = Snippet {
8-
title: Some(TitleAnnotation {
8+
title: Some(Annotation {
99
label: Some("mismatched types".to_string()),
1010
id: None,
1111
annotation_type: AnnotationType::Error,
1212
}),
13+
footer: None,
1314
slices: vec![
1415
Slice {
1516
source: "Foo".to_string(),

src/display_list.rs

+44-27
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1-
use snippet::{AnnotationType, Slice, Snippet, TitleAnnotation};
1+
use snippet::{AnnotationType, Slice, Snippet, Annotation};
22

33
pub struct DisplayList {
44
pub body: Vec<DisplayLine>,
55
}
66

77
#[derive(Debug, Clone, PartialEq)]
88
pub enum DisplayLine {
9-
Description {
10-
snippet_type: DisplaySnippetType,
11-
id: Option<String>,
9+
AlignedAnnotation {
1210
label: String,
11+
annotation_type: DisplayAnnotationType,
12+
},
13+
Annotation {
14+
label: String,
15+
id: Option<String>,
16+
annotation_type: DisplayAnnotationType,
1317
},
1418
Origin {
1519
path: String,
@@ -23,21 +27,21 @@ pub enum DisplayLine {
2327
content: String,
2428
range: (usize, usize),
2529
},
26-
Annotation {
30+
SourceAnnotation {
2731
inline_marks: Vec<DisplayMark>,
2832
range: (usize, usize),
2933
label: Option<String>,
3034
annotation_type: DisplayAnnotationType,
35+
annotation_part: DisplayAnnotationPart,
3136
},
3237
Fold {
3338
inline_marks: Vec<DisplayMark>,
3439
},
3540
}
3641

3742
#[derive(Debug, Clone, PartialEq)]
38-
pub enum DisplayAnnotationType {
39-
Error,
40-
Warning,
43+
pub enum DisplayAnnotationPart {
44+
Singleline,
4145
MultilineStart,
4246
MultilineEnd,
4347
}
@@ -49,9 +53,11 @@ pub enum DisplayMark {
4953
}
5054

5155
#[derive(Debug, Clone, PartialEq)]
52-
pub enum DisplaySnippetType {
56+
pub enum DisplayAnnotationType {
5357
Error,
5458
Warning,
59+
Note,
60+
Help,
5561
}
5662

5763
#[derive(Debug, Clone, PartialEq)]
@@ -62,15 +68,23 @@ pub enum DisplayHeaderType {
6268

6369
// Formatting
6470

65-
fn format_title(annotation: &TitleAnnotation) -> DisplayLine {
71+
fn format_title(annotation: &Annotation) -> DisplayLine {
6672
let label = annotation.label.clone().unwrap_or("".to_string());
67-
DisplayLine::Description {
68-
snippet_type: DisplaySnippetType::from(annotation.annotation_type),
73+
DisplayLine::Annotation {
74+
annotation_type: DisplayAnnotationType::from(annotation.annotation_type),
6975
id: annotation.id.clone(),
7076
label,
7177
}
7278
}
7379

80+
fn format_annotation(annotation: &Annotation) -> DisplayLine {
81+
let label = annotation.label.clone().unwrap_or("".to_string());
82+
DisplayLine::AlignedAnnotation {
83+
annotation_type: DisplayAnnotationType::from(annotation.annotation_type),
84+
label,
85+
}
86+
}
87+
7488
fn format_slice(slice: &Slice, is_first: bool) -> Vec<DisplayLine> {
7589
let mut body = format_body(slice);
7690
let mut result = vec![];
@@ -132,7 +146,7 @@ fn fold_body(body: &[DisplayLine]) -> Vec<DisplayLine> {
132146

133147
while idx < body.len() {
134148
match body[idx] {
135-
DisplayLine::Annotation {
149+
DisplayLine::SourceAnnotation {
136150
ref inline_marks, ..
137151
} => {
138152
if no_annotation_lines_counter > 10 {
@@ -204,13 +218,14 @@ fn format_body(slice: &Slice) -> Vec<DisplayLine> {
204218
let range = (start - line_start, end - line_start);
205219
body.insert(
206220
body_idx + 1,
207-
DisplayLine::Annotation {
221+
DisplayLine::SourceAnnotation {
208222
inline_marks: vec![],
209223
range,
210224
label: Some(annotation.label.clone()),
211225
annotation_type: DisplayAnnotationType::from(
212226
annotation.annotation_type,
213227
),
228+
annotation_part: DisplayAnnotationPart::Singleline,
214229
},
215230
);
216231
annotation_line_count += 1;
@@ -229,11 +244,14 @@ fn format_body(slice: &Slice) -> Vec<DisplayLine> {
229244
let range = (start - line_start, start - line_start + 1);
230245
body.insert(
231246
body_idx + 1,
232-
DisplayLine::Annotation {
247+
DisplayLine::SourceAnnotation {
233248
inline_marks: vec![],
234249
range,
235250
label: None,
236-
annotation_type: DisplayAnnotationType::MultilineStart,
251+
annotation_type: DisplayAnnotationType::from(
252+
annotation.annotation_type,
253+
),
254+
annotation_part: DisplayAnnotationPart::MultilineStart,
237255
},
238256
);
239257
annotation_line_count += 1;
@@ -261,11 +279,14 @@ fn format_body(slice: &Slice) -> Vec<DisplayLine> {
261279
let range = (end - line_start, end - line_start + 1);
262280
body.insert(
263281
body_idx + 1,
264-
DisplayLine::Annotation {
282+
DisplayLine::SourceAnnotation {
265283
inline_marks: vec![DisplayMark::AnnotationThrough],
266284
range,
267285
label: Some(annotation.label.clone()),
268-
annotation_type: DisplayAnnotationType::MultilineEnd,
286+
annotation_type: DisplayAnnotationType::from(
287+
annotation.annotation_type,
288+
),
289+
annotation_part: DisplayAnnotationPart::MultilineEnd,
269290
},
270291
);
271292
annotation_line_count += 1;
@@ -299,6 +320,9 @@ impl From<Snippet> for DisplayList {
299320
body.append(&mut format_slice(&slice, slice_idx == 0));
300321
slice_idx += 1;
301322
}
323+
if let Some(annotation) = snippet.footer {
324+
body.push(format_annotation(&annotation));
325+
}
302326

303327
Self { body }
304328
}
@@ -309,15 +333,8 @@ impl From<AnnotationType> for DisplayAnnotationType {
309333
match at {
310334
AnnotationType::Error => DisplayAnnotationType::Error,
311335
AnnotationType::Warning => DisplayAnnotationType::Warning,
312-
}
313-
}
314-
}
315-
316-
impl From<AnnotationType> for DisplaySnippetType {
317-
fn from(at: AnnotationType) -> Self {
318-
match at {
319-
AnnotationType::Error => DisplaySnippetType::Error,
320-
AnnotationType::Warning => DisplaySnippetType::Warning,
336+
AnnotationType::Note => DisplayAnnotationType::Note,
337+
AnnotationType::Help => DisplayAnnotationType::Help,
321338
}
322339
}
323340
}

src/display_list_formatting.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
use display_list::{DisplayAnnotationType, DisplayLine, DisplayMark, DisplaySnippetType};
1+
use display_list::{DisplayLine, DisplayMark, DisplayAnnotationType, DisplayAnnotationPart};
22
use std::fmt;
33

44
pub trait DisplayListFormatting {
5-
fn format_snippet_type(snippet_type: &DisplaySnippetType) -> String;
5+
fn format_annotation_type(annotation_type: &DisplayAnnotationType) -> String;
66

77
fn format_inline_marks(inline_marks: &[DisplayMark], inline_marks_width: usize) -> String;
88

99
fn format_annotation_content(
1010
range: &(usize, usize),
1111
label: &Option<String>,
1212
annotation_type: &DisplayAnnotationType,
13+
annotation_part: &DisplayAnnotationPart,
1314
) -> String;
1415

1516
fn format_line(

0 commit comments

Comments
 (0)