8000 Add suppost for Hyperlinks on IDs by Muscraft · Pull Request #230 · rust-lang/annotate-snippets-rs · GitHub
[go: up one dir, main page]

Skip to content

Add suppost for Hyperlinks on IDs #230

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 2 commits into from
Jun 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


8000
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 7 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions examples/id_hyperlink.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use annotate_snippets::renderer::OutputTheme;
use annotate_snippets::{AnnotationKind, Group, Level, Renderer, Snippet};

fn main() {
let source = r#"//@ compile-flags: -Zterminal-urls=yes
fn main() {
let () = 4; //~ ERROR
}
"#;
let message = Level::ERROR
.header("mismatched types")
.id("E0308")
.id_url("https://doc.rust-lang.org/error_codes/E0308.html")
.group(
Group::new().element(
Snippet::source(source)
.line_start(1)
.path("$DIR/terminal_urls.rs")
.fold(true)
.annotation(
AnnotationKind::Primary
.span(59..61)
.label("expected integer, found `()`"),
)
.annotation(
AnnotationKind::Context
.span(64..65)
.label("this expression has type `{integer}`"),
),
),
);

let renderer = Renderer::styled().theme(OutputTheme::Unicode);
anstream::println!("{}", renderer.render(message));
}
40 changes: 40 additions & 0 deletions examples/id_hyperlink.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 17 additions & 2 deletions src/renderer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ use crate::renderer::source_map::{
AnnotatedLineInfo, LineInfo, Loc, SourceMap, SubstitutionHighlight,
};
use crate::renderer::styled_buffer::StyledBuffer;
use crate::snippet::Id;
use crate::{Annotation, AnnotationKind, Element, Group, Message, Origin, Patch, Snippet, Title};
pub use anstyle::*;
use margin::Margin;
Expand Down Expand Up @@ -545,7 +546,7 @@ impl Renderer {
title: &Title<'_>,
max_line_num_len: usize,
title_style: TitleStyle,
id: Option<&&str>,
id: Option<&Id<'_>>,
is_cont: bool,
buffer_msg_line_offset: usize,
) {
Expand Down Expand Up @@ -620,17 +621,31 @@ impl Renderer {
);
}
label_width += title.level.as_str().len();
if let Some(id) = id {
if let Some(Id { id: Some(id), url }) = id {
buffer.append(
buffer_msg_line_offset,
"[",
ElementStyle::Level(title.level.level),
);
if let Some(url) = url.as_ref() {
buffer.append(
buffer_msg_line_offset,
&format!("\x1B]8;;{url}\x1B\\"),
ElementStyle::Level(title.level.level),
);
}
buffer.append(
buffer_msg_line_offset,
id,
ElementStyle::Level(title.level.level),
);
if url.is_some() {
buffer.append(
buffer_msg_line_offset,
"\x1B]8;;\x1B\\",
ElementStyle::Level(title.level.level),
);
}
buffer.append(
buffer_msg_line_offset,
"]",
Expand Down
20 changes: 18 additions & 2 deletions src/snippet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub(crate) const WARNING_TXT: &str = "warning";
/// Top-level user message
#[derive(Clone, Debug)]
pub struct Message<'a> {
pub(crate) id: Option<&'a str>, // for "correctness", could be sloppy and be on Title
pub(crate) id: Option<Id<'a>>, // for "correctness", could be sloppy and be on Title
pub(crate) groups: Vec<Group<'a>>,
}

Expand All @@ -26,7 +26,17 @@ impl<'a> Message<'a> {
///
/// </div>
pub fn id(mut self, id: &'a str) -> Self {
self.id = Some(id);
self.id.get_or_insert(Id::default()).id = Some(id);
self
}

/// <div class="warning">
///
/// This is only relevant if the `id` present
///
/// </div>
pub fn id_url(mut self, url: &'a str) -> Self {
self.id.get_or_insert(Id::default()).url = Some(url);
self
}

Expand Down Expand Up @@ -75,6 +85,12 @@ impl<'a> Message<'a> {
}
}

#[derive(Clone, Debug, Default)]
pub(crate) struct Id<'a> {
pub(crate) id: Option<&'a str>,
pub(crate) url: Option<&'a 8000 str>,
}

/// An [`Element`] container
#[derive(Clone, Debug)]
pub struct Group<'a> {
Expand Down
7 changes: 7 additions & 0 deletions tests/examples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ fn highlight_title() {
assert_example(target, expected);
}

#[test]
fn id_hyperlink() {
let target = "id_hyperlink";
let expected = snapbox::file!["../examples/id_hyperlink.svg": TermSvg];
assert_example(target, expected);
}

#[test]
fn multislice() {
let target = "multislice";
Expand Down
0