Skip to content

Instantly share code, notes, and snippets.

@zbraniecki
Created August 26, 2019 17:30
Show Gist options
  • Save zbraniecki/1624937ef922a66884af8b2689d928ad to your computer and use it in GitHub Desktop.
Save zbraniecki/1624937ef922a66884af8b2689d928ad to your computer and use it in GitHub Desktop.
annotate-snippets replacement for language-reporting emit.rs example
use annotate_snippets::display_list::DisplayList;
use annotate_snippets::formatter::DisplayListFormatter;
use annotate_snippets::snippet::{Annotation, AnnotationType, Slice, Snippet, SourceAnnotation};
fn main() {
let source = r##"
(define test 123)
(+ test "")
()
"##;
// First snippet
let snippet = Snippet {
slices: vec![Slice {
source: source.to_string(),
line_start: 1,
origin: Some("test".to_string()),
fold: true,
annotations: vec![
SourceAnnotation {
label: "Expected integer but got string".to_string(),
annotation_type: AnnotationType::Error,
range: (29, 31),
},
SourceAnnotation {
label: "Expected integer but got string".to_string(),
annotation_type: AnnotationType::Info,
range: (29, 31),
},
],
}],
title: Some(Annotation {
label: Some("Unexpected type in `+` application".to_string()),
id: Some("E0001".to_string()),
annotation_type: AnnotationType::Error,
}),
footer: vec![],
};
let dl = DisplayList::from(snippet);
let dlf = DisplayListFormatter::new(true, false);
println!("{}\n", dlf.format(&dl));
// Second snippet
let snippet = Snippet {
slices: vec![Slice {
source: source.to_string(),
line_start: 1,
origin: Some("test".to_string()),
fold: true,
annotations: vec![SourceAnnotation {
label: "".to_string(),
annotation_type: AnnotationType::Warning,
range: (21, 32),
}],
}],
title: Some(Annotation {
label: Some("`+` function has no effect unless its result is used".to_string()),
id: None,
annotation_type: AnnotationType::Warning,
}),
footer: vec![Annotation {
label: Some("Great job!".to_string()),
id: None,
annotation_type: AnnotationType::Help,
}],
};
let dl = DisplayList::from(snippet);
let dlf = DisplayListFormatter::new(true, false);
println!("{}", dlf.format(&dl));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment