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
test: Add a test for passing precolored text
  • Loading branch information
Muscraft committed Apr 16, 2025
commit 481920d56fd1b573dfac210ab32bd255b8f2ad84
68 changes: 68 additions & 0 deletions examples/highlight_title.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use annotate_snippets::{AnnotationKind, Group, Level, Renderer, Snippet};
use anstyle::Effects;

fn main() {
let source = r#"// Make sure "highlighted" code is colored purple

//@ compile-flags: --error-format=human --color=always
//@ error-pattern:for<'a> 
//@ edition:2018

use core::pin::Pin;
use core::future::Future;
use core::any::Any;

fn query(_: fn(Box<(dyn Any + Send + '_)>) -> Pin<Box<(
dyn Future<Output = Result<Box<(dyn Any + 'static)>, String>> + Send + 'static
)>>) {}

fn wrapped_fn<'a>(_: Box<(dyn Any + Send)>) -> Pin<Box<(
dyn Future<Output = Result<Box<(dyn Any + 'static)>, String>> + Send + 'static
)>> {
Box::pin(async { Err("nope".into()) })
}

fn main() {
query(wrapped_fn);
}
"#;

let magenta = annotate_snippets::renderer::AnsiColor::Magenta
.on_default()
.effects(Effects::BOLD);
let title = format!(
"expected fn pointer `{}for<'a>{} fn(Box<{}(dyn Any + Send + 'a){}>) -> Pin<_>`
found fn item `fn(Box<{}(dyn Any + Send + 'static){}>) -> Pin<_> {}{{wrapped_fn}}{}`",
magenta.render(),
magenta.render_reset(),
magenta.render(),
magenta.render_reset(),
magenta.render(),
magenta.render_reset(),
magenta.render(),
magenta.render_reset()
);

let message = Level::Error.message("mismatched types").id("E0308").group(
Group::new()
.element(
Snippet::source(source)
.fold(true)
.origin("$DIR/highlighting.rs")
.annotation(
AnnotationKind::Primary
.span(589..599)
.label("one type is more general than the other"),
)
.annotation(
AnnotationKind::Context
.span(583..588)
.label("arguments to this function are incorrect"),
),
)
.element(Level::Note.title(&title)),
);

let renderer = Renderer::styled().anonymized_line_numbers(true);
anstream::println!("{}", renderer.render(message));
}
44 changes: 44 additions & 0 deletions examples/highlight_title.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions tests/examples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ fn highlight_source() {
assert_example(target, expected);
}

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

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