Skip to content

Commit 7c2dc77

Browse files
committed
Add InlineAnnotation
1 parent 231cf08 commit 7c2dc77

File tree

7 files changed

+90
-10
lines changed

7 files changed

+90
-10
lines changed

Diff for: examples/format.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use annotate_snippets::DisplayList;
2-
use annotate_snippets::{Annotation, AnnotationType, SourceAnnotation};
2+
use annotate_snippets::{Annotation, AnnotationType, InlineAnnotation, SourceAnnotation};
33
use annotate_snippets::{Slice, Snippet};
44

55
use annotate_snippets::renderers::get_renderer;
@@ -52,6 +52,20 @@ fn main() {
5252
range: 23..725,
5353
},
5454
],
55+
inline_annotations: &[
56+
InlineAnnotation {
57+
annotation_type: AnnotationType::Warning,
58+
range: 5..19,
59+
},
60+
InlineAnnotation {
61+
annotation_type: AnnotationType::Error,
62+
range: 49..50,
63+
},
64+
InlineAnnotation {
65+
annotation_type: AnnotationType::Error,
66+
range: 724..725,
67+
},
68+
],
5569
}],
5670
};
5771
let dl = DisplayList::from(&snippet);

Diff for: src/annotation.rs

+6
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,9 @@ pub struct SourceAnnotation<'s> {
2323
pub label: &'s str,
2424
pub annotation_type: AnnotationType,
2525
}
26+
27+
#[derive(Debug, Clone)]
28+
pub struct InlineAnnotation {
29+
pub range: Range<usize>,
30+
pub annotation_type: AnnotationType,
31+
}

Diff for: src/display_list/line.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,17 @@ pub enum DisplayLine<'d> {
1313
}
1414

