Skip to content

New api #195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Apr 16, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: Add ability to highlight source code
  • Loading branch information
Muscraft committed Apr 16, 2025
commit 222c013d9cb828d1171b26811e4530dc8cb6e068
1 change: 1 addition & 0 deletions examples/highlight_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ fn main() {}
AnnotationKind::Primary
.span(72..85)
.label("allocation not allowed in constants")
.highlight_source(true),
),
)
.element(
Expand Down
2 changes: 1 addition & 1 deletion examples/highlight_source.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions src/renderer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1260,6 +1260,15 @@ impl Renderer {
underline.style,
);
}
_ if annotation.highlight_source => {
buffer.set_style_range(
line_offset,
(code_offset + annotation.start.display).saturating_sub(left),
(code_offset + annotation.end.display).saturating_sub(left),
underline.style,
annotation.is_primary(),
);
}
_ => {}
}
}
Expand Down Expand Up @@ -2471,6 +2480,9 @@ pub(crate) struct LineAnnotation<'a> {
/// Is this a single line, multiline or multiline span minimized down to a
/// smaller span.
pub annotation_type: LineAnnotationType,

/// Whether the source code should be highlighted
pub highlight_source: bool,
}

impl LineAnnotation<'_> {
Expand Down
14 changes: 13 additions & 1 deletion src/renderer/source_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,13 @@ impl<'a> SourceMap<'a> {
.collect::<Vec<_>>();
let mut multiline_annotations = vec![];

for Annotation { range, label, kind } in annotations {
for Annotation {
range,
label,
kind,
highlight_source,
} in annotations
{
let (lo, mut hi) = self.span_to_locations(range.clone());

// Watch out for "empty spans". If we get a span like 6..6, we
Expand All @@ -169,6 +175,7 @@ impl<'a> SourceMap<'a> {
kind,
label,
annotation_type: LineAnnotationType::Singleline,
highlight_source,
};
self.add_annotation_to_file(&mut annotated_line_infos, lo.line, line_ann);
} else {
Expand All @@ -179,6 +186,7 @@ impl<'a> SourceMap<'a> {
kind,
label,
overlaps_exactly: false,
highlight_source,
});
}
}
Expand Down Expand Up @@ -502,6 +510,7 @@ pub(crate) struct MultilineAnnotation<'a> {
pub kind: AnnotationKind,
pub label: Option<&'a str>,
pub overlaps_exactly: bool,
pub highlight_source: bool,
}

impl<'a> MultilineAnnotation<'a> {
Expand All @@ -526,6 +535,7 @@ impl<'a> MultilineAnnotation<'a> {
kind: self.kind,
label: None,
annotation_type: LineAnnotationType::MultilineStart(self.depth),
highlight_source: self.highlight_source,
}
}

Expand All @@ -541,6 +551,7 @@ impl<'a> MultilineAnnotation<'a> {
kind: self.kind,
label: self.label,
annotation_type: LineAnnotationType::MultilineEnd(self.depth),
highlight_source: self.highlight_source,
}
}

Expand All @@ -551,6 +562,7 @@ impl<'a> MultilineAnnotation<'a> {
kind: self.kind,
label: None,
annotation_type: LineAnnotationType::MultilineLine(self.depth),
highlight_source: self.highlight_source,
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/snippet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,13 +273,19 @@ pub struct Annotation<'a> {
pub(crate) range: Range<usize>,
pub(crate) label: Option<&'a str>,
pub(crate) kind: AnnotationKind,
pub(crate) highlight_source: bool,
}

impl<'a> Annotation<'a> {
pub fn label(mut self, label: &'a str) -> Self {
self.label = Some(label);
self
}

pub fn highlight_source(mut self, highlight_source: bool) -> Self {
self.highlight_source = highlight_source;
self
}
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
Expand All @@ -296,6 +302,7 @@ impl AnnotationKind {
range: span,
label: None,
kind: self,
highlight_source: false,
}
}

Expand Down