Skip to content

Commit 222c013

Browse files
committed
feat: Add ability to highlight source code
1 parent 57340c8 commit 222c013

File tree

5 files changed

+34
-2
lines changed

5 files changed

+34
-2
lines changed

examples/highlight_source.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ fn main() {}
2222
AnnotationKind::Primary
2323
.span(72..85)
2424
.label("allocation not allowed in constants")
25+
.highlight_source(true),
2526
),
2627
)
2728
.element(

examples/highlight_source.svg

Lines changed: 1 addition & 1 deletion
Loading

src/renderer/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,6 +1260,15 @@ impl Renderer {
12601260
underline.style,
12611261
);
12621262
}
1263+
_ if annotation.highlight_source => {
1264+
buffer.set_style_range(
1265+
line_offset,
1266+
(code_offset + annotation.start.display).saturating_sub(left),
1267+
(code_offset + annotation.end.display).saturating_sub(left),
1268+
underline.style,
1269+
annotation.is_primary(),
1270+
);
1271+
}
12631272
_ => {}
12641273
}
12651274
}
@@ -2471,6 +2480,9 @@ pub(crate) struct LineAnnotation<'a> {
24712480
/// Is this a single line, multiline or multiline span minimized down to a
24722481
/// smaller span.
24732482
pub annotation_type: LineAnnotationType,
2483+
2484+
/// Whether the source code should be highlighted
2485+
pub highlight_source: bool,
24742486
}
24752487

24762488
impl LineAnnotation<'_> {

src/renderer/source_map.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,13 @@ impl<'a> SourceMap<'a> {
149149
.collect::<Vec<_>>();
150150
let mut multiline_annotations = vec![];
151151

152-
for Annotation { range, label, kind } in annotations {
152+
for Annotation {
153+
range,
154+
label,
155+
kind,
156+
highlight_source,
157+
} in annotations
158+
{
153159
let (lo, mut hi) = self.span_to_locations(range.clone());
154160

155161
// Watch out for "empty spans". If we get a span like 6..6, we
@@ -169,6 +175,7 @@ impl<'a> SourceMap<'a> {
169175
kind,
170176
label,
171177
annotation_type: LineAnnotationType::Singleline,
178+
highlight_source,
172179
};
173180
self.add_annotation_to_file(&mut annotated_line_infos, lo.line, line_ann);
174181
} else {
@@ -179,6 +186,7 @@ impl<'a> SourceMap<'a> {
179186
kind,
180187
label,
181188
overlaps_exactly: false,
189+
highlight_source,
182190
});
183191
}
184192
}
@@ -502,6 +510,7 @@ pub(crate) struct MultilineAnnotation<'a> {
502510
pub kind: AnnotationKind,
503511
pub label: Option<&'a str>,
504512
pub overlaps_exactly: bool,
513+
pub highlight_source: bool,
505514
}
506515

507516
impl<'a> MultilineAnnotation<'a> {
@@ -526,6 +535,7 @@ impl<'a> MultilineAnnotation<'a> {
526535
kind: self.kind,
527536
label: None,
528537
annotation_type: LineAnnotationType::MultilineStart(self.depth),
538+
highlight_source: self.highlight_source,
529539
}
530540
}
531541

@@ -541,6 +551,7 @@ impl<'a> MultilineAnnotation<'a> {
541551
kind: self.kind,
542552
label: self.label,
543553
annotation_type: LineAnnotationType::MultilineEnd(self.depth),
554+
highlight_source: self.highlight_source,
544555
}
545556
}
546557

@@ -551,6 +562,7 @@ impl<'a> MultilineAnnotation<'a> {
551562
kind: self.kind,
552563
label: None,
553564
annotation_type: LineAnnotationType::MultilineLine(self.depth),
565+
highlight_source: self.highlight_source,
554566
}
555567
}
556568
}

src/snippet.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,13 +273,19 @@ pub struct Annotation<'a> {
273273
pub(crate) range: Range<usize>,
274274
pub(crate) label: Option<&'a str>,
275275
pub(crate) kind: AnnotationKind,
276+
pub(crate) highlight_source: bool,
276277
}
277278

278279
impl<'a> Annotation<'a> {
279280
pub fn label(mut self, label: &'a str) -> Self {
280281
self.label = Some(label);
281282
self
282283
}
284+
285+
pub fn highlight_source(mut self, highlight_source: bool) -> Self {
286+
self.highlight_source = highlight_source;
287+
self
288+
}
283289
}
284290

285291
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
@@ -296,6 +302,7 @@ impl AnnotationKind {
296302
range: span,
297303
label: None,
298304
kind: self,
305+
highlight_source: false,
299306
}
300307
}
301308

0 commit comments

Comments
 (0)