1515
#[derive(Debug, Clone)]
16-
pub enum DisplaySourceLine<'d> {
17-
Content {
16+
pub enum DisplayContentElement<'d> {
17+
Text(&'d str),
18+
AnnotatedText {
1819
text: &'d str,
20+
annotation_type: AnnotationType,
1921
},
22+
}
23+
24+
#[derive(Debug, Clone)]
25+
pub enum DisplaySourceLine<'d> {
26+
Content(Vec<DisplayContentElement<'d>>),
2027
Annotation {
2128
annotation: Annotation<'d>,
2229
range: Range<usize>,

Diff for: src/display_list/list.rs

+40-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
use super::annotation::Annotation;
2-
use super::line::{DisplayLine, DisplayMark, DisplayMarkType, DisplayRawLine, DisplaySourceLine};
3-
use crate::{Slice, Snippet, SourceAnnotation};
2+
use super::line::{
3+
DisplayContentElement, DisplayLine, DisplayMark, DisplayMarkType, DisplayRawLine,
4+
DisplaySourceLine,
5+
};
6+
use crate::{InlineAnnotation, Slice, Snippet, SourceAnnotation};
47

58
#[derive(Debug, Clone)]
69
pub struct DisplayList<'d> {
@@ -55,15 +58,17 @@ impl<'d> From<&Slice<'d>> for DisplayList<'d> {
5558
});
5659

5760
let mut annotations: Vec<&SourceAnnotation> = slice.annotations.iter().collect();
61+
let mut inline_annotations: Vec<&InlineAnnotation> =
62+
slice.inline_annotations.iter().collect();
5863

59-
// let mut current_annotation = annotations.next();
6064
let mut line_start_pos = 0;
6165

6266
let mut i = slice.line_start.unwrap_or(1);
6367
for line in slice.source.lines() {
6468
let line_range = line_start_pos..(line_start_pos + line.chars().count() + 1);
6569

6670
let mut current_annotations = vec![];
71+
let mut current_inline_annotations = vec![];
6772
let mut inline_marks = vec![];
6873

6974
annotations.retain(|ann| {
@@ -100,10 +105,41 @@ impl<'d> From<&Slice<'d>> for DisplayList<'d> {
100105
}
101106
});
102107

108+
inline_annotations.retain(|ann| {
109+
if line_range.contains(&ann.range.start) && line_range.contains(&ann.range.end) {
110+
// Annotation in this line
111+
current_inline_annotations.push(*ann);
112+
false
113+
} else {
114+
true
115+
}
116+
});
117+
118+
let mut frag = vec![];
119+
120+
let mut ptr = 0;
121+
for ann in current_inline_annotations {
122+
if ptr < ann.range.start {
123+
frag.push(DisplayContentElement::Text(
124+
&line[ptr..ann.range.start - line_range.start],
125+
));
126+
}
127+
frag.push(DisplayContentElement::AnnotatedText {
128+
text: &line
129+
[(ann.range.start - line_range.start)..(ann.range.end - line_range.start)],
130+
annotation_type: ann.annotation_type.clone(),
131+
});
132+
ptr = ann.range.end - line_range.start;
133+
}
134+
135+
if ptr < line_range.end {
136+
frag.push(DisplayContentElement::Text(&line[ptr..]));
137+
}
138+
103139
body.push(DisplayLine::Source {
104140
lineno: Some(i),
105141
inline_marks,
106-
line: DisplaySourceLine::Content { text: line },
142+
line: DisplaySourceLine::Content(frag),
107143
});
108144
for ann in current_annotations {
109145
let start = if ann.range.start >= line_start_pos {

Diff for: src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pub mod renderers;
44
pub mod slice;
55
pub mod snippet;
66

7-
pub use annotation::{Annotation, AnnotationType, SourceAnnotation};
7+
pub use annotation::{Annotation, AnnotationType, InlineAnnotation, SourceAnnotation};
88
pub use display_list::DisplayList;
99
pub use slice::Slice;
1010
pub use snippet::Snippet;

Diff for: src/renderers/ascii_default/mod.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::renderers::ascii_default::styles::plain::Style;
1010

1111
use super::Renderer as RendererTrait;
1212
use crate::annotation::AnnotationType;
13+
use crate::display_list::line::DisplayContentElement;
1314
use crate::display_list::line::DisplayLine;
1415
use crate::display_list::line::DisplayMark;
1516
use crate::display_list::line::DisplayMarkType;
@@ -120,7 +121,22 @@ impl<S: StyleTrait> Renderer<S> {
120121
line: &DisplaySourceLine,
121122
) -> std::io::Result<()> {
122123
match line {
123-
DisplaySourceLine::Content { text } => write!(w, " {}", text),
124+
DisplaySourceLine::Content(frag) => {
125+
write!(w, " ")?;
126+
for elem in frag {
127+
match elem {
128+
DisplayContentElement::Text(t) => write!(w, "{}", t)?,
129+
DisplayContentElement::AnnotatedText {
130+
text,
131+
annotation_type,
132+
} => {
133+
let (_, style) = self.get_annotation_type_style(&annotation_type);
134+
S::fmt(w, text, &[StyleType::Emphasis, style])?
135+
}
136+
}
137+
}
138+
Ok(())
139+
}
124140
DisplaySourceLine::Annotation { annotation, range } => {
125141
let (_, style) = self.get_annotation_type_style(&annotation.annotation_type);
126142
let styles = [StyleType::Emphasis, style];

Diff for: src/slice.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
use crate::annotation::SourceAnnotation;
1+
use crate::annotation::{InlineAnnotation, SourceAnnotation};
22

33
#[derive(Debug, Clone, Default)]
44
pub struct Slice<'s> {
55
pub source: &'s str,
66
pub line_start: Option<usize>,
77
pub origin: Option<&'s str>,
88
pub annotations: &'s [SourceAnnotation<'s>],
9+
pub inline_annotations: &'s [InlineAnnotation],
910
}

0 commit comments

Comments
 (0)