From 87bc645dcb2c2ccc07cb3b4af74c17103c9f618d Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 11 Mar 2024 11:24:33 -0500 Subject: [PATCH 01/13] fix: Remove AnnotationType to Label conversion I suggested this when a `Label`s title was an `Option`. We shouldn't be creating labels with empty strings. --- src/snippet.rs | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/snippet.rs b/src/snippet.rs index 37f431a1..a2547327 100644 --- a/src/snippet.rs +++ b/src/snippet.rs @@ -113,15 +113,6 @@ impl<'a> Label<'a> { } } -impl From for Label<'_> { - fn from(annotation_type: AnnotationType) -> Self { - Label { - annotation_type, - label: "", - } - } -} - /// Structure containing the slice of text to be annotated and /// basic information about the location of the slice. /// From b49f9471d920c7f561fa61970039b0ba44e448ac Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 11 Mar 2024 14:12:18 -0500 Subject: [PATCH 02/13] fix!: Rename AnnotationType to Level --- src/renderer/display_list.rs | 54 +++++++------------ src/snippet.rs | 25 ++++----- tests/fixtures/deserialize.rs | 23 ++++---- tests/fixtures/no-color/issue_52.toml | 4 +- tests/fixtures/no-color/issue_9.toml | 8 +-- .../no-color/multiline_annotation.toml | 6 +-- .../no-color/multiline_annotation2.toml | 4 +- .../no-color/multiline_annotation3.toml | 4 +- .../no-color/multiple_annotations.toml | 8 +-- tests/fixtures/no-color/one_past.toml | 4 +- tests/fixtures/no-color/simple.toml | 6 +-- tests/fixtures/no-color/strip_line.toml | 4 +- tests/fixtures/no-color/strip_line_char.toml | 4 +- .../fixtures/no-color/strip_line_non_ws.toml | 4 +- 14 files changed, 70 insertions(+), 88 deletions(-) diff --git a/src/renderer/display_list.rs b/src/renderer/display_list.rs index 01484d45..f0bc537c 100644 --- a/src/renderer/display_list.rs +++ b/src/renderer/display_list.rs @@ -649,14 +649,14 @@ pub enum DisplayAnnotationType { Help, } -impl From for DisplayAnnotationType { - fn from(at: snippet::AnnotationType) -> Self { +impl From for DisplayAnnotationType { + fn from(at: snippet::Level) -> Self { match at { - snippet::AnnotationType::Error => DisplayAnnotationType::Error, - snippet::AnnotationType::Warning => DisplayAnnotationType::Warning, - snippet::AnnotationType::Info => DisplayAnnotationType::Info, - snippet::AnnotationType::Note => DisplayAnnotationType::Note, - snippet::AnnotationType::Help => DisplayAnnotationType::Help, + snippet::Level::Error => DisplayAnnotationType::Error, + snippet::Level::Warning => DisplayAnnotationType::Warning, + snippet::Level::Info => DisplayAnnotationType::Info, + snippet::Level::Note => DisplayAnnotationType::Note, + snippet::Level::Help => DisplayAnnotationType::Help, } } } @@ -736,7 +736,7 @@ fn format_label( fn format_title<'a>(title: snippet::Label<'a>, id: Option<&'a str>) -> DisplayLine<'a> { DisplayLine::Raw(DisplayRawLine::Annotation { annotation: Annotation { - annotation_type: DisplayAnnotationType::from(title.annotation_type), + annotation_type: DisplayAnnotationType::from(title.level), id, label: format_label(Some(title.label), Some(DisplayTextStyle::Emphasis)), }, @@ -750,7 +750,7 @@ fn format_footer(footer: snippet::Label<'_>) -> Vec> { for (i, line) in footer.label.lines().enumerate() { result.push(DisplayLine::Raw(DisplayRawLine::Annotation { annotation: Annotation { - annotation_type: DisplayAnnotationType::from(footer.annotation_type), + annotation_type: DisplayAnnotationType::from(footer.level), id: None, label: format_label(Some(line), None), }, @@ -1010,10 +1010,10 @@ fn format_body( // It would be nice to use filter_drain here once it's stable. annotations.retain(|annotation| { let body_idx = idx + annotation_line_count; - let annotation_type = match annotation.annotation_type { - snippet::AnnotationType::Error => DisplayAnnotationType::None, - snippet::AnnotationType::Warning => DisplayAnnotationType::None, - _ => DisplayAnnotationType::from(annotation.annotation_type), + let annotation_type = match annotation.level { + snippet::Level::Error => DisplayAnnotationType::None, + snippet::Level::Warning => DisplayAnnotationType::None, + _ => DisplayAnnotationType::from(annotation.level), }; match annotation.range { Range { start, .. } if start > line_end_index => true, @@ -1036,9 +1036,7 @@ fn format_body( label: format_label(Some(annotation.label), None), }, range, - annotation_type: DisplayAnnotationType::from( - annotation.annotation_type, - ), + annotation_type: DisplayAnnotationType::from(annotation.level), annotation_part: DisplayAnnotationPart::Standalone, }, }, @@ -1059,9 +1057,7 @@ fn format_body( { inline_marks.push(DisplayMark { mark_type: DisplayMarkType::AnnotationStart, - annotation_type: DisplayAnnotationType::from( - annotation.annotation_type, - ), + annotation_type: DisplayAnnotationType::from(annotation.level), }); } } else { @@ -1079,9 +1075,7 @@ fn format_body( label: vec![], }, range, - annotation_type: DisplayAnnotationType::from( - annotation.annotation_type, - ), + annotation_type: DisplayAnnotationType::from(annotation.level), annotation_part: DisplayAnnotationPart::MultilineStart, }, }, @@ -1098,9 +1092,7 @@ fn format_body( { inline_marks.push(DisplayMark { mark_type: DisplayMarkType::AnnotationThrough, - annotation_type: DisplayAnnotationType::from( - annotation.annotation_type, - ), + annotation_type: DisplayAnnotationType::from(annotation.level), }); } true @@ -1117,9 +1109,7 @@ fn format_body( { inline_marks.push(DisplayMark { mark_type: DisplayMarkType::AnnotationThrough, - annotation_type: DisplayAnnotationType::from( - annotation.annotation_type, - ), + annotation_type: DisplayAnnotationType::from(annotation.level), }); } @@ -1131,9 +1121,7 @@ fn format_body( lineno: None, inline_marks: vec![DisplayMark { mark_type: DisplayMarkType::AnnotationThrough, - annotation_type: DisplayAnnotationType::from( - annotation.annotation_type, - ), + annotation_type: DisplayAnnotationType::from(annotation.level), }], line: DisplaySourceLine::Annotation { annotation: Annotation { @@ -1142,9 +1130,7 @@ fn format_body( label: format_label(Some(annotation.label), None), }, range, - annotation_type: DisplayAnnotationType::from( - annotation.annotation_type, - ), + annotation_type: DisplayAnnotationType::from(annotation.level), annotation_part: DisplayAnnotationPart::MultilineEnd, }, }, diff --git a/src/snippet.rs b/src/snippet.rs index a2547327..f8907b87 100644 --- a/src/snippet.rs +++ b/src/snippet.rs @@ -67,35 +67,32 @@ impl<'a> Snippet<'a> { } pub struct Label<'a> { - pub(crate) annotation_type: AnnotationType, + pub(crate) level: Level, pub(crate) label: &'a str, } impl<'a> Label<'a> { - pub fn new(annotation_type: AnnotationType, label: &'a str) -> Self { - Self { - annotation_type, - label, - } + pub fn new(level: Level, label: &'a str) -> Self { + Self { level, label } } pub fn error(label: &'a str) -> Self { - Self::new(AnnotationType::Error, label) + Self::new(Level::Error, label) } pub fn warning(label: &'a str) -> Self { - Self::new(AnnotationType::Warning, label) + Self::new(Level::Warning, label) } pub fn info(label: &'a str) -> Self { - Self::new(AnnotationType::Info, label) + Self::new(Level::Info, label) } pub fn note(label: &'a str) -> Self { - Self::new(AnnotationType::Note, label) + Self::new(Level::Note, label) } pub fn help(label: &'a str) -> Self { - Self::new(AnnotationType::Help, label) + Self::new(Level::Help, label) } pub fn label(mut self, label: &'a str) -> Self { @@ -108,7 +105,7 @@ impl<'a> Label<'a> { SourceAnnotation { range: span, label: self.label, - annotation_type: self.annotation_type, + level: self.level, } } } @@ -155,7 +152,7 @@ impl<'a> Slice<'a> { /// Types of annotations. #[derive(Debug, Clone, Copy, PartialEq)] -pub enum AnnotationType { +pub enum Level { /// Error annotations are displayed using red color and "^" character. Error, /// Warning annotations are displayed using blue color and "-" character. @@ -173,5 +170,5 @@ pub struct SourceAnnotation<'a> { /// The byte range of the annotation in the `source` string pub(crate) range: Range, pub(crate) label: &'a str, - pub(crate) annotation_type: AnnotationType, + pub(crate) level: Level, } diff --git a/tests/fixtures/deserialize.rs b/tests/fixtures/deserialize.rs index a01c343c..ebdf592f 100644 --- a/tests/fixtures/deserialize.rs +++ b/tests/fixtures/deserialize.rs @@ -2,7 +2,7 @@ use serde::{Deserialize, Deserializer, Serialize}; use std::ops::Range; use annotate_snippets::{ - renderer::Margin, AnnotationType, Label, Renderer, Slice, Snippet, SourceAnnotation, + renderer::Margin, Label, Level, Renderer, Slice, Snippet, SourceAnnotation, }; #[derive(Deserialize)] @@ -63,8 +63,7 @@ where LabelDef<'a>, ); - Wrapper::deserialize(deserializer) - .map(|Wrapper(label)| Label::new(label.annotation_type, label.label)) + Wrapper::deserialize(deserializer).map(|Wrapper(label)| Label::new(label.level, label.label)) } fn deserialize_labels<'de, D>(deserializer: D) -> Result>, D::Error> @@ -80,7 +79,7 @@ where let v = Vec::deserialize(deserializer)?; Ok(v.into_iter() - .map(|Wrapper(a)| Label::new(a.annotation_type, a.label)) + .map(|Wrapper(a)| Label::new(a.level, a.label)) .collect()) } @@ -151,8 +150,8 @@ pub struct SourceAnnotationDef<'a> { pub range: Range, #[serde(borrow)] pub label: &'a str, - #[serde(with = "AnnotationTypeDef")] - pub annotation_type: AnnotationType, + #[serde(with = "LevelDef")] + pub level: Level, } impl<'a> From> for SourceAnnotation<'a> { @@ -160,24 +159,24 @@ impl<'a> From> for SourceAnnotation<'a> { let SourceAnnotationDef { range, label, - annotation_type, + level, } = val; - Label::new(annotation_type, label).span(range) + Label::new(level, label).span(range) } } #[derive(Serialize, Deserialize)] pub struct LabelDef<'a> { - #[serde(with = "AnnotationTypeDef")] - pub annotation_type: AnnotationType, + #[serde(with = "LevelDef")] + pub level: Level, #[serde(borrow)] pub label: &'a str, } #[allow(dead_code)] #[derive(Serialize, Deserialize)] -#[serde(remote = "AnnotationType")] -enum AnnotationTypeDef { +#[serde(remote = "Level")] +enum LevelDef { Error, Warning, Info, diff --git a/tests/fixtures/no-color/issue_52.toml b/tests/fixtures/no-color/issue_52.toml index ea239dd3..0c2378e6 100644 --- a/tests/fixtures/no-color/issue_52.toml +++ b/tests/fixtures/no-color/issue_52.toml @@ -1,5 +1,5 @@ [snippet.title] -annotation_type = "Error" +level = "Error" label = "" [[snippet.slices]] @@ -13,5 +13,5 @@ origin = "path/to/error.rs" fold = true [[snippet.slices.annotations]] label = "error here" -annotation_type = "Warning" +level = "Warning" range = [2,16] diff --git a/tests/fixtures/no-color/issue_9.toml b/tests/fixtures/no-color/issue_9.toml index ee1fe27b..c6a1c454 100644 --- a/tests/fixtures/no-color/issue_9.toml +++ b/tests/fixtures/no-color/issue_9.toml @@ -1,6 +1,6 @@ [snippet.title] label = "expected one of `.`, `;`, `?`, or an operator, found `for`" -annotation_type = "Error" +level = "Error" [[snippet.slices]] source = "let x = vec![1];" @@ -8,7 +8,7 @@ line_start = 4 origin = "/code/rust/src/test/ui/annotate-snippet/suggestion.rs" [[snippet.slices.annotations]] label = "move occurs because `x` has type `std::vec::Vec`, which does not implement the `Copy` trait" -annotation_type = "Warning" +level = "Warning" range = [4, 5] [[snippet.slices]] @@ -16,7 +16,7 @@ source = "let y = x;" line_start = 7 [[snippet.slices.annotations]] label = "value moved here" -annotation_type = "Warning" +level = "Warning" range = [8, 9] [[snippet.slices]] @@ -24,5 +24,5 @@ source = "x;" line_start = 9 [[snippet.slices.annotations]] label = "value used here after move" -annotation_type = "Error" +level = "Error" range = [0, 1] diff --git a/tests/fixtures/no-color/multiline_annotation.toml b/tests/fixtures/no-color/multiline_annotation.toml index 48c07250..64a3513b 100644 --- a/tests/fixtures/no-color/multiline_annotation.toml +++ b/tests/fixtures/no-color/multiline_annotation.toml @@ -28,13 +28,13 @@ origin = "src/format.rs" fold = true [[snippet.slices.annotations]] label = "expected `std::option::Option` because of return type" -annotation_type = "Warning" +level = "Warning" range = [5, 19] [[snippet.slices.annotations]] label = "expected enum `std::option::Option`, found ()" -annotation_type = "Error" +level = "Error" range = [22, 766] [snippet] -title = { annotation_type = "Error", label = "mismatched types" } +title = { level = "Error", label = "mismatched types" } id = "E0308" diff --git a/tests/fixtures/no-color/multiline_annotation2.toml b/tests/fixtures/no-color/multiline_annotation2.toml index 89294bcc..7c487ab7 100644 --- a/tests/fixtures/no-color/multiline_annotation2.toml +++ b/tests/fixtures/no-color/multiline_annotation2.toml @@ -9,9 +9,9 @@ origin = "src/display_list.rs" fold = false [[snippet.slices.annotations]] label = "missing fields `lineno`, `content`" -annotation_type = "Error" +level = "Error" range = [31, 128] [snippet] -title = { annotation_type = "Error", label = "pattern does not mention fields `lineno`, `content`" } +title = { level = "Error", label = "pattern does not mention fields `lineno`, `content`" } id = "E0027" diff --git a/tests/fixtures/no-color/multiline_annotation3.toml b/tests/fixtures/no-color/multiline_annotation3.toml index efc1f5f6..3850ecab 100644 --- a/tests/fixtures/no-color/multiline_annotation3.toml +++ b/tests/fixtures/no-color/multiline_annotation3.toml @@ -9,9 +9,9 @@ origin = "foo.txt" fold = false [[snippet.slices.annotations]] label = "this should not be on separate lines" -annotation_type = "Error" +level = "Error" range = [11, 18] [snippet] -title = { annotation_type = "Error", label = "spacing error found" } +title = { level = "Error", label = "spacing error found" } id = "E####" diff --git a/tests/fixtures/no-color/multiple_annotations.toml b/tests/fixtures/no-color/multiple_annotations.toml index ae35cef6..bbb2e285 100644 --- a/tests/fixtures/no-color/multiple_annotations.toml +++ b/tests/fixtures/no-color/multiple_annotations.toml @@ -1,5 +1,5 @@ [snippet.title] -annotation_type = "Error" +level = "Error" label = "" [[snippet.slices]] @@ -17,13 +17,13 @@ fn add_title_line(result: &mut Vec, main_annotation: Option<&Annotation> line_start = 96 [[snippet.slices.annotations]] label = "Variable defined here" -annotation_type = "Error" +level = "Error" range = [100, 110] [[snippet.slices.annotations]] label = "Referenced here" -annotation_type = "Error" +level = "Error" range = [184, 194] [[snippet.slices.annotations]] label = "Referenced again here" -annotation_type = "Error" +level = "Error" range = [243, 253] diff --git a/tests/fixtures/no-color/one_past.toml b/tests/fixtures/no-color/one_past.toml index 62d1d42c..d7eb0416 100644 --- a/tests/fixtures/no-color/one_past.toml +++ b/tests/fixtures/no-color/one_past.toml @@ -1,6 +1,6 @@ [snippet.title] label = "expected `.`, `=`" -annotation_type = "Error" +level = "Error" [[snippet.slices]] source = "asdf" @@ -8,5 +8,5 @@ line_start = 1 origin = "Cargo.toml" [[snippet.slices.annotations]] label = "" -annotation_type = "Error" +level = "Error" range = [4, 5] diff --git a/tests/fixtures/no-color/simple.toml b/tests/fixtures/no-color/simple.toml index 2e9b6d89..0d592c1e 100644 --- a/tests/fixtures/no-color/simple.toml +++ b/tests/fixtures/no-color/simple.toml @@ -7,12 +7,12 @@ line_start = 169 origin = "src/format_color.rs" [[snippet.slices.annotations]] label = "unexpected token" -annotation_type = "Error" +level = "Error" range = [20, 23] [[snippet.slices.annotations]] label = "expected one of `.`, `;`, `?`, or an operator here" -annotation_type = "Warning" +level = "Warning" range = [10, 11] [snippet.title] label = "expected one of `.`, `;`, `?`, or an operator, found `for`" -annotation_type = "Error" \ No newline at end of file +level = "Error" diff --git a/tests/fixtures/no-color/strip_line.toml b/tests/fixtures/no-color/strip_line.toml index 9de50aac..eedfcbba 100644 --- a/tests/fixtures/no-color/strip_line.toml +++ b/tests/fixtures/no-color/strip_line.toml @@ -1,5 +1,5 @@ [snippet] -title = { annotation_type = "Error", label = "mismatched types" } +title = { level = "Error", label = "mismatched types" } id = "E0308" [[snippet.slices]] @@ -9,7 +9,7 @@ origin = "$DIR/whitespace-trimming.rs" [[snippet.slices.annotations]] label = "expected (), found integer" -annotation_type = "Error" +level = "Error" range = [192, 194] [renderer] diff --git a/tests/fixtures/no-color/strip_line_char.toml b/tests/fixtures/no-color/strip_line_char.toml index 72df2abd..dc51bb6b 100644 --- a/tests/fixtures/no-color/strip_line_char.toml +++ b/tests/fixtures/no-color/strip_line_char.toml @@ -1,5 +1,5 @@ [snippet] -title = { annotation_type = "Error", label = "mismatched types" } +title = { level = "Error", label = "mismatched types" } id = "E0308" [[snippet.slices]] @@ -9,7 +9,7 @@ origin = "$DIR/whitespace-trimming.rs" [[snippet.slices.annotations]] label = "expected (), found integer" -annotation_type = "Error" +level = "Error" range = [192, 194] [renderer] diff --git a/tests/fixtures/no-color/strip_line_non_ws.toml b/tests/fixtures/no-color/strip_line_non_ws.toml index 236d8f17..f56f55d1 100644 --- a/tests/fixtures/no-color/strip_line_non_ws.toml +++ b/tests/fixtures/no-color/strip_line_non_ws.toml @@ -1,5 +1,5 @@ [snippet] -title = { annotation_type = "Error", label = "mismatched types" } +title = { level = "Error", label = "mismatched types" } id = "E0308" [[snippet.slices]] @@ -9,7 +9,7 @@ origin = "$DIR/non-whitespace-trimming.rs" [[snippet.slices.annotations]] label = "expected (), found integer" -annotation_type = "Error" +level = "Error" range = [240, 242] [renderer] From bbf9c5fe27e83652433151cbfc7d6cafc02a8c47 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 11 Mar 2024 14:13:31 -0500 Subject: [PATCH 03/13] fix!: Rename SourceAnnotation to Annotation --- src/snippet.rs | 12 ++++++------ tests/fixtures/deserialize.rs | 22 +++++++++------------- 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/src/snippet.rs b/src/snippet.rs index f8907b87..dde50186 100644 --- a/src/snippet.rs +++ b/src/snippet.rs @@ -100,9 +100,9 @@ impl<'a> Label<'a> { self } - /// Create a [`SourceAnnotation`] with the given span for a [`Slice`] - pub fn span(&self, span: Range) -> SourceAnnotation<'a> { - SourceAnnotation { + /// Create a [`Annotation`] with the given span for a [`Slice`] + pub fn span(&self, span: Range) -> Annotation<'a> { + Annotation { range: span, label: self.label, level: self.level, @@ -119,7 +119,7 @@ pub struct Slice<'a> { pub(crate) source: &'a str, pub(crate) line_start: usize, pub(crate) origin: Option<&'a str>, - pub(crate) annotations: Vec>, + pub(crate) annotations: Vec>, pub(crate) fold: bool, } @@ -139,7 +139,7 @@ impl<'a> Slice<'a> { self } - pub fn annotation(mut self, annotation: SourceAnnotation<'a>) -> Self { + pub fn annotation(mut self, annotation: Annotation<'a>) -> Self { self.annotations.push(annotation); self } @@ -166,7 +166,7 @@ pub enum Level { /// /// This gets created by [`Label::span`]. #[derive(Debug)] -pub struct SourceAnnotation<'a> { +pub struct Annotation<'a> { /// The byte range of the annotation in the `source` string pub(crate) range: Range, pub(crate) label: &'a str, diff --git a/tests/fixtures/deserialize.rs b/tests/fixtures/deserialize.rs index ebdf592f..bd94770c 100644 --- a/tests/fixtures/deserialize.rs +++ b/tests/fixtures/deserialize.rs @@ -1,9 +1,7 @@ use serde::{Deserialize, Deserializer, Serialize}; use std::ops::Range; -use annotate_snippets::{ - renderer::Margin, Label, Level, Renderer, Slice, Snippet, SourceAnnotation, -}; +use annotate_snippets::{renderer::Margin, Annotation, Label, Level, Renderer, Slice, Snippet}; #[derive(Deserialize)] pub struct Fixture<'a> { @@ -105,9 +103,9 @@ pub struct SliceDef<'a> { pub line_start: usize, #[serde(borrow)] pub origin: Option<&'a str>, - #[serde(deserialize_with = "deserialize_source_annotations")] + #[serde(deserialize_with = "deserialize_annotations")] #[serde(borrow)] - pub annotations: Vec>, + pub annotations: Vec>, #[serde(default)] pub fold: bool, } @@ -132,21 +130,19 @@ impl<'a> From> for Slice<'a> { } } -fn deserialize_source_annotations<'de, D>( - deserializer: D, -) -> Result>, D::Error> +fn deserialize_annotations<'de, D>(deserializer: D) -> Result>, D::Error> where D: Deserializer<'de>, { #[derive(Deserialize)] - struct Wrapper<'a>(#[serde(borrow)] SourceAnnotationDef<'a>); + struct Wrapper<'a>(#[serde(borrow)] AnnotationDef<'a>); let v = Vec::deserialize(deserializer)?; Ok(v.into_iter().map(|Wrapper(a)| a.into()).collect()) } #[derive(Serialize, Deserialize)] -pub struct SourceAnnotationDef<'a> { +pub struct AnnotationDef<'a> { pub range: Range, #[serde(borrow)] pub label: &'a str, @@ -154,9 +150,9 @@ pub struct SourceAnnotationDef<'a> { pub level: Level, } -impl<'a> From> for SourceAnnotation<'a> { - fn from(val: SourceAnnotationDef<'a>) -> Self { - let SourceAnnotationDef { +impl<'a> From> for Annotation<'a> { + fn from(val: AnnotationDef<'a>) -> Self { + let AnnotationDef { range, label, level, From 105da760b6e1bd4cfce4c642ac679ecf6011f511 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 11 Mar 2024 15:13:58 -0500 Subject: [PATCH 04/13] fix!: Rename Snippet to Message --- benches/simple.rs | 6 ++--- examples/expected_type.rs | 6 ++--- examples/footer.rs | 6 ++--- examples/format.rs | 6 ++--- examples/multislice.rs | 6 ++--- src/lib.rs | 4 +-- src/renderer/display_list.rs | 18 ++++++------- src/renderer/mod.rs | 14 +++++----- src/snippet.rs | 6 ++--- tests/fixtures/deserialize.rs | 26 +++++++++---------- tests/fixtures/main.rs | 8 +++--- tests/fixtures/no-color/issue_52.toml | 6 ++--- tests/fixtures/no-color/issue_9.toml | 14 +++++----- .../no-color/multiline_annotation.toml | 8 +++--- .../no-color/multiline_annotation2.toml | 6 ++--- .../no-color/multiline_annotation3.toml | 6 ++--- .../no-color/multiple_annotations.toml | 10 +++---- tests/fixtures/no-color/one_past.toml | 6 ++--- tests/fixtures/no-color/simple.toml | 8 +++--- tests/fixtures/no-color/strip_line.toml | 6 ++--- tests/fixtures/no-color/strip_line_char.toml | 6 ++--- .../fixtures/no-color/strip_line_non_ws.toml | 6 ++--- tests/formatter.rs | 12 ++++----- 23 files changed, 100 insertions(+), 100 deletions(-) diff --git a/benches/simple.rs b/benches/simple.rs index 4eacfdaa..75a852f0 100644 --- a/benches/simple.rs +++ b/benches/simple.rs @@ -4,7 +4,7 @@ extern crate criterion; use criterion::{black_box, Criterion}; -use annotate_snippets::{Label, Renderer, Slice, Snippet}; +use annotate_snippets::{Label, Message, Renderer, Slice}; fn create_snippet(renderer: Renderer) { let source = r#") -> Option { @@ -29,7 +29,7 @@ fn create_snippet(renderer: Renderer) { _ => continue, } }"#; - let snippet = Snippet::error("mismatched types").id("E0308").slice( + let message = Message::error("mismatched types").id("E0308").slice( Slice::new(source, 51) .origin("src/format.rs") .annotation( @@ -38,7 +38,7 @@ fn create_snippet(renderer: Renderer) { .annotation(Label::error("expected enum `std::option::Option`").span(26..724)), ); - let _result = renderer.render(snippet).to_string(); + let _result = renderer.render(message).to_string(); } pub fn criterion_benchmark(c: &mut Criterion) { diff --git a/examples/expected_type.rs b/examples/expected_type.rs index adcbeff1..ccdb8351 100644 --- a/examples/expected_type.rs +++ b/examples/expected_type.rs @@ -1,11 +1,11 @@ -use annotate_snippets::{Label, Renderer, Slice, Snippet}; +use annotate_snippets::{Label, Message, Renderer, Slice}; fn main() { let source = r#" annotations: vec![SourceAnnotation { label: "expected struct `annotate_snippets::snippet::Slice`, found reference" , range: <22, 25>,"#; - let snippet = Snippet::error("expected type, found `22`").slice( + let message = Message::error("expected type, found `22`").slice( Slice::new(source, 26) .origin("examples/footer.rs") .fold(true) @@ -19,5 +19,5 @@ fn main() { ); let renderer = Renderer::styled(); - anstream::println!("{}", renderer.render(snippet)); + anstream::println!("{}", renderer.render(message)); } diff --git a/examples/footer.rs b/examples/footer.rs index 6c45328a..924fae2f 100644 --- a/examples/footer.rs +++ b/examples/footer.rs @@ -1,7 +1,7 @@ -use annotate_snippets::{Label, Renderer, Slice, Snippet}; +use annotate_snippets::{Label, Message, Renderer, Slice}; fn main() { - let snippet = Snippet::error("mismatched types") + let message = Message::error("mismatched types") .id("E0308") .slice( Slice::new(" slices: vec![\"A\",", 13) @@ -18,5 +18,5 @@ fn main() { )); let renderer = Renderer::styled(); - anstream::println!("{}", renderer.render(snippet)); + anstream::println!("{}", renderer.render(message)); } diff --git a/examples/format.rs b/examples/format.rs index 1337812e..85e9a82b 100644 --- a/examples/format.rs +++ b/examples/format.rs @@ -1,4 +1,4 @@ -use annotate_snippets::{Label, Renderer, Slice, Snippet}; +use annotate_snippets::{Label, Message, Renderer, Slice}; fn main() { let source = r#") -> Option { @@ -23,7 +23,7 @@ fn main() { _ => continue, } }"#; - let snippet = Snippet::error("mismatched types").id("E0308").slice( + let message = Message::error("mismatched types").id("E0308").slice( Slice::new(source, 51) .origin("src/format.rs") .annotation( @@ -33,5 +33,5 @@ fn main() { ); let renderer = Renderer::styled(); - anstream::println!("{}", renderer.render(snippet)); + anstream::println!("{}", renderer.render(message)); } diff --git a/examples/multislice.rs b/examples/multislice.rs index b6fcb3f8..48e73726 100644 --- a/examples/multislice.rs +++ b/examples/multislice.rs @@ -1,10 +1,10 @@ -use annotate_snippets::{Renderer, Slice, Snippet}; +use annotate_snippets::{Message, Renderer, Slice}; fn main() { - let snippet = Snippet::error("mismatched types") + let message = Message::error("mismatched types") .slice(Slice::new("Foo", 51).origin("src/format.rs")) .slice(Slice::new("Faa", 129).origin("src/display.rs")); let renderer = Renderer::styled(); - anstream::println!("{}", renderer.render(snippet)); + anstream::println!("{}", renderer.render(message)); } diff --git a/src/lib.rs b/src/lib.rs index 8602c0a4..a2e4231d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,10 +16,10 @@ //! The crate uses a three stage process with two conversions between states: //! //! ```text -//! Snippet --> Renderer --> impl Display +//! Message --> Renderer --> impl Display //! ``` //! -//! The input type - [Snippet] is a structure designed +//! The input type - [Message] is a structure designed //! to align with likely output from any parser whose code snippet is to be //! annotated. //! diff --git a/src/renderer/display_list.rs b/src/renderer/display_list.rs index f0bc537c..07216c39 100644 --- a/src/renderer/display_list.rs +++ b/src/renderer/display_list.rs @@ -106,12 +106,12 @@ impl<'a> DisplayList<'a> { const WARNING_TXT: &'static str = "warning"; pub(crate) fn new( - snippet::Snippet { + snippet::Message { title, id, footer, slices, - }: snippet::Snippet<'a>, + }: snippet::Message<'a>, stylesheet: &'a Stylesheet, anonymized_line_numbers: bool, margin: Option, @@ -1206,7 +1206,7 @@ mod tests { #[test] fn test_format_title() { - let input = snippet::Snippet::error("This is a title").id("E0001"); + let input = snippet::Message::error("This is a title").id("E0001"); let output = from_display_lines(vec![DisplayLine::Raw(DisplayRawLine::Annotation { annotation: Annotation { annotation_type: DisplayAnnotationType::Error, @@ -1227,7 +1227,7 @@ mod tests { let line_1 = "This is line 1"; let line_2 = "This is line 2"; let source = [line_1, line_2].join("\n"); - let input = snippet::Snippet::error("").slice(snippet::Slice::new(&source, 5402)); + let input = snippet::Message::error("").slice(snippet::Slice::new(&source, 5402)); let output = from_display_lines(vec![ DisplayLine::Raw(DisplayRawLine::Annotation { annotation: Annotation { @@ -1277,7 +1277,7 @@ mod tests { let src_0_len = src_0.len(); let src_1 = "This is slice 2"; let src_1_len = src_1.len(); - let input = snippet::Snippet::error("") + let input = snippet::Message::error("") .slice(snippet::Slice::new(src_0, 5402).origin("file1.rs")) .slice(snippet::Slice::new(src_1, 2).origin("file2.rs")); let output = from_display_lines(vec![ @@ -1350,7 +1350,7 @@ mod tests { let source = [line_1, line_2].join("\n"); // In line 2 let range = 22..24; - let input = snippet::Snippet::error("").slice( + let input = snippet::Message::error("").slice( snippet::Slice::new(&source, 5402) .annotation(snippet::Label::info("Test annotation").span(range.clone())), ); @@ -1420,7 +1420,7 @@ mod tests { #[test] fn test_format_label() { let input = - snippet::Snippet::error("").footer(snippet::Label::error("This __is__ a title")); + snippet::Message::error("").footer(snippet::Label::error("This __is__ a title")); let output = from_display_lines(vec![ DisplayLine::Raw(DisplayRawLine::Annotation { annotation: Annotation { @@ -1455,7 +1455,7 @@ mod tests { fn test_i26() { let source = "short"; let label = "label"; - let input = snippet::Snippet::error("").slice( + let input = snippet::Message::error("").slice( snippet::Slice::new(source, 0) .annotation(snippet::Label::error(label).span(0..source.len() + 2)), ); @@ -1464,7 +1464,7 @@ mod tests { #[test] fn test_i_29() { - let snippets = snippet::Snippet::error("oops").slice( + let snippets = snippet::Message::error("oops").slice( snippet::Slice::new("First line\r\nSecond oops line", 1) .origin("") .fold(true) diff --git a/src/renderer/mod.rs b/src/renderer/mod.rs index 7046407b..ef24cfd5 100644 --- a/src/renderer/mod.rs +++ b/src/renderer/mod.rs @@ -1,9 +1,9 @@ -//! The renderer for [`Snippet`]s +//! The renderer for [`Message`]s //! //! # Example //! ``` -//! use annotate_snippets::{Renderer, Slice, Snippet}; -//! let snippet = Snippet::error("mismatched types") +//! use annotate_snippets::{Renderer, Slice, Message}; +//! let snippet = Message::error("mismatched types") //! .slice(Slice::new("Foo", 51).origin("src/format.rs")) //! .slice(Slice::new("Faa", 129).origin("src/display.rs")); //! @@ -14,14 +14,14 @@ mod display_list; mod margin; pub(crate) mod stylesheet; -use crate::snippet::Snippet; +use crate::snippet::Message; pub use anstyle::*; use display_list::DisplayList; pub use margin::Margin; use std::fmt::Display; use stylesheet::Stylesheet; -/// A renderer for [`Snippet`]s +/// A renderer for [`Message`]s #[derive(Clone)] pub struct Renderer { anonymized_line_numbers: bool, @@ -165,9 +165,9 @@ impl Renderer { } /// Render a snippet into a `Display`able object - pub fn render<'a>(&'a self, snippet: Snippet<'a>) -> impl Display + 'a { + pub fn render<'a>(&'a self, msg: Message<'a>) -> impl Display + 'a { DisplayList::new( - snippet, + msg, &self.stylesheet, self.anonymized_line_numbers, self.margin, diff --git a/src/snippet.rs b/src/snippet.rs index dde50186..61253caa 100644 --- a/src/snippet.rs +++ b/src/snippet.rs @@ -5,7 +5,7 @@ //! ``` //! use annotate_snippets::*; //! -//! Snippet::error("mismatched types") +//! Message::error("mismatched types") //! .slice(Slice::new("Foo", 51).origin("src/format.rs")) //! .slice(Slice::new("Faa", 129).origin("src/display.rs")); //! ``` @@ -13,14 +13,14 @@ use std::ops::Range; /// Primary structure provided for formatting -pub struct Snippet<'a> { +pub struct Message<'a> { pub(crate) title: Label<'a>, pub(crate) id: Option<&'a str>, pub(crate) slices: Vec>, pub(crate) footer: Vec>, } -impl<'a> Snippet<'a> { +impl<'a> Message<'a> { pub fn title(title: Label<'a>) -> Self { Self { title, diff --git a/tests/fixtures/deserialize.rs b/tests/fixtures/deserialize.rs index bd94770c..6bee4199 100644 --- a/tests/fixtures/deserialize.rs +++ b/tests/fixtures/deserialize.rs @@ -1,18 +1,18 @@ use serde::{Deserialize, Deserializer, Serialize}; use std::ops::Range; -use annotate_snippets::{renderer::Margin, Annotation, Label, Level, Renderer, Slice, Snippet}; +use annotate_snippets::{renderer::Margin, Annotation, Label, Level, Message, Renderer, Slice}; #[derive(Deserialize)] pub struct Fixture<'a> { #[serde(default)] pub renderer: RendererDef, #[serde(borrow)] - pub snippet: SnippetDef<'a>, + pub message: MessageDef<'a>, } #[derive(Deserialize)] -pub struct SnippetDef<'a> { +pub struct MessageDef<'a> { #[serde(deserialize_with = "deserialize_label")] #[serde(borrow)] pub title: Label<'a>, @@ -28,25 +28,25 @@ pub struct SnippetDef<'a> { pub slices: Vec>, } -impl<'a> From> for Snippet<'a> { - fn from(val: SnippetDef<'a>) -> Self { - let SnippetDef { +impl<'a> From> for Message<'a> { + fn from(val: MessageDef<'a>) -> Self { + let MessageDef { title, id, footer, slices, } = val; - let mut snippet = Snippet::title(title); + let mut message = Message::title(title); if let Some(id) = id { - snippet = snippet.id(id); + message = message.id(id); } - snippet = slices + message = slices .into_iter() - .fold(snippet, |snippet, slice| snippet.slice(slice)); - snippet = footer + .fold(message, |message, slice| message.slice(slice)); + message = footer .into_iter() - .fold(snippet, |snippet, label| snippet.footer(label)); - snippet + .fold(message, |message, label| message.footer(label)); + message } } diff --git a/tests/fixtures/main.rs b/tests/fixtures/main.rs index c320407a..841b3639 100644 --- a/tests/fixtures/main.rs +++ b/tests/fixtures/main.rs @@ -1,7 +1,7 @@ mod deserialize; use crate::deserialize::Fixture; -use annotate_snippets::{Renderer, Snippet}; +use annotate_snippets::{Message, Renderer}; use snapbox::data::DataFormat; use snapbox::Data; use std::error::Error; @@ -26,8 +26,8 @@ fn setup(input_path: std::path::PathBuf) -> snapbox::harness::Case { fn test(input_path: &std::path::Path) -> Result> { let src = std::fs::read_to_string(input_path)?; - let (renderer, snippet): (Renderer, Snippet<'_>) = - toml::from_str(&src).map(|a: Fixture| (a.renderer.into(), a.snippet.into()))?; - let actual = renderer.render(snippet).to_string(); + let (renderer, message): (Renderer, Message<'_>) = + toml::from_str(&src).map(|a: Fixture| (a.renderer.into(), a.message.into()))?; + let actual = renderer.render(message).to_string(); Ok(Data::from(actual).coerce_to(DataFormat::TermSvg)) } diff --git a/tests/fixtures/no-color/issue_52.toml b/tests/fixtures/no-color/issue_52.toml index 0c2378e6..749324e7 100644 --- a/tests/fixtures/no-color/issue_52.toml +++ b/tests/fixtures/no-color/issue_52.toml @@ -1,8 +1,8 @@ -[snippet.title] +[message.title] level = "Error" label = "" -[[snippet.slices]] +[[message.slices]] source = """ @@ -11,7 +11,7 @@ invalid syntax line_start = 1 origin = "path/to/error.rs" fold = true -[[snippet.slices.annotations]] +[[message.slices.annotations]] label = "error here" level = "Warning" range = [2,16] diff --git a/tests/fixtures/no-color/issue_9.toml b/tests/fixtures/no-color/issue_9.toml index c6a1c454..e534505d 100644 --- a/tests/fixtures/no-color/issue_9.toml +++ b/tests/fixtures/no-color/issue_9.toml @@ -1,28 +1,28 @@ -[snippet.title] +[message.title] label = "expected one of `.`, `;`, `?`, or an operator, found `for`" level = "Error" -[[snippet.slices]] +[[message.slices]] source = "let x = vec![1];" line_start = 4 origin = "/code/rust/src/test/ui/annotate-snippet/suggestion.rs" -[[snippet.slices.annotations]] +[[message.slices.annotations]] label = "move occurs because `x` has type `std::vec::Vec`, which does not implement the `Copy` trait" level = "Warning" range = [4, 5] -[[snippet.slices]] +[[message.slices]] source = "let y = x;" line_start = 7 -[[snippet.slices.annotations]] +[[message.slices.annotations]] label = "value moved here" level = "Warning" range = [8, 9] -[[snippet.slices]] +[[message.slices]] source = "x;" line_start = 9 -[[snippet.slices.annotations]] +[[message.slices.annotations]] label = "value used here after move" level = "Error" range = [0, 1] diff --git a/tests/fixtures/no-color/multiline_annotation.toml b/tests/fixtures/no-color/multiline_annotation.toml index 64a3513b..55b0513a 100644 --- a/tests/fixtures/no-color/multiline_annotation.toml +++ b/tests/fixtures/no-color/multiline_annotation.toml @@ -1,4 +1,4 @@ -[[snippet.slices]] +[[message.slices]] source = """ ) -> Option { for ann in annotations { @@ -26,15 +26,15 @@ source = """ line_start = 51 origin = "src/format.rs" fold = true -[[snippet.slices.annotations]] +[[message.slices.annotations]] label = "expected `std::option::Option` because of return type" level = "Warning" range = [5, 19] -[[snippet.slices.annotations]] +[[message.slices.annotations]] label = "expected enum `std::option::Option`, found ()" level = "Error" range = [22, 766] -[snippet] +[message] title = { level = "Error", label = "mismatched types" } id = "E0308" diff --git a/tests/fixtures/no-color/multiline_annotation2.toml b/tests/fixtures/no-color/multiline_annotation2.toml index 7c487ab7..b1506ef0 100644 --- a/tests/fixtures/no-color/multiline_annotation2.toml +++ b/tests/fixtures/no-color/multiline_annotation2.toml @@ -1,4 +1,4 @@ -[[snippet.slices]] +[[message.slices]] source = """ if let DisplayLine::Source { ref mut inline_marks, @@ -7,11 +7,11 @@ source = """ line_start = 139 origin = "src/display_list.rs" fold = false -[[snippet.slices.annotations]] +[[message.slices.annotations]] label = "missing fields `lineno`, `content`" level = "Error" range = [31, 128] -[snippet] +[message] title = { level = "Error", label = "pattern does not mention fields `lineno`, `content`" } id = "E0027" diff --git a/tests/fixtures/no-color/multiline_annotation3.toml b/tests/fixtures/no-color/multiline_annotation3.toml index 3850ecab..a5f0400e 100644 --- a/tests/fixtures/no-color/multiline_annotation3.toml +++ b/tests/fixtures/no-color/multiline_annotation3.toml @@ -1,4 +1,4 @@ -[[snippet.slices]] +[[message.slices]] source = """ This is an exampl e of an edge case of an annotation overflowing @@ -7,11 +7,11 @@ to exactly one character on next line. line_start = 26 origin = "foo.txt" fold = false -[[snippet.slices.annotations]] +[[message.slices.annotations]] label = "this should not be on separate lines" level = "Error" range = [11, 18] -[snippet] +[message] title = { level = "Error", label = "spacing error found" } id = "E####" diff --git a/tests/fixtures/no-color/multiple_annotations.toml b/tests/fixtures/no-color/multiple_annotations.toml index bbb2e285..1c97a27c 100644 --- a/tests/fixtures/no-color/multiple_annotations.toml +++ b/tests/fixtures/no-color/multiple_annotations.toml @@ -1,8 +1,8 @@ -[snippet.title] +[message.title] level = "Error" label = "" -[[snippet.slices]] +[[message.slices]] source = """ fn add_title_line(result: &mut Vec, main_annotation: Option<&Annotation>) { if let Some(annotation) = main_annotation { @@ -15,15 +15,15 @@ fn add_title_line(result: &mut Vec, main_annotation: Option<&Annotation> } """ line_start = 96 -[[snippet.slices.annotations]] +[[message.slices.annotations]] label = "Variable defined here" level = "Error" range = [100, 110] -[[snippet.slices.annotations]] +[[message.slices.annotations]] label = "Referenced here" level = "Error" range = [184, 194] -[[snippet.slices.annotations]] +[[message.slices.annotations]] label = "Referenced again here" level = "Error" range = [243, 253] diff --git a/tests/fixtures/no-color/one_past.toml b/tests/fixtures/no-color/one_past.toml index d7eb0416..6dbee4bf 100644 --- a/tests/fixtures/no-color/one_past.toml +++ b/tests/fixtures/no-color/one_past.toml @@ -1,12 +1,12 @@ -[snippet.title] +[message.title] label = "expected `.`, `=`" level = "Error" -[[snippet.slices]] +[[message.slices]] source = "asdf" line_start = 1 origin = "Cargo.toml" -[[snippet.slices.annotations]] +[[message.slices.annotations]] label = "" level = "Error" range = [4, 5] diff --git a/tests/fixtures/no-color/simple.toml b/tests/fixtures/no-color/simple.toml index 0d592c1e..c511ef92 100644 --- a/tests/fixtures/no-color/simple.toml +++ b/tests/fixtures/no-color/simple.toml @@ -1,18 +1,18 @@ -[[snippet.slices]] +[[message.slices]] source = """ }) for line in &self.body {""" line_start = 169 origin = "src/format_color.rs" -[[snippet.slices.annotations]] +[[message.slices.annotations]] label = "unexpected token" level = "Error" range = [20, 23] -[[snippet.slices.annotations]] +[[message.slices.annotations]] label = "expected one of `.`, `;`, `?`, or an operator here" level = "Warning" range = [10, 11] -[snippet.title] +[message.title] label = "expected one of `.`, `;`, `?`, or an operator, found `for`" level = "Error" diff --git a/tests/fixtures/no-color/strip_line.toml b/tests/fixtures/no-color/strip_line.toml index eedfcbba..02601257 100644 --- a/tests/fixtures/no-color/strip_line.toml +++ b/tests/fixtures/no-color/strip_line.toml @@ -1,13 +1,13 @@ -[snippet] +[message] title = { level = "Error", label = "mismatched types" } id = "E0308" -[[snippet.slices]] +[[message.slices]] source = " let _: () = 42;" line_start = 4 origin = "$DIR/whitespace-trimming.rs" -[[snippet.slices.annotations]] +[[message.slices.annotations]] label = "expected (), found integer" level = "Error" range = [192, 194] diff --git a/tests/fixtures/no-color/strip_line_char.toml b/tests/fixtures/no-color/strip_line_char.toml index dc51bb6b..70ee2e34 100644 --- a/tests/fixtures/no-color/strip_line_char.toml +++ b/tests/fixtures/no-color/strip_line_char.toml @@ -1,13 +1,13 @@ -[snippet] +[message] title = { level = "Error", label = "mismatched types" } id = "E0308" -[[snippet.slices]] +[[message.slices]] source = " let _: () = 42ñ" line_start = 4 origin = "$DIR/whitespace-trimming.rs" -[[snippet.slices.annotations]] +[[message.slices.annotations]] label = "expected (), found integer" level = "Error" range = [192, 194] diff --git a/tests/fixtures/no-color/strip_line_non_ws.toml b/tests/fixtures/no-color/strip_line_non_ws.toml index f56f55d1..e133bbc0 100644 --- a/tests/fixtures/no-color/strip_line_non_ws.toml +++ b/tests/fixtures/no-color/strip_line_non_ws.toml @@ -1,13 +1,13 @@ -[snippet] +[message] title = { level = "Error", label = "mismatched types" } id = "E0308" -[[snippet.slices]] +[[message.slices]] source = " let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = 42; let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = ();" line_start = 4 origin = "$DIR/non-whitespace-trimming.rs" -[[snippet.slices.annotations]] +[[message.slices.annotations]] label = "expected (), found integer" level = "Error" range = [240, 242] diff --git a/tests/formatter.rs b/tests/formatter.rs index 8f5c09fc..4b903665 100644 --- a/tests/formatter.rs +++ b/tests/formatter.rs @@ -1,8 +1,8 @@ -use annotate_snippets::{Label, Renderer, Slice, Snippet}; +use annotate_snippets::{Label, Message, Renderer, Slice}; #[test] fn test_i_29() { - let snippets = Snippet::error("oops").slice( + let snippets = Message::error("oops").slice( Slice::new("First line\r\nSecond oops line", 1) .origin("") .annotation(Label::error("oops").span(19..23)) @@ -22,7 +22,7 @@ fn test_i_29() { #[test] fn test_point_to_double_width_characters() { - let snippets = Snippet::error("").slice( + let snippets = Message::error("").slice( Slice::new("こんにちは、世界", 1) .origin("") .annotation(Label::error("world").span(12..16)), @@ -41,7 +41,7 @@ fn test_point_to_double_width_characters() { #[test] fn test_point_to_double_width_characters_across_lines() { - let snippets = Snippet::error("").slice( + let snippets = Message::error("").slice( Slice::new("おはよう\nございます", 1) .origin("") .annotation(Label::error("Good morning").span(4..15)), @@ -62,7 +62,7 @@ fn test_point_to_double_width_characters_across_lines() { #[test] fn test_point_to_double_width_characters_multiple() { - let snippets = Snippet::error("").slice( + let snippets = Message::error("").slice( Slice::new("お寿司\n食べたい🍣", 1) .origin("") .annotation(Label::error("Sushi1").span(0..6)) @@ -84,7 +84,7 @@ fn test_point_to_double_width_characters_multiple() { #[test] fn test_point_to_double_width_characters_mixed() { - let snippets = Snippet::error("").slice( + let snippets = Message::error("").slice( Slice::new("こんにちは、新しいWorld!", 1) .origin("") .annotation(Label::error("New world").span(12..23)), From 1c18950300cf8b93d92d89e9797ed0bae02c0a37 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 11 Mar 2024 15:18:05 -0500 Subject: [PATCH 05/13] fix!: Rename Slice to Snippet --- benches/simple.rs | 6 +-- examples/expected_type.rs | 6 +-- examples/footer.rs | 6 +-- examples/format.rs | 6 +-- examples/multislice.rs | 6 +-- src/renderer/display_list.rs | 48 +++++++++---------- src/renderer/mod.rs | 6 +-- src/snippet.rs | 22 ++++----- tests/fixtures/deserialize.rs | 38 ++++++++------- tests/fixtures/no-color/issue_52.toml | 4 +- tests/fixtures/no-color/issue_9.toml | 12 ++--- .../no-color/multiline_annotation.toml | 6 +-- .../no-color/multiline_annotation2.toml | 4 +- .../no-color/multiline_annotation3.toml | 4 +- .../no-color/multiple_annotations.toml | 8 ++-- tests/fixtures/no-color/one_past.toml | 4 +- tests/fixtures/no-color/simple.toml | 6 +-- tests/fixtures/no-color/strip_line.toml | 4 +- tests/fixtures/no-color/strip_line_char.toml | 4 +- .../fixtures/no-color/strip_line_non_ws.toml | 4 +- tests/formatter.rs | 22 ++++----- 21 files changed, 114 insertions(+), 112 deletions(-) diff --git a/benches/simple.rs b/benches/simple.rs index 75a852f0..a22b42de 100644 --- a/benches/simple.rs +++ b/benches/simple.rs @@ -4,7 +4,7 @@ extern crate criterion; use criterion::{black_box, Criterion}; -use annotate_snippets::{Label, Message, Renderer, Slice}; +use annotate_snippets::{Label, Message, Renderer, Snippet}; fn create_snippet(renderer: Renderer) { let source = r#") -> Option { @@ -29,8 +29,8 @@ fn create_snippet(renderer: Renderer) { _ => continue, } }"#; - let message = Message::error("mismatched types").id("E0308").slice( - Slice::new(source, 51) + let message = Message::error("mismatched types").id("E0308").snippet( + Snippet::new(source, 51) .origin("src/format.rs") .annotation( Label::warning("expected `Option` because of return type").span(5..19), diff --git a/examples/expected_type.rs b/examples/expected_type.rs index ccdb8351..d15cabb1 100644 --- a/examples/expected_type.rs +++ b/examples/expected_type.rs @@ -1,12 +1,12 @@ -use annotate_snippets::{Label, Message, Renderer, Slice}; +use annotate_snippets::{Label, Message, Renderer, Snippet}; fn main() { let source = r#" annotations: vec![SourceAnnotation { label: "expected struct `annotate_snippets::snippet::Slice`, found reference" , range: <22, 25>,"#; - let message = Message::error("expected type, found `22`").slice( - Slice::new(source, 26) + let message = Message::error("expected type, found `22`").snippet( + Snippet::new(source, 26) .origin("examples/footer.rs") .fold(true) .annotation( diff --git a/examples/footer.rs b/examples/footer.rs index 924fae2f..e82d1a99 100644 --- a/examples/footer.rs +++ b/examples/footer.rs @@ -1,10 +1,10 @@ -use annotate_snippets::{Label, Message, Renderer, Slice}; +use annotate_snippets::{Label, Message, Renderer, Snippet}; fn main() { let message = Message::error("mismatched types") .id("E0308") - .slice( - Slice::new(" slices: vec![\"A\",", 13) + .snippet( + Snippet::new(" slices: vec![\"A\",", 13) .origin("src/multislice.rs") .annotation( Label::error( diff --git a/examples/format.rs b/examples/format.rs index 85e9a82b..ad6b6dee 100644 --- a/examples/format.rs +++ b/examples/format.rs @@ -1,4 +1,4 @@ -use annotate_snippets::{Label, Message, Renderer, Slice}; +use annotate_snippets::{Label, Message, Renderer, Snippet}; fn main() { let source = r#") -> Option { @@ -23,8 +23,8 @@ fn main() { _ => continue, } }"#; - let message = Message::error("mismatched types").id("E0308").slice( - Slice::new(source, 51) + let message = Message::error("mismatched types").id("E0308").snippet( + Snippet::new(source, 51) .origin("src/format.rs") .annotation( Label::warning("expected `Option` because of return type").span(5..19), diff --git a/examples/multislice.rs b/examples/multislice.rs index 48e73726..28e8fdef 100644 --- a/examples/multislice.rs +++ b/examples/multislice.rs @@ -1,9 +1,9 @@ -use annotate_snippets::{Message, Renderer, Slice}; +use annotate_snippets::{Message, Renderer, Snippet}; fn main() { let message = Message::error("mismatched types") - .slice(Slice::new("Foo", 51).origin("src/format.rs")) - .slice(Slice::new("Faa", 129).origin("src/display.rs")); + .snippet(Snippet::new("Foo", 51).origin("src/format.rs")) + .snippet(Snippet::new("Faa", 129).origin("src/display.rs")); let renderer = Renderer::styled(); anstream::println!("{}", renderer.render(message)); diff --git a/src/renderer/display_list.rs b/src/renderer/display_list.rs index 07216c39..e02e1760 100644 --- a/src/renderer/display_list.rs +++ b/src/renderer/display_list.rs @@ -110,7 +110,7 @@ impl<'a> DisplayList<'a> { title, id, footer, - slices, + snippets, }: snippet::Message<'a>, stylesheet: &'a Stylesheet, anonymized_line_numbers: bool, @@ -120,9 +120,9 @@ impl<'a> DisplayList<'a> { body.push(format_title(title, id)); - for (idx, slice) in slices.into_iter().enumerate() { + for (idx, snippet) in snippets.into_iter().enumerate() { body.append(&mut format_slice( - slice, + snippet, idx == 0, !footer.is_empty(), margin, @@ -542,7 +542,7 @@ pub enum DisplayLine<'a> { /// A source line. #[derive(Debug, PartialEq)] pub enum DisplaySourceLine<'a> { - /// A line with the content of the Slice. + /// A line with the content of the Snippet. Content { text: &'a str, range: (usize, usize), // meta information for annotation placement. @@ -762,15 +762,15 @@ fn format_footer(footer: snippet::Label<'_>) -> Vec> { } fn format_slice( - slice: snippet::Slice<'_>, + snippet: snippet::Snippet<'_>, is_first: bool, has_footer: bool, margin: Option, ) -> Vec> { - let main_range = slice.annotations.first().map(|x| x.range.start); - let origin = slice.origin; + let main_range = snippet.annotations.first().map(|x| x.range.start); + let origin = snippet.origin; let need_empty_header = origin.is_some() || is_first; - let mut body = format_body(slice, need_empty_header, has_footer, margin); + let mut body = format_body(snippet, need_empty_header, has_footer, margin); let header = format_header(origin, main_range, &body, is_first); let mut result = vec![]; @@ -942,13 +942,13 @@ fn fold_body(mut body: Vec>) -> Vec> { } fn format_body( - slice: snippet::Slice<'_>, + snippet: snippet::Snippet<'_>, need_empty_header: bool, has_footer: bool, margin: Option, ) -> Vec> { - let source_len = slice.source.len(); - if let Some(bigger) = slice.annotations.iter().find_map(|x| { + let source_len = snippet.source.len(); + if let Some(bigger) = snippet.annotations.iter().find_map(|x| { // Allow highlighting one past the last character in the source. if source_len + 1 < x.range.end { Some(&x.range) @@ -963,7 +963,7 @@ fn format_body( } let mut body = vec![]; - let mut current_line = slice.line_start; + let mut current_line = snippet.line_start; let mut current_index = 0; let mut line_info = vec![]; @@ -972,7 +972,7 @@ fn format_body( line_end_index: usize, } - for (line, end_line) in CursorLines::new(slice.source) { + for (line, end_line) in CursorLines::new(snippet.source) { let line_length: usize = line .chars() .map(|c| unicode_width::UnicodeWidthChar::width(c).unwrap_or(0)) @@ -995,7 +995,7 @@ fn format_body( } let mut annotation_line_count = 0; - let mut annotations = slice.annotations; + let mut annotations = snippet.annotations; for ( idx, LineInfo { @@ -1143,7 +1143,7 @@ fn format_body( }); } - if slice.fold { + if snippet.fold { body = fold_body(body); } @@ -1227,7 +1227,7 @@ mod tests { let line_1 = "This is line 1"; let line_2 = "This is line 2"; let source = [line_1, line_2].join("\n"); - let input = snippet::Message::error("").slice(snippet::Slice::new(&source, 5402)); + let input = snippet::Message::error("").snippet(snippet::Snippet::new(&source, 5402)); let output = from_display_lines(vec![ DisplayLine::Raw(DisplayRawLine::Annotation { annotation: Annotation { @@ -1278,8 +1278,8 @@ mod tests { let src_1 = "This is slice 2"; let src_1_len = src_1.len(); let input = snippet::Message::error("") - .slice(snippet::Slice::new(src_0, 5402).origin("file1.rs")) - .slice(snippet::Slice::new(src_1, 2).origin("file2.rs")); + .snippet(snippet::Snippet::new(src_0, 5402).origin("file1.rs")) + .snippet(snippet::Snippet::new(src_1, 2).origin("file2.rs")); let output = from_display_lines(vec![ DisplayLine::Raw(DisplayRawLine::Annotation { annotation: Annotation { @@ -1350,8 +1350,8 @@ mod tests { let source = [line_1, line_2].join("\n"); // In line 2 let range = 22..24; - let input = snippet::Message::error("").slice( - snippet::Slice::new(&source, 5402) + let input = snippet::Message::error("").snippet( + snippet::Snippet::new(&source, 5402) .annotation(snippet::Label::info("Test annotation").span(range.clone())), ); let output = from_display_lines(vec![ @@ -1455,8 +1455,8 @@ mod tests { fn test_i26() { let source = "short"; let label = "label"; - let input = snippet::Message::error("").slice( - snippet::Slice::new(source, 0) + let input = snippet::Message::error("").snippet( + snippet::Snippet::new(source, 0) .annotation(snippet::Label::error(label).span(0..source.len() + 2)), ); let _ = DisplayList::new(input, &STYLESHEET, false, None); @@ -1464,8 +1464,8 @@ mod tests { #[test] fn test_i_29() { - let snippets = snippet::Message::error("oops").slice( - snippet::Slice::new("First line\r\nSecond oops line", 1) + let snippets = snippet::Message::error("oops").snippet( + snippet::Snippet::new("First line\r\nSecond oops line", 1) .origin("") .fold(true) .annotation(snippet::Label::error("oops").span(19..23)), diff --git a/src/renderer/mod.rs b/src/renderer/mod.rs index ef24cfd5..5ce75081 100644 --- a/src/renderer/mod.rs +++ b/src/renderer/mod.rs @@ -2,10 +2,10 @@ //! //! # Example //! ``` -//! use annotate_snippets::{Renderer, Slice, Message}; +//! use annotate_snippets::{Renderer, Snippet, Message}; //! let snippet = Message::error("mismatched types") -//! .slice(Slice::new("Foo", 51).origin("src/format.rs")) -//! .slice(Slice::new("Faa", 129).origin("src/display.rs")); +//! .snippet(Snippet::new("Foo", 51).origin("src/format.rs")) +//! .snippet(Snippet::new("Faa", 129).origin("src/display.rs")); //! //! let renderer = Renderer::styled(); //! println!("{}", renderer.render(snippet)); diff --git a/src/snippet.rs b/src/snippet.rs index 61253caa..f8832c6c 100644 --- a/src/snippet.rs +++ b/src/snippet.rs @@ -6,8 +6,8 @@ //! use annotate_snippets::*; //! //! Message::error("mismatched types") -//! .slice(Slice::new("Foo", 51).origin("src/format.rs")) -//! .slice(Slice::new("Faa", 129).origin("src/display.rs")); +//! .snippet(Snippet::new("Foo", 51).origin("src/format.rs")) +//! .snippet(Snippet::new("Faa", 129).origin("src/display.rs")); //! ``` use std::ops::Range; @@ -16,7 +16,7 @@ use std::ops::Range; pub struct Message<'a> { pub(crate) title: Label<'a>, pub(crate) id: Option<&'a str>, - pub(crate) slices: Vec>, + pub(crate) snippets: Vec>, pub(crate) footer: Vec>, } @@ -25,7 +25,7 @@ impl<'a> Message<'a> { Self { title, id: None, - slices: vec![], + snippets: vec![], footer: vec![], } } @@ -55,8 +55,8 @@ impl<'a> Message<'a> { self } - pub fn slice(mut self, slice: Slice<'a>) -> Self { - self.slices.push(slice); + pub fn snippet(mut self, slice: Snippet<'a>) -> Self { + self.snippets.push(slice); self } @@ -100,7 +100,7 @@ impl<'a> Label<'a> { self } - /// Create a [`Annotation`] with the given span for a [`Slice`] + /// Create a [`Annotation`] with the given span for a [`Snippet`] pub fn span(&self, span: Range) -> Annotation<'a> { Annotation { range: span, @@ -113,9 +113,9 @@ impl<'a> Label<'a> { /// Structure containing the slice of text to be annotated and /// basic information about the location of the slice. /// -/// One `Slice` is meant to represent a single, continuous, +/// One `Snippet` is meant to represent a single, continuous, /// slice of source code that you want to annotate. -pub struct Slice<'a> { +pub struct Snippet<'a> { pub(crate) source: &'a str, pub(crate) line_start: usize, pub(crate) origin: Option<&'a str>, @@ -123,7 +123,7 @@ pub struct Slice<'a> { pub(crate) fold: bool, } -impl<'a> Slice<'a> { +impl<'a> Snippet<'a> { pub fn new(source: &'a str, line_start: usize) -> Self { Self { source, @@ -162,7 +162,7 @@ pub enum Level { Help, } -/// An annotation for a [`Slice`]. +/// An annotation for a [`Snippet`]. /// /// This gets created by [`Label::span`]. #[derive(Debug)] diff --git a/tests/fixtures/deserialize.rs b/tests/fixtures/deserialize.rs index 6bee4199..99d14674 100644 --- a/tests/fixtures/deserialize.rs +++ b/tests/fixtures/deserialize.rs @@ -1,7 +1,7 @@ use serde::{Deserialize, Deserializer, Serialize}; use std::ops::Range; -use annotate_snippets::{renderer::Margin, Annotation, Label, Level, Message, Renderer, Slice}; +use annotate_snippets::{renderer::Margin, Annotation, Label, Level, Message, Renderer, Snippet}; #[derive(Deserialize)] pub struct Fixture<'a> { @@ -23,9 +23,9 @@ pub struct MessageDef<'a> { #[serde(default)] #[serde(borrow)] pub footer: Vec>, - #[serde(deserialize_with = "deserialize_slices")] + #[serde(deserialize_with = "deserialize_snippets")] #[serde(borrow)] - pub slices: Vec>, + pub snippets: Vec>, } impl<'a> From> for Message<'a> { @@ -34,15 +34,15 @@ impl<'a> From> for Message<'a> { title, id, footer, - slices, + snippets, } = val; let mut message = Message::title(title); if let Some(id) = id { message = message.id(id); } - message = slices + message = snippets .into_iter() - .fold(message, |message, slice| message.slice(slice)); + .fold(message, |message, snippet| message.snippet(snippet)); message = footer .into_iter() .fold(message, |message, label| message.footer(label)); @@ -81,15 +81,15 @@ where .collect()) } -fn deserialize_slices<'de, D>(deserializer: D) -> Result>, D::Error> +fn deserialize_snippets<'de, D>(deserializer: D) -> Result>, D::Error> where D: Deserializer<'de>, { #[derive(Deserialize)] struct Wrapper<'a>( - #[serde(with = "SliceDef")] + #[serde(with = "SnippetDef")] #[serde(borrow)] - SliceDef<'a>, + SnippetDef<'a>, ); let v = Vec::deserialize(deserializer)?; @@ -97,7 +97,7 @@ where } #[derive(Deserialize)] -pub struct SliceDef<'a> { +pub struct SnippetDef<'a> { #[serde(borrow)] pub source: &'a str, pub line_start: usize, @@ -110,23 +110,25 @@ pub struct SliceDef<'a> { pub fold: bool, } -impl<'a> From> for Slice<'a> { - fn from(val: SliceDef<'a>) -> Self { - let SliceDef { +impl<'a> From> for Snippet<'a> { + fn from(val: SnippetDef<'a>) -> Self { + let SnippetDef { source, line_start, origin, annotations, fold, } = val; - let mut slice = Slice::new(source, line_start).fold(fold); + let mut snippet = Snippet::new(source, line_start).fold(fold); if let Some(origin) = origin { - slice = slice.origin(origin) + snippet = snippet.origin(origin) } - slice = annotations + snippet = annotations .into_iter() - .fold(slice, |slice, annotation| slice.annotation(annotation)); - slice + .fold(snippet, |snippet, annotation| { + snippet.annotation(annotation) + }); + snippet } } diff --git a/tests/fixtures/no-color/issue_52.toml b/tests/fixtures/no-color/issue_52.toml index 749324e7..9729dcc6 100644 --- a/tests/fixtures/no-color/issue_52.toml +++ b/tests/fixtures/no-color/issue_52.toml @@ -2,7 +2,7 @@ level = "Error" label = "" -[[message.slices]] +[[message.snippets]] source = """ @@ -11,7 +11,7 @@ invalid syntax line_start = 1 origin = "path/to/error.rs" fold = true -[[message.slices.annotations]] +[[message.snippets.annotations]] label = "error here" level = "Warning" range = [2,16] diff --git a/tests/fixtures/no-color/issue_9.toml b/tests/fixtures/no-color/issue_9.toml index e534505d..04ca88cf 100644 --- a/tests/fixtures/no-color/issue_9.toml +++ b/tests/fixtures/no-color/issue_9.toml @@ -2,27 +2,27 @@ label = "expected one of `.`, `;`, `?`, or an operator, found `for`" level = "Error" -[[message.slices]] +[[message.snippets]] source = "let x = vec![1];" line_start = 4 origin = "/code/rust/src/test/ui/annotate-snippet/suggestion.rs" -[[message.slices.annotations]] +[[message.snippets.annotations]] label = "move occurs because `x` has type `std::vec::Vec`, which does not implement the `Copy` trait" level = "Warning" range = [4, 5] -[[message.slices]] +[[message.snippets]] source = "let y = x;" line_start = 7 -[[message.slices.annotations]] +[[message.snippets.annotations]] label = "value moved here" level = "Warning" range = [8, 9] -[[message.slices]] +[[message.snippets]] source = "x;" line_start = 9 -[[message.slices.annotations]] +[[message.snippets.annotations]] label = "value used here after move" level = "Error" range = [0, 1] diff --git a/tests/fixtures/no-color/multiline_annotation.toml b/tests/fixtures/no-color/multiline_annotation.toml index 55b0513a..d20716d5 100644 --- a/tests/fixtures/no-color/multiline_annotation.toml +++ b/tests/fixtures/no-color/multiline_annotation.toml @@ -1,4 +1,4 @@ -[[message.slices]] +[[message.snippets]] source = """ ) -> Option { for ann in annotations { @@ -26,11 +26,11 @@ source = """ line_start = 51 origin = "src/format.rs" fold = true -[[message.slices.annotations]] +[[message.snippets.annotations]] label = "expected `std::option::Option` because of return type" level = "Warning" range = [5, 19] -[[message.slices.annotations]] +[[message.snippets.annotations]] label = "expected enum `std::option::Option`, found ()" level = "Error" range = [22, 766] diff --git a/tests/fixtures/no-color/multiline_annotation2.toml b/tests/fixtures/no-color/multiline_annotation2.toml index b1506ef0..db7da7b4 100644 --- a/tests/fixtures/no-color/multiline_annotation2.toml +++ b/tests/fixtures/no-color/multiline_annotation2.toml @@ -1,4 +1,4 @@ -[[message.slices]] +[[message.snippets]] source = """ if let DisplayLine::Source { ref mut inline_marks, @@ -7,7 +7,7 @@ source = """ line_start = 139 origin = "src/display_list.rs" fold = false -[[message.slices.annotations]] +[[message.snippets.annotations]] label = "missing fields `lineno`, `content`" level = "Error" range = [31, 128] diff --git a/tests/fixtures/no-color/multiline_annotation3.toml b/tests/fixtures/no-color/multiline_annotation3.toml index a5f0400e..76c6bc52 100644 --- a/tests/fixtures/no-color/multiline_annotation3.toml +++ b/tests/fixtures/no-color/multiline_annotation3.toml @@ -1,4 +1,4 @@ -[[message.slices]] +[[message.snippets]] source = """ This is an exampl e of an edge case of an annotation overflowing @@ -7,7 +7,7 @@ to exactly one character on next line. line_start = 26 origin = "foo.txt" fold = false -[[message.slices.annotations]] +[[message.snippets.annotations]] label = "this should not be on separate lines" level = "Error" range = [11, 18] diff --git a/tests/fixtures/no-color/multiple_annotations.toml b/tests/fixtures/no-color/multiple_annotations.toml index 1c97a27c..d9adbfac 100644 --- a/tests/fixtures/no-color/multiple_annotations.toml +++ b/tests/fixtures/no-color/multiple_annotations.toml @@ -2,7 +2,7 @@ level = "Error" label = "" -[[message.slices]] +[[message.snippets]] source = """ fn add_title_line(result: &mut Vec, main_annotation: Option<&Annotation>) { if let Some(annotation) = main_annotation { @@ -15,15 +15,15 @@ fn add_title_line(result: &mut Vec, main_annotation: Option<&Annotation> } """ line_start = 96 -[[message.slices.annotations]] +[[message.snippets.annotations]] label = "Variable defined here" level = "Error" range = [100, 110] -[[message.slices.annotations]] +[[message.snippets.annotations]] label = "Referenced here" level = "Error" range = [184, 194] -[[message.slices.annotations]] +[[message.snippets.annotations]] label = "Referenced again here" level = "Error" range = [243, 253] diff --git a/tests/fixtures/no-color/one_past.toml b/tests/fixtures/no-color/one_past.toml index 6dbee4bf..a84fa88a 100644 --- a/tests/fixtures/no-color/one_past.toml +++ b/tests/fixtures/no-color/one_past.toml @@ -2,11 +2,11 @@ label = "expected `.`, `=`" level = "Error" -[[message.slices]] +[[message.snippets]] source = "asdf" line_start = 1 origin = "Cargo.toml" -[[message.slices.annotations]] +[[message.snippets.annotations]] label = "" level = "Error" range = [4, 5] diff --git a/tests/fixtures/no-color/simple.toml b/tests/fixtures/no-color/simple.toml index c511ef92..70a65df7 100644 --- a/tests/fixtures/no-color/simple.toml +++ b/tests/fixtures/no-color/simple.toml @@ -1,15 +1,15 @@ -[[message.slices]] +[[message.snippets]] source = """ }) for line in &self.body {""" line_start = 169 origin = "src/format_color.rs" -[[message.slices.annotations]] +[[message.snippets.annotations]] label = "unexpected token" level = "Error" range = [20, 23] -[[message.slices.annotations]] +[[message.snippets.annotations]] label = "expected one of `.`, `;`, `?`, or an operator here" level = "Warning" range = [10, 11] diff --git a/tests/fixtures/no-color/strip_line.toml b/tests/fixtures/no-color/strip_line.toml index 02601257..21eb8973 100644 --- a/tests/fixtures/no-color/strip_line.toml +++ b/tests/fixtures/no-color/strip_line.toml @@ -2,12 +2,12 @@ title = { level = "Error", label = "mismatched types" } id = "E0308" -[[message.slices]] +[[message.snippets]] source = " let _: () = 42;" line_start = 4 origin = "$DIR/whitespace-trimming.rs" -[[message.slices.annotations]] +[[message.snippets.annotations]] label = "expected (), found integer" level = "Error" range = [192, 194] diff --git a/tests/fixtures/no-color/strip_line_char.toml b/tests/fixtures/no-color/strip_line_char.toml index 70ee2e34..d7daae0f 100644 --- a/tests/fixtures/no-color/strip_line_char.toml +++ b/tests/fixtures/no-color/strip_line_char.toml @@ -2,12 +2,12 @@ title = { level = "Error", label = "mismatched types" } id = "E0308" -[[message.slices]] +[[message.snippets]] source = " let _: () = 42ñ" line_start = 4 origin = "$DIR/whitespace-trimming.rs" -[[message.slices.annotations]] +[[message.snippets.annotations]] label = "expected (), found integer" level = "Error" range = [192, 194] diff --git a/tests/fixtures/no-color/strip_line_non_ws.toml b/tests/fixtures/no-color/strip_line_non_ws.toml index e133bbc0..3afb6ba0 100644 --- a/tests/fixtures/no-color/strip_line_non_ws.toml +++ b/tests/fixtures/no-color/strip_line_non_ws.toml @@ -2,12 +2,12 @@ title = { level = "Error", label = "mismatched types" } id = "E0308" -[[message.slices]] +[[message.snippets]] source = " let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = 42; let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = ();" line_start = 4 origin = "$DIR/non-whitespace-trimming.rs" -[[message.slices.annotations]] +[[message.snippets.annotations]] label = "expected (), found integer" level = "Error" range = [240, 242] diff --git a/tests/formatter.rs b/tests/formatter.rs index 4b903665..54663cb5 100644 --- a/tests/formatter.rs +++ b/tests/formatter.rs @@ -1,9 +1,9 @@ -use annotate_snippets::{Label, Message, Renderer, Slice}; +use annotate_snippets::{Label, Message, Renderer, Snippet}; #[test] fn test_i_29() { - let snippets = Message::error("oops").slice( - Slice::new("First line\r\nSecond oops line", 1) + let snippets = Message::error("oops").snippet( + Snippet::new("First line\r\nSecond oops line", 1) .origin("") .annotation(Label::error("oops").span(19..23)) .fold(true), @@ -22,8 +22,8 @@ fn test_i_29() { #[test] fn test_point_to_double_width_characters() { - let snippets = Message::error("").slice( - Slice::new("こんにちは、世界", 1) + let snippets = Message::error("").snippet( + Snippet::new("こんにちは、世界", 1) .origin("") .annotation(Label::error("world").span(12..16)), ); @@ -41,8 +41,8 @@ fn test_point_to_double_width_characters() { #[test] fn test_point_to_double_width_characters_across_lines() { - let snippets = Message::error("").slice( - Slice::new("おはよう\nございます", 1) + let snippets = Message::error("").snippet( + Snippet::new("おはよう\nございます", 1) .origin("") .annotation(Label::error("Good morning").span(4..15)), ); @@ -62,8 +62,8 @@ fn test_point_to_double_width_characters_across_lines() { #[test] fn test_point_to_double_width_characters_multiple() { - let snippets = Message::error("").slice( - Slice::new("お寿司\n食べたい🍣", 1) + let snippets = Message::error("").snippet( + Snippet::new("お寿司\n食べたい🍣", 1) .origin("") .annotation(Label::error("Sushi1").span(0..6)) .annotation(Label::note("Sushi2").span(11..15)), @@ -84,8 +84,8 @@ fn test_point_to_double_width_characters_multiple() { #[test] fn test_point_to_double_width_characters_mixed() { - let snippets = Message::error("").slice( - Slice::new("こんにちは、新しいWorld!", 1) + let snippets = Message::error("").snippet( + Snippet::new("こんにちは、新しいWorld!", 1) .origin("") .annotation(Label::error("New world").span(12..23)), ); From 95645d15d89fe29f43c8737e4f6791f11a55b934 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 11 Mar 2024 15:19:10 -0500 Subject: [PATCH 06/13] refactor: Re-order Snippet fields according to render layout --- src/snippet.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/snippet.rs b/src/snippet.rs index f8832c6c..153fc7fb 100644 --- a/src/snippet.rs +++ b/src/snippet.rs @@ -116,19 +116,21 @@ impl<'a> Label<'a> { /// One `Snippet` is meant to represent a single, continuous, /// slice of source code that you want to annotate. pub struct Snippet<'a> { - pub(crate) source: &'a str, - pub(crate) line_start: usize, pub(crate) origin: Option<&'a str>, + pub(crate) line_start: usize, + + pub(crate) source: &'a str, pub(crate) annotations: Vec>, + pub(crate) fold: bool, } impl<'a> Snippet<'a> { pub fn new(source: &'a str, line_start: usize) -> Self { Self { - source, - line_start, origin: None, + line_start, + source, annotations: vec![], fold: false, } From 91bd796dde322ef4573c1ca707ccf99898938c07 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 11 Mar 2024 15:22:01 -0500 Subject: [PATCH 07/13] refactor: Re-order structs logically --- src/snippet.rs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/snippet.rs b/src/snippet.rs index 153fc7fb..bfe849be 100644 --- a/src/snippet.rs +++ b/src/snippet.rs @@ -152,6 +152,17 @@ impl<'a> Snippet<'a> { } } +/// An annotation for a [`Snippet`]. +/// +/// This gets created by [`Label::span`]. +#[derive(Debug)] +pub struct Annotation<'a> { + /// The byte range of the annotation in the `source` string + pub(crate) range: Range, + pub(crate) label: &'a str, + pub(crate) level: Level, +} + /// Types of annotations. #[derive(Debug, Clone, Copy, PartialEq)] pub enum Level { @@ -163,14 +174,3 @@ pub enum Level { Note, Help, } - -/// An annotation for a [`Snippet`]. -/// -/// This gets created by [`Label::span`]. -#[derive(Debug)] -pub struct Annotation<'a> { - /// The byte range of the annotation in the `source` string - pub(crate) range: Range, - pub(crate) label: &'a str, - pub(crate) level: Level, -} From b821bd397e2118a4d786231e1ca9ace28c2632a9 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 11 Mar 2024 15:28:58 -0500 Subject: [PATCH 08/13] fix!: Default Snippet::line_start --- benches/simple.rs | 3 ++- examples/expected_type.rs | 3 ++- examples/footer.rs | 3 ++- examples/format.rs | 3 ++- examples/multislice.rs | 4 ++-- src/renderer/display_list.rs | 24 ++++++++++++++++++------ src/renderer/mod.rs | 4 ++-- src/snippet.rs | 13 +++++++++---- tests/fixtures/deserialize.rs | 2 +- tests/formatter.rs | 10 +++++----- 10 files changed, 45 insertions(+), 24 deletions(-) diff --git a/benches/simple.rs b/benches/simple.rs index a22b42de..26ff1518 100644 --- a/benches/simple.rs +++ b/benches/simple.rs @@ -30,7 +30,8 @@ fn create_snippet(renderer: Renderer) { } }"#; let message = Message::error("mismatched types").id("E0308").snippet( - Snippet::new(source, 51) + Snippet::new(source) + .line_start(51) .origin("src/format.rs") .annotation( Label::warning("expected `Option` because of return type").span(5..19), diff --git a/examples/expected_type.rs b/examples/expected_type.rs index d15cabb1..73d3425c 100644 --- a/examples/expected_type.rs +++ b/examples/expected_type.rs @@ -6,7 +6,8 @@ fn main() { , range: <22, 25>,"#; let message = Message::error("expected type, found `22`").snippet( - Snippet::new(source, 26) + Snippet::new(source) + .line_start(26) .origin("examples/footer.rs") .fold(true) .annotation( diff --git a/examples/footer.rs b/examples/footer.rs index e82d1a99..482487d6 100644 --- a/examples/footer.rs +++ b/examples/footer.rs @@ -4,7 +4,8 @@ fn main() { let message = Message::error("mismatched types") .id("E0308") .snippet( - Snippet::new(" slices: vec![\"A\",", 13) + Snippet::new(" slices: vec![\"A\",") + .line_start(13) .origin("src/multislice.rs") .annotation( Label::error( diff --git a/examples/format.rs b/examples/format.rs index ad6b6dee..646eefd2 100644 --- a/examples/format.rs +++ b/examples/format.rs @@ -24,7 +24,8 @@ fn main() { } }"#; let message = Message::error("mismatched types").id("E0308").snippet( - Snippet::new(source, 51) + Snippet::new(source) + .line_start(51) .origin("src/format.rs") .annotation( Label::warning("expected `Option` because of return type").span(5..19), diff --git a/examples/multislice.rs b/examples/multislice.rs index 28e8fdef..39ca1a09 100644 --- a/examples/multislice.rs +++ b/examples/multislice.rs @@ -2,8 +2,8 @@ use annotate_snippets::{Message, Renderer, Snippet}; fn main() { let message = Message::error("mismatched types") - .snippet(Snippet::new("Foo", 51).origin("src/format.rs")) - .snippet(Snippet::new("Faa", 129).origin("src/display.rs")); + .snippet(Snippet::new("Foo").line_start(51).origin("src/format.rs")) + .snippet(Snippet::new("Faa").line_start(129).origin("src/display.rs")); let renderer = Renderer::styled(); anstream::println!("{}", renderer.render(message)); diff --git a/src/renderer/display_list.rs b/src/renderer/display_list.rs index e02e1760..13e97c85 100644 --- a/src/renderer/display_list.rs +++ b/src/renderer/display_list.rs @@ -1227,7 +1227,8 @@ mod tests { let line_1 = "This is line 1"; let line_2 = "This is line 2"; let source = [line_1, line_2].join("\n"); - let input = snippet::Message::error("").snippet(snippet::Snippet::new(&source, 5402)); + let input = + snippet::Message::error("").snippet(snippet::Snippet::new(&source).line_start(5402)); let output = from_display_lines(vec![ DisplayLine::Raw(DisplayRawLine::Annotation { annotation: Annotation { @@ -1278,8 +1279,16 @@ mod tests { let src_1 = "This is slice 2"; let src_1_len = src_1.len(); let input = snippet::Message::error("") - .snippet(snippet::Snippet::new(src_0, 5402).origin("file1.rs")) - .snippet(snippet::Snippet::new(src_1, 2).origin("file2.rs")); + .snippet( + snippet::Snippet::new(src_0) + .line_start(5402) + .origin("file1.rs"), + ) + .snippet( + snippet::Snippet::new(src_1) + .line_start(2) + .origin("file2.rs"), + ); let output = from_display_lines(vec![ DisplayLine::Raw(DisplayRawLine::Annotation { annotation: Annotation { @@ -1351,7 +1360,8 @@ mod tests { // In line 2 let range = 22..24; let input = snippet::Message::error("").snippet( - snippet::Snippet::new(&source, 5402) + snippet::Snippet::new(&source) + .line_start(5402) .annotation(snippet::Label::info("Test annotation").span(range.clone())), ); let output = from_display_lines(vec![ @@ -1456,7 +1466,8 @@ mod tests { let source = "short"; let label = "label"; let input = snippet::Message::error("").snippet( - snippet::Snippet::new(source, 0) + snippet::Snippet::new(source) + .line_start(0) .annotation(snippet::Label::error(label).span(0..source.len() + 2)), ); let _ = DisplayList::new(input, &STYLESHEET, false, None); @@ -1465,7 +1476,8 @@ mod tests { #[test] fn test_i_29() { let snippets = snippet::Message::error("oops").snippet( - snippet::Snippet::new("First line\r\nSecond oops line", 1) + snippet::Snippet::new("First line\r\nSecond oops line") + .line_start(1) .origin("") .fold(true) .annotation(snippet::Label::error("oops").span(19..23)), diff --git a/src/renderer/mod.rs b/src/renderer/mod.rs index 5ce75081..1974b6fb 100644 --- a/src/renderer/mod.rs +++ b/src/renderer/mod.rs @@ -4,8 +4,8 @@ //! ``` //! use annotate_snippets::{Renderer, Snippet, Message}; //! let snippet = Message::error("mismatched types") -//! .snippet(Snippet::new("Foo", 51).origin("src/format.rs")) -//! .snippet(Snippet::new("Faa", 129).origin("src/display.rs")); +//! .snippet(Snippet::new("Foo").line_start(51).origin("src/format.rs")) +//! .snippet(Snippet::new("Faa").line_start(129).origin("src/display.rs")); //! //! let renderer = Renderer::styled(); //! println!("{}", renderer.render(snippet)); diff --git a/src/snippet.rs b/src/snippet.rs index bfe849be..e32a5a30 100644 --- a/src/snippet.rs +++ b/src/snippet.rs @@ -6,8 +6,8 @@ //! use annotate_snippets::*; //! //! Message::error("mismatched types") -//! .snippet(Snippet::new("Foo", 51).origin("src/format.rs")) -//! .snippet(Snippet::new("Faa", 129).origin("src/display.rs")); +//! .snippet(Snippet::new("Foo").line_start(51).origin("src/format.rs")) +//! .snippet(Snippet::new("Faa").line_start(129).origin("src/display.rs")); //! ``` use std::ops::Range; @@ -126,16 +126,21 @@ pub struct Snippet<'a> { } impl<'a> Snippet<'a> { - pub fn new(source: &'a str, line_start: usize) -> Self { + pub fn new(source: &'a str) -> Self { Self { origin: None, - line_start, + line_start: 1, source, annotations: vec![], fold: false, } } + pub fn line_start(mut self, line_start: usize) -> Self { + self.line_start = line_start; + self + } + pub fn origin(mut self, origin: &'a str) -> Self { self.origin = Some(origin); self diff --git a/tests/fixtures/deserialize.rs b/tests/fixtures/deserialize.rs index 99d14674..36349a94 100644 --- a/tests/fixtures/deserialize.rs +++ b/tests/fixtures/deserialize.rs @@ -119,7 +119,7 @@ impl<'a> From> for Snippet<'a> { annotations, fold, } = val; - let mut snippet = Snippet::new(source, line_start).fold(fold); + let mut snippet = Snippet::new(source).line_start(line_start).fold(fold); if let Some(origin) = origin { snippet = snippet.origin(origin) } diff --git a/tests/formatter.rs b/tests/formatter.rs index 54663cb5..ebf7604e 100644 --- a/tests/formatter.rs +++ b/tests/formatter.rs @@ -3,7 +3,7 @@ use annotate_snippets::{Label, Message, Renderer, Snippet}; #[test] fn test_i_29() { let snippets = Message::error("oops").snippet( - Snippet::new("First line\r\nSecond oops line", 1) + Snippet::new("First line\r\nSecond oops line") .origin("") .annotation(Label::error("oops").span(19..23)) .fold(true), @@ -23,7 +23,7 @@ fn test_i_29() { #[test] fn test_point_to_double_width_characters() { let snippets = Message::error("").snippet( - Snippet::new("こんにちは、世界", 1) + Snippet::new("こんにちは、世界") .origin("") .annotation(Label::error("world").span(12..16)), ); @@ -42,7 +42,7 @@ fn test_point_to_double_width_characters() { #[test] fn test_point_to_double_width_characters_across_lines() { let snippets = Message::error("").snippet( - Snippet::new("おはよう\nございます", 1) + Snippet::new("おはよう\nございます") .origin("") .annotation(Label::error("Good morning").span(4..15)), ); @@ -63,7 +63,7 @@ fn test_point_to_double_width_characters_across_lines() { #[test] fn test_point_to_double_width_characters_multiple() { let snippets = Message::error("").snippet( - Snippet::new("お寿司\n食べたい🍣", 1) + Snippet::new("お寿司\n食べたい🍣") .origin("") .annotation(Label::error("Sushi1").span(0..6)) .annotation(Label::note("Sushi2").span(11..15)), @@ -85,7 +85,7 @@ fn test_point_to_double_width_characters_multiple() { #[test] fn test_point_to_double_width_characters_mixed() { let snippets = Message::error("").snippet( - Snippet::new("こんにちは、新しいWorld!", 1) + Snippet::new("こんにちは、新しいWorld!") .origin("") .annotation(Label::error("New world").span(12..23)), ); From 39672b2b5da27ac820af3a12db78bf8995c82e5f Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 11 Mar 2024 15:50:02 -0500 Subject: [PATCH 09/13] fix!: Move Message contruction to Level This fits with the session-type / type-state style builder and allows Diagnoatic adapters, like rustc, to leverage it vs the hard-coded functions. --- benches/simple.rs | 4 +- examples/expected_type.rs | 4 +- examples/footer.rs | 5 +- examples/format.rs | 4 +- examples/multislice.rs | 5 +- src/renderer/display_list.rs | 32 ++++++++----- src/renderer/mod.rs | 4 +- src/snippet.rs | 48 +++++++------------ tests/fixtures/deserialize.rs | 22 ++------- tests/fixtures/no-color/issue_52.toml | 4 +- tests/fixtures/no-color/issue_9.toml | 4 +- .../no-color/multiline_annotation.toml | 9 ++-- .../no-color/multiline_annotation2.toml | 9 ++-- .../no-color/multiline_annotation3.toml | 9 ++-- .../no-color/multiple_annotations.toml | 4 +- tests/fixtures/no-color/one_past.toml | 4 +- tests/fixtures/no-color/simple.toml | 7 +-- tests/fixtures/no-color/strip_line.toml | 3 +- tests/fixtures/no-color/strip_line_char.toml | 3 +- .../fixtures/no-color/strip_line_non_ws.toml | 3 +- tests/formatter.rs | 12 ++--- 21 files changed, 96 insertions(+), 103 deletions(-) diff --git a/benches/simple.rs b/benches/simple.rs index 26ff1518..59d02a08 100644 --- a/benches/simple.rs +++ b/benches/simple.rs @@ -4,7 +4,7 @@ extern crate criterion; use criterion::{black_box, Criterion}; -use annotate_snippets::{Label, Message, Renderer, Snippet}; +use annotate_snippets::{Label, Level, Renderer, Snippet}; fn create_snippet(renderer: Renderer) { let source = r#") -> Option { @@ -29,7 +29,7 @@ fn create_snippet(renderer: Renderer) { _ => continue, } }"#; - let message = Message::error("mismatched types").id("E0308").snippet( + let message = Level::Error.title("mismatched types").id("E0308").snippet( Snippet::new(source) .line_start(51) .origin("src/format.rs") diff --git a/examples/expected_type.rs b/examples/expected_type.rs index 73d3425c..3938d5f5 100644 --- a/examples/expected_type.rs +++ b/examples/expected_type.rs @@ -1,11 +1,11 @@ -use annotate_snippets::{Label, Message, Renderer, Snippet}; +use annotate_snippets::{Label, Level, Renderer, Snippet}; fn main() { let source = r#" annotations: vec![SourceAnnotation { label: "expected struct `annotate_snippets::snippet::Slice`, found reference" , range: <22, 25>,"#; - let message = Message::error("expected type, found `22`").snippet( + let message = Level::Error.title("expected type, found `22`").snippet( Snippet::new(source) .line_start(26) .origin("examples/footer.rs") diff --git a/examples/footer.rs b/examples/footer.rs index 482487d6..e873546f 100644 --- a/examples/footer.rs +++ b/examples/footer.rs @@ -1,7 +1,8 @@ -use annotate_snippets::{Label, Message, Renderer, Snippet}; +use annotate_snippets::{Label, Level, Renderer, Snippet}; fn main() { - let message = Message::error("mismatched types") + let message = Level::Error + .title("mismatched types") .id("E0308") .snippet( Snippet::new(" slices: vec![\"A\",") diff --git a/examples/format.rs b/examples/format.rs index 646eefd2..7d19b87a 100644 --- a/examples/format.rs +++ b/examples/format.rs @@ -1,4 +1,4 @@ -use annotate_snippets::{Label, Message, Renderer, Snippet}; +use annotate_snippets::{Label, Level, Renderer, Snippet}; fn main() { let source = r#") -> Option { @@ -23,7 +23,7 @@ fn main() { _ => continue, } }"#; - let message = Message::error("mismatched types").id("E0308").snippet( + let message = Level::Error.title("mismatched types").id("E0308").snippet( Snippet::new(source) .line_start(51) .origin("src/format.rs") diff --git a/examples/multislice.rs b/examples/multislice.rs index 39ca1a09..38afbd48 100644 --- a/examples/multislice.rs +++ b/examples/multislice.rs @@ -1,7 +1,8 @@ -use annotate_snippets::{Message, Renderer, Snippet}; +use annotate_snippets::{Level, Renderer, Snippet}; fn main() { - let message = Message::error("mismatched types") + let message = Level::Error + .title("mismatched types") .snippet(Snippet::new("Foo").line_start(51).origin("src/format.rs")) .snippet(Snippet::new("Faa").line_start(129).origin("src/display.rs")); diff --git a/src/renderer/display_list.rs b/src/renderer/display_list.rs index 13e97c85..2ba7f832 100644 --- a/src/renderer/display_list.rs +++ b/src/renderer/display_list.rs @@ -107,8 +107,9 @@ impl<'a> DisplayList<'a> { pub(crate) fn new( snippet::Message { - title, + level, id, + title, footer, snippets, }: snippet::Message<'a>, @@ -118,7 +119,13 @@ impl<'a> DisplayList<'a> { ) -> DisplayList<'a> { let mut body = vec![]; - body.push(format_title(title, id)); + body.push(format_title( + snippet::Label { + level, + label: title, + }, + id, + )); for (idx, snippet) in snippets.into_iter().enumerate() { body.append(&mut format_slice( @@ -1206,7 +1213,7 @@ mod tests { #[test] fn test_format_title() { - let input = snippet::Message::error("This is a title").id("E0001"); + let input = snippet::Level::Error.title("This is a title").id("E0001"); let output = from_display_lines(vec![DisplayLine::Raw(DisplayRawLine::Annotation { annotation: Annotation { annotation_type: DisplayAnnotationType::Error, @@ -1227,8 +1234,9 @@ mod tests { let line_1 = "This is line 1"; let line_2 = "This is line 2"; let source = [line_1, line_2].join("\n"); - let input = - snippet::Message::error("").snippet(snippet::Snippet::new(&source).line_start(5402)); + let input = snippet::Level::Error + .title("") + .snippet(snippet::Snippet::new(&source).line_start(5402)); let output = from_display_lines(vec![ DisplayLine::Raw(DisplayRawLine::Annotation { annotation: Annotation { @@ -1278,7 +1286,8 @@ mod tests { let src_0_len = src_0.len(); let src_1 = "This is slice 2"; let src_1_len = src_1.len(); - let input = snippet::Message::error("") + let input = snippet::Level::Error + .title("") .snippet( snippet::Snippet::new(src_0) .line_start(5402) @@ -1359,7 +1368,7 @@ mod tests { let source = [line_1, line_2].join("\n"); // In line 2 let range = 22..24; - let input = snippet::Message::error("").snippet( + let input = snippet::Level::Error.title("").snippet( snippet::Snippet::new(&source) .line_start(5402) .annotation(snippet::Label::info("Test annotation").span(range.clone())), @@ -1429,8 +1438,9 @@ mod tests { #[test] fn test_format_label() { - let input = - snippet::Message::error("").footer(snippet::Label::error("This __is__ a title")); + let input = snippet::Level::Error + .title("") + .footer(snippet::Label::error("This __is__ a title")); let output = from_display_lines(vec![ DisplayLine::Raw(DisplayRawLine::Annotation { annotation: Annotation { @@ -1465,7 +1475,7 @@ mod tests { fn test_i26() { let source = "short"; let label = "label"; - let input = snippet::Message::error("").snippet( + let input = snippet::Level::Error.title("").snippet( snippet::Snippet::new(source) .line_start(0) .annotation(snippet::Label::error(label).span(0..source.len() + 2)), @@ -1475,7 +1485,7 @@ mod tests { #[test] fn test_i_29() { - let snippets = snippet::Message::error("oops").snippet( + let snippets = snippet::Level::Error.title("oops").snippet( snippet::Snippet::new("First line\r\nSecond oops line") .line_start(1) .origin("") diff --git a/src/renderer/mod.rs b/src/renderer/mod.rs index 1974b6fb..29f3dae7 100644 --- a/src/renderer/mod.rs +++ b/src/renderer/mod.rs @@ -2,8 +2,8 @@ //! //! # Example //! ``` -//! use annotate_snippets::{Renderer, Snippet, Message}; -//! let snippet = Message::error("mismatched types") +//! use annotate_snippets::{Renderer, Snippet, Level}; +//! let snippet = Level::Error.title("mismatched types") //! .snippet(Snippet::new("Foo").line_start(51).origin("src/format.rs")) //! .snippet(Snippet::new("Faa").line_start(129).origin("src/display.rs")); //! diff --git a/src/snippet.rs b/src/snippet.rs index e32a5a30..d2906d49 100644 --- a/src/snippet.rs +++ b/src/snippet.rs @@ -5,7 +5,7 @@ //! ``` //! use annotate_snippets::*; //! -//! Message::error("mismatched types") +//! Level::Error.title("mismatched types") //! .snippet(Snippet::new("Foo").line_start(51).origin("src/format.rs")) //! .snippet(Snippet::new("Faa").line_start(129).origin("src/display.rs")); //! ``` @@ -13,43 +13,17 @@ use std::ops::Range; /// Primary structure provided for formatting +/// +/// See [`Level::title`] to create a [`Message`] pub struct Message<'a> { - pub(crate) title: Label<'a>, + pub(crate) level: Level, pub(crate) id: Option<&'a str>, + pub(crate) title: &'a str, pub(crate) snippets: Vec>, pub(crate) footer: Vec>, } impl<'a> Message<'a> { - pub fn title(title: Label<'a>) -> Self { - Self { - title, - id: None, - snippets: vec![], - footer: vec![], - } - } - - pub fn error(title: &'a str) -> Self { - Self::title(Label::error(title)) - } - - pub fn warning(title: &'a str) -> Self { - Self::title(Label::warning(title)) - } - - pub fn info(title: &'a str) -> Self { - Self::title(Label::info(title)) - } - - pub fn note(title: &'a str) -> Self { - Self::title(Label::note(title)) - } - - pub fn help(title: &'a str) -> Self { - Self::title(Label::help(title)) - } - pub fn id(mut self, id: &'a str) -> Self { self.id = Some(id); self @@ -179,3 +153,15 @@ pub enum Level { Note, Help, } + +impl Level { + pub fn title(self, title: &str) -> Message<'_> { + Message { + level: self, + id: None, + title, + snippets: vec![], + footer: vec![], + } + } +} diff --git a/tests/fixtures/deserialize.rs b/tests/fixtures/deserialize.rs index 36349a94..7e64945a 100644 --- a/tests/fixtures/deserialize.rs +++ b/tests/fixtures/deserialize.rs @@ -13,9 +13,10 @@ pub struct Fixture<'a> { #[derive(Deserialize)] pub struct MessageDef<'a> { - #[serde(deserialize_with = "deserialize_label")] + #[serde(with = "LevelDef")] + pub level: Level, #[serde(borrow)] - pub title: Label<'a>, + pub title: &'a str, #[serde(default)] #[serde(borrow)] pub id: Option<&'a str>, @@ -31,12 +32,13 @@ pub struct MessageDef<'a> { impl<'a> From> for Message<'a> { fn from(val: MessageDef<'a>) -> Self { let MessageDef { + level, title, id, footer, snippets, } = val; - let mut message = Message::title(title); + let mut message = level.title(title); if let Some(id) = id { message = message.id(id); } @@ -50,20 +52,6 @@ impl<'a> From> for Message<'a> { } } -fn deserialize_label<'de, D>(deserializer: D) -> Result, D::Error> -where - D: Deserializer<'de>, -{ - #[derive(Deserialize)] - struct Wrapper<'a>( - #[serde(with = "LabelDef")] - #[serde(borrow)] - LabelDef<'a>, - ); - - Wrapper::deserialize(deserializer).map(|Wrapper(label)| Label::new(label.level, label.label)) -} - fn deserialize_labels<'de, D>(deserializer: D) -> Result>, D::Error> where D: Deserializer<'de>, diff --git a/tests/fixtures/no-color/issue_52.toml b/tests/fixtures/no-color/issue_52.toml index 9729dcc6..1e81a713 100644 --- a/tests/fixtures/no-color/issue_52.toml +++ b/tests/fixtures/no-color/issue_52.toml @@ -1,6 +1,6 @@ -[message.title] +[message] level = "Error" -label = "" +title = "" [[message.snippets]] source = """ diff --git a/tests/fixtures/no-color/issue_9.toml b/tests/fixtures/no-color/issue_9.toml index 04ca88cf..1f35243c 100644 --- a/tests/fixtures/no-color/issue_9.toml +++ b/tests/fixtures/no-color/issue_9.toml @@ -1,6 +1,6 @@ -[message.title] -label = "expected one of `.`, `;`, `?`, or an operator, found `for`" +[message] level = "Error" +title = "expected one of `.`, `;`, `?`, or an operator, found `for`" [[message.snippets]] source = "let x = vec![1];" diff --git a/tests/fixtures/no-color/multiline_annotation.toml b/tests/fixtures/no-color/multiline_annotation.toml index d20716d5..09fc7d44 100644 --- a/tests/fixtures/no-color/multiline_annotation.toml +++ b/tests/fixtures/no-color/multiline_annotation.toml @@ -1,3 +1,8 @@ +[message] +level = "Error" +id = "E0308" +title = "mismatched types" + [[message.snippets]] source = """ ) -> Option { @@ -34,7 +39,3 @@ range = [5, 19] label = "expected enum `std::option::Option`, found ()" level = "Error" range = [22, 766] - -[message] -title = { level = "Error", label = "mismatched types" } -id = "E0308" diff --git a/tests/fixtures/no-color/multiline_annotation2.toml b/tests/fixtures/no-color/multiline_annotation2.toml index db7da7b4..671b5344 100644 --- a/tests/fixtures/no-color/multiline_annotation2.toml +++ b/tests/fixtures/no-color/multiline_annotation2.toml @@ -1,3 +1,8 @@ +[message] +level = "Error" +id = "E0027" +title = "pattern does not mention fields `lineno`, `content`" + [[message.snippets]] source = """ if let DisplayLine::Source { @@ -11,7 +16,3 @@ fold = false label = "missing fields `lineno`, `content`" level = "Error" range = [31, 128] - -[message] -title = { level = "Error", label = "pattern does not mention fields `lineno`, `content`" } -id = "E0027" diff --git a/tests/fixtures/no-color/multiline_annotation3.toml b/tests/fixtures/no-color/multiline_annotation3.toml index 76c6bc52..dd853324 100644 --- a/tests/fixtures/no-color/multiline_annotation3.toml +++ b/tests/fixtures/no-color/multiline_annotation3.toml @@ -1,3 +1,8 @@ +[message] +level = "Error" +id = "E####" +title = "spacing error found" + [[message.snippets]] source = """ This is an exampl @@ -11,7 +16,3 @@ fold = false label = "this should not be on separate lines" level = "Error" range = [11, 18] - -[message] -title = { level = "Error", label = "spacing error found" } -id = "E####" diff --git a/tests/fixtures/no-color/multiple_annotations.toml b/tests/fixtures/no-color/multiple_annotations.toml index d9adbfac..842b137e 100644 --- a/tests/fixtures/no-color/multiple_annotations.toml +++ b/tests/fixtures/no-color/multiple_annotations.toml @@ -1,6 +1,6 @@ -[message.title] +[message] level = "Error" -label = "" +title = "" [[message.snippets]] source = """ diff --git a/tests/fixtures/no-color/one_past.toml b/tests/fixtures/no-color/one_past.toml index a84fa88a..b681c293 100644 --- a/tests/fixtures/no-color/one_past.toml +++ b/tests/fixtures/no-color/one_past.toml @@ -1,6 +1,6 @@ -[message.title] -label = "expected `.`, `=`" +[message] level = "Error" +title = "expected `.`, `=`" [[message.snippets]] source = "asdf" diff --git a/tests/fixtures/no-color/simple.toml b/tests/fixtures/no-color/simple.toml index 70a65df7..76b5bac6 100644 --- a/tests/fixtures/no-color/simple.toml +++ b/tests/fixtures/no-color/simple.toml @@ -1,3 +1,7 @@ +[message] +level = "Error" +title = "expected one of `.`, `;`, `?`, or an operator, found `for`" + [[message.snippets]] source = """ }) @@ -13,6 +17,3 @@ range = [20, 23] label = "expected one of `.`, `;`, `?`, or an operator here" level = "Warning" range = [10, 11] -[message.title] -label = "expected one of `.`, `;`, `?`, or an operator, found `for`" -level = "Error" diff --git a/tests/fixtures/no-color/strip_line.toml b/tests/fixtures/no-color/strip_line.toml index 21eb8973..d44024bd 100644 --- a/tests/fixtures/no-color/strip_line.toml +++ b/tests/fixtures/no-color/strip_line.toml @@ -1,6 +1,7 @@ [message] -title = { level = "Error", label = "mismatched types" } +level = "Error" id = "E0308" +title = "mismatched types" [[message.snippets]] source = " let _: () = 42;" diff --git a/tests/fixtures/no-color/strip_line_char.toml b/tests/fixtures/no-color/strip_line_char.toml index d7daae0f..e3f74822 100644 --- a/tests/fixtures/no-color/strip_line_char.toml +++ b/tests/fixtures/no-color/strip_line_char.toml @@ -1,6 +1,7 @@ [message] -title = { level = "Error", label = "mismatched types" } +level = "Error" id = "E0308" +title = "mismatched types" [[message.snippets]] source = " let _: () = 42ñ" diff --git a/tests/fixtures/no-color/strip_line_non_ws.toml b/tests/fixtures/no-color/strip_line_non_ws.toml index 3afb6ba0..2985177b 100644 --- a/tests/fixtures/no-color/strip_line_non_ws.toml +++ b/tests/fixtures/no-color/strip_line_non_ws.toml @@ -1,6 +1,7 @@ [message] -title = { level = "Error", label = "mismatched types" } +level = "Error" id = "E0308" +title = "mismatched types" [[message.snippets]] source = " let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = 42; let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = ();" diff --git a/tests/formatter.rs b/tests/formatter.rs index ebf7604e..3224175a 100644 --- a/tests/formatter.rs +++ b/tests/formatter.rs @@ -1,8 +1,8 @@ -use annotate_snippets::{Label, Message, Renderer, Snippet}; +use annotate_snippets::{Label, Level, Renderer, Snippet}; #[test] fn test_i_29() { - let snippets = Message::error("oops").snippet( + let snippets = Level::Error.title("oops").snippet( Snippet::new("First line\r\nSecond oops line") .origin("") .annotation(Label::error("oops").span(19..23)) @@ -22,7 +22,7 @@ fn test_i_29() { #[test] fn test_point_to_double_width_characters() { - let snippets = Message::error("").snippet( + let snippets = Level::Error.title("").snippet( Snippet::new("こんにちは、世界") .origin("") .annotation(Label::error("world").span(12..16)), @@ -41,7 +41,7 @@ fn test_point_to_double_width_characters() { #[test] fn test_point_to_double_width_characters_across_lines() { - let snippets = Message::error("").snippet( + let snippets = Level::Error.title("").snippet( Snippet::new("おはよう\nございます") .origin("") .annotation(Label::error("Good morning").span(4..15)), @@ -62,7 +62,7 @@ fn test_point_to_double_width_characters_across_lines() { #[test] fn test_point_to_double_width_characters_multiple() { - let snippets = Message::error("").snippet( + let snippets = Level::Error.title("").snippet( Snippet::new("お寿司\n食べたい🍣") .origin("") .annotation(Label::error("Sushi1").span(0..6)) @@ -84,7 +84,7 @@ fn test_point_to_double_width_characters_multiple() { #[test] fn test_point_to_double_width_characters_mixed() { - let snippets = Message::error("").snippet( + let snippets = Level::Error.title("").snippet( Snippet::new("こんにちは、新しいWorld!") .origin("") .annotation(Label::error("New world").span(12..23)), From 86823c4b3aedf5cad62b871d33b6aa6be56a1323 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 11 Mar 2024 15:59:45 -0500 Subject: [PATCH 10/13] fix!: Rename Snippet::new to Snippet::source --- benches/simple.rs | 2 +- examples/expected_type.rs | 2 +- examples/footer.rs | 2 +- examples/format.rs | 2 +- examples/multislice.rs | 12 ++++++++++-- src/renderer/display_list.rs | 12 ++++++------ src/renderer/mod.rs | 4 ++-- src/snippet.rs | 6 +++--- tests/fixtures/deserialize.rs | 2 +- tests/formatter.rs | 10 +++++----- 10 files changed, 31 insertions(+), 23 deletions(-) diff --git a/benches/simple.rs b/benches/simple.rs index 59d02a08..ab573024 100644 --- a/benches/simple.rs +++ b/benches/simple.rs @@ -30,7 +30,7 @@ fn create_snippet(renderer: Renderer) { } }"#; let message = Level::Error.title("mismatched types").id("E0308").snippet( - Snippet::new(source) + Snippet::source(source) .line_start(51) .origin("src/format.rs") .annotation( diff --git a/examples/expected_type.rs b/examples/expected_type.rs index 3938d5f5..942728f8 100644 --- a/examples/expected_type.rs +++ b/examples/expected_type.rs @@ -6,7 +6,7 @@ fn main() { , range: <22, 25>,"#; let message = Level::Error.title("expected type, found `22`").snippet( - Snippet::new(source) + Snippet::source(source) .line_start(26) .origin("examples/footer.rs") .fold(true) diff --git a/examples/footer.rs b/examples/footer.rs index e873546f..858f811a 100644 --- a/examples/footer.rs +++ b/examples/footer.rs @@ -5,7 +5,7 @@ fn main() { .title("mismatched types") .id("E0308") .snippet( - Snippet::new(" slices: vec![\"A\",") + Snippet::source(" slices: vec![\"A\",") .line_start(13) .origin("src/multislice.rs") .annotation( diff --git a/examples/format.rs b/examples/format.rs index 7d19b87a..34a76f40 100644 --- a/examples/format.rs +++ b/examples/format.rs @@ -24,7 +24,7 @@ fn main() { } }"#; let message = Level::Error.title("mismatched types").id("E0308").snippet( - Snippet::new(source) + Snippet::source(source) .line_start(51) .origin("src/format.rs") .annotation( diff --git a/examples/multislice.rs b/examples/multislice.rs index 38afbd48..ea31bbd0 100644 --- a/examples/multislice.rs +++ b/examples/multislice.rs @@ -3,8 +3,16 @@ use annotate_snippets::{Level, Renderer, Snippet}; fn main() { let message = Level::Error .title("mismatched types") - .snippet(Snippet::new("Foo").line_start(51).origin("src/format.rs")) - .snippet(Snippet::new("Faa").line_start(129).origin("src/display.rs")); + .snippet( + Snippet::source("Foo") + .line_start(51) + .origin("src/format.rs"), + ) + .snippet( + Snippet::source("Faa") + .line_start(129) + .origin("src/display.rs"), + ); let renderer = Renderer::styled(); anstream::println!("{}", renderer.render(message)); diff --git a/src/renderer/display_list.rs b/src/renderer/display_list.rs index 2ba7f832..b4c50a90 100644 --- a/src/renderer/display_list.rs +++ b/src/renderer/display_list.rs @@ -1236,7 +1236,7 @@ mod tests { let source = [line_1, line_2].join("\n"); let input = snippet::Level::Error .title("") - .snippet(snippet::Snippet::new(&source).line_start(5402)); + .snippet(snippet::Snippet::source(&source).line_start(5402)); let output = from_display_lines(vec![ DisplayLine::Raw(DisplayRawLine::Annotation { annotation: Annotation { @@ -1289,12 +1289,12 @@ mod tests { let input = snippet::Level::Error .title("") .snippet( - snippet::Snippet::new(src_0) + snippet::Snippet::source(src_0) .line_start(5402) .origin("file1.rs"), ) .snippet( - snippet::Snippet::new(src_1) + snippet::Snippet::source(src_1) .line_start(2) .origin("file2.rs"), ); @@ -1369,7 +1369,7 @@ mod tests { // In line 2 let range = 22..24; let input = snippet::Level::Error.title("").snippet( - snippet::Snippet::new(&source) + snippet::Snippet::source(&source) .line_start(5402) .annotation(snippet::Label::info("Test annotation").span(range.clone())), ); @@ -1476,7 +1476,7 @@ mod tests { let source = "short"; let label = "label"; let input = snippet::Level::Error.title("").snippet( - snippet::Snippet::new(source) + snippet::Snippet::source(source) .line_start(0) .annotation(snippet::Label::error(label).span(0..source.len() + 2)), ); @@ -1486,7 +1486,7 @@ mod tests { #[test] fn test_i_29() { let snippets = snippet::Level::Error.title("oops").snippet( - snippet::Snippet::new("First line\r\nSecond oops line") + snippet::Snippet::source("First line\r\nSecond oops line") .line_start(1) .origin("") .fold(true) diff --git a/src/renderer/mod.rs b/src/renderer/mod.rs index 29f3dae7..5f9394d5 100644 --- a/src/renderer/mod.rs +++ b/src/renderer/mod.rs @@ -4,8 +4,8 @@ //! ``` //! use annotate_snippets::{Renderer, Snippet, Level}; //! let snippet = Level::Error.title("mismatched types") -//! .snippet(Snippet::new("Foo").line_start(51).origin("src/format.rs")) -//! .snippet(Snippet::new("Faa").line_start(129).origin("src/display.rs")); +//! .snippet(Snippet::source("Foo").line_start(51).origin("src/format.rs")) +//! .snippet(Snippet::source("Faa").line_start(129).origin("src/display.rs")); //! //! let renderer = Renderer::styled(); //! println!("{}", renderer.render(snippet)); diff --git a/src/snippet.rs b/src/snippet.rs index d2906d49..e0a05ea8 100644 --- a/src/snippet.rs +++ b/src/snippet.rs @@ -6,8 +6,8 @@ //! use annotate_snippets::*; //! //! Level::Error.title("mismatched types") -//! .snippet(Snippet::new("Foo").line_start(51).origin("src/format.rs")) -//! .snippet(Snippet::new("Faa").line_start(129).origin("src/display.rs")); +//! .snippet(Snippet::source("Foo").line_start(51).origin("src/format.rs")) +//! .snippet(Snippet::source("Faa").line_start(129).origin("src/display.rs")); //! ``` use std::ops::Range; @@ -100,7 +100,7 @@ pub struct Snippet<'a> { } impl<'a> Snippet<'a> { - pub fn new(source: &'a str) -> Self { + pub fn source(source: &'a str) -> Self { Self { origin: None, line_start: 1, diff --git a/tests/fixtures/deserialize.rs b/tests/fixtures/deserialize.rs index 7e64945a..f5bb411f 100644 --- a/tests/fixtures/deserialize.rs +++ b/tests/fixtures/deserialize.rs @@ -107,7 +107,7 @@ impl<'a> From> for Snippet<'a> { annotations, fold, } = val; - let mut snippet = Snippet::new(source).line_start(line_start).fold(fold); + let mut snippet = Snippet::source(source).line_start(line_start).fold(fold); if let Some(origin) = origin { snippet = snippet.origin(origin) } diff --git a/tests/formatter.rs b/tests/formatter.rs index 3224175a..c905b1cf 100644 --- a/tests/formatter.rs +++ b/tests/formatter.rs @@ -3,7 +3,7 @@ use annotate_snippets::{Label, Level, Renderer, Snippet}; #[test] fn test_i_29() { let snippets = Level::Error.title("oops").snippet( - Snippet::new("First line\r\nSecond oops line") + Snippet::source("First line\r\nSecond oops line") .origin("") .annotation(Label::error("oops").span(19..23)) .fold(true), @@ -23,7 +23,7 @@ fn test_i_29() { #[test] fn test_point_to_double_width_characters() { let snippets = Level::Error.title("").snippet( - Snippet::new("こんにちは、世界") + Snippet::source("こんにちは、世界") .origin("") .annotation(Label::error("world").span(12..16)), ); @@ -42,7 +42,7 @@ fn test_point_to_double_width_characters() { #[test] fn test_point_to_double_width_characters_across_lines() { let snippets = Level::Error.title("").snippet( - Snippet::new("おはよう\nございます") + Snippet::source("おはよう\nございます") .origin("") .annotation(Label::error("Good morning").span(4..15)), ); @@ -63,7 +63,7 @@ fn test_point_to_double_width_characters_across_lines() { #[test] fn test_point_to_double_width_characters_multiple() { let snippets = Level::Error.title("").snippet( - Snippet::new("お寿司\n食べたい🍣") + Snippet::source("お寿司\n食べたい🍣") .origin("") .annotation(Label::error("Sushi1").span(0..6)) .annotation(Label::note("Sushi2").span(11..15)), @@ -85,7 +85,7 @@ fn test_point_to_double_width_characters_multiple() { #[test] fn test_point_to_double_width_characters_mixed() { let snippets = Level::Error.title("").snippet( - Snippet::new("こんにちは、新しいWorld!") + Snippet::source("こんにちは、新しいWorld!") .origin("") .annotation(Label::error("New world").span(12..23)), ); From c821084068a1acd2688b6c8d0b3423e143d359e2 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 11 Mar 2024 16:13:40 -0500 Subject: [PATCH 11/13] fix!: Make Annotation labels optional --- benches/simple.rs | 12 +++++++++--- examples/expected_type.rs | 11 +++++------ examples/footer.rs | 30 ++++++++++++++---------------- examples/format.rs | 12 +++++++++--- src/renderer/display_list.rs | 14 +++++++++----- src/snippet.rs | 29 ++++++++++++++++++----------- tests/fixtures/deserialize.rs | 2 +- tests/formatter.rs | 14 +++++++------- 8 files changed, 72 insertions(+), 52 deletions(-) diff --git a/benches/simple.rs b/benches/simple.rs index ab573024..fccb70be 100644 --- a/benches/simple.rs +++ b/benches/simple.rs @@ -4,7 +4,7 @@ extern crate criterion; use criterion::{black_box, Criterion}; -use annotate_snippets::{Label, Level, Renderer, Snippet}; +use annotate_snippets::{Level, Renderer, Snippet}; fn create_snippet(renderer: Renderer) { let source = r#") -> Option { @@ -34,9 +34,15 @@ fn create_snippet(renderer: Renderer) { .line_start(51) .origin("src/format.rs") .annotation( - Label::warning("expected `Option` because of return type").span(5..19), + Level::Warning + .span(5..19) + .label("expected `Option` because of return type"), ) - .annotation(Label::error("expected enum `std::option::Option`").span(26..724)), + .annotation( + Level::Error + .span(26..724) + .label("expected enum `std::option::Option`"), + ), ); let _result = renderer.render(message).to_string(); diff --git a/examples/expected_type.rs b/examples/expected_type.rs index 942728f8..0184deeb 100644 --- a/examples/expected_type.rs +++ b/examples/expected_type.rs @@ -1,4 +1,4 @@ -use annotate_snippets::{Label, Level, Renderer, Snippet}; +use annotate_snippets::{Level, Renderer, Snippet}; fn main() { let source = r#" annotations: vec![SourceAnnotation { @@ -11,12 +11,11 @@ fn main() { .origin("examples/footer.rs") .fold(true) .annotation( - Label::error( - "expected struct `annotate_snippets::snippet::Slice`, found reference", - ) - .span(193..195), + Level::Error + .span(193..195) + .label("expected struct `annotate_snippets::snippet::Slice`, found reference"), ) - .annotation(Label::info("while parsing this struct").span(34..50)), + .annotation(Level::Info.span(34..50).label("while parsing this struct")), ); let renderer = Renderer::styled(); diff --git a/examples/footer.rs b/examples/footer.rs index 858f811a..8b4d0780 100644 --- a/examples/footer.rs +++ b/examples/footer.rs @@ -1,23 +1,21 @@ use annotate_snippets::{Label, Level, Renderer, Snippet}; fn main() { - let message = Level::Error - .title("mismatched types") - .id("E0308") - .snippet( - Snippet::source(" slices: vec![\"A\",") - .line_start(13) - .origin("src/multislice.rs") - .annotation( - Label::error( + let message = + Level::Error + .title("mismatched types") + .id("E0308") + .snippet( + Snippet::source(" slices: vec![\"A\",") + .line_start(13) + .origin("src/multislice.rs") + .annotation(Level::Error.span(21..24).label( "expected struct `annotate_snippets::snippet::Slice`, found reference", - ) - .span(21..24), - ), - ) - .footer(Label::note( - "expected type: `snippet::Annotation`\n found type: `__&__snippet::Annotation`", - )); + )), + ) + .footer(Label::note( + "expected type: `snippet::Annotation`\n found type: `__&__snippet::Annotation`", + )); let renderer = Renderer::styled(); anstream::println!("{}", renderer.render(message)); diff --git a/examples/format.rs b/examples/format.rs index 34a76f40..1606777b 100644 --- a/examples/format.rs +++ b/examples/format.rs @@ -1,4 +1,4 @@ -use annotate_snippets::{Label, Level, Renderer, Snippet}; +use annotate_snippets::{Level, Renderer, Snippet}; fn main() { let source = r#") -> Option { @@ -28,9 +28,15 @@ fn main() { .line_start(51) .origin("src/format.rs") .annotation( - Label::warning("expected `Option` because of return type").span(5..19), + Level::Warning + .span(5..19) + .label("expected `Option` because of return type"), ) - .annotation(Label::error("expected enum `std::option::Option`").span(26..724)), + .annotation( + Level::Error + .span(26..724) + .label("expected enum `std::option::Option`"), + ), ); let renderer = Renderer::styled(); diff --git a/src/renderer/display_list.rs b/src/renderer/display_list.rs index b4c50a90..d54282fe 100644 --- a/src/renderer/display_list.rs +++ b/src/renderer/display_list.rs @@ -1040,7 +1040,7 @@ fn format_body( annotation: Annotation { annotation_type, id: None, - label: format_label(Some(annotation.label), None), + label: format_label(annotation.label, None), }, range, annotation_type: DisplayAnnotationType::from(annotation.level), @@ -1134,7 +1134,7 @@ fn format_body( annotation: Annotation { annotation_type, id: None, - label: format_label(Some(annotation.label), None), + label: format_label(annotation.label, None), }, range, annotation_type: DisplayAnnotationType::from(annotation.level), @@ -1371,7 +1371,11 @@ mod tests { let input = snippet::Level::Error.title("").snippet( snippet::Snippet::source(&source) .line_start(5402) - .annotation(snippet::Label::info("Test annotation").span(range.clone())), + .annotation( + snippet::Level::Info + .span(range.clone()) + .label("Test annotation"), + ), ); let output = from_display_lines(vec![ DisplayLine::Raw(DisplayRawLine::Annotation { @@ -1478,7 +1482,7 @@ mod tests { let input = snippet::Level::Error.title("").snippet( snippet::Snippet::source(source) .line_start(0) - .annotation(snippet::Label::error(label).span(0..source.len() + 2)), + .annotation(snippet::Level::Error.span(0..source.len() + 2).label(label)), ); let _ = DisplayList::new(input, &STYLESHEET, false, None); } @@ -1490,7 +1494,7 @@ mod tests { .line_start(1) .origin("") .fold(true) - .annotation(snippet::Label::error("oops").span(19..23)), + .annotation(snippet::Level::Error.span(19..23).label("oops")), ); let expected = from_display_lines(vec![ diff --git a/src/snippet.rs b/src/snippet.rs index e0a05ea8..7b214028 100644 --- a/src/snippet.rs +++ b/src/snippet.rs @@ -73,15 +73,6 @@ impl<'a> Label<'a> { self.label = label; self } - - /// Create a [`Annotation`] with the given span for a [`Snippet`] - pub fn span(&self, span: Range) -> Annotation<'a> { - Annotation { - range: span, - label: self.label, - level: self.level, - } - } } /// Structure containing the slice of text to be annotated and @@ -133,15 +124,22 @@ impl<'a> Snippet<'a> { /// An annotation for a [`Snippet`]. /// -/// This gets created by [`Label::span`]. +/// See [`Level::span`] to create a [`Annotation`] #[derive(Debug)] pub struct Annotation<'a> { /// The byte range of the annotation in the `source` string pub(crate) range: Range, - pub(crate) label: &'a str, + pub(crate) label: Option<&'a str>, pub(crate) level: Level, } +impl<'a> Annotation<'a> { + pub fn label(mut self, label: &'a str) -> Self { + self.label = Some(label); + self + } +} + /// Types of annotations. #[derive(Debug, Clone, Copy, PartialEq)] pub enum Level { @@ -164,4 +162,13 @@ impl Level { footer: vec![], } } + + /// Create a [`Annotation`] with the given span for a [`Snippet`] + pub fn span<'a>(self, span: Range) -> Annotation<'a> { + Annotation { + range: span, + label: None, + level: self, + } + } } diff --git a/tests/fixtures/deserialize.rs b/tests/fixtures/deserialize.rs index f5bb411f..a22f2157 100644 --- a/tests/fixtures/deserialize.rs +++ b/tests/fixtures/deserialize.rs @@ -147,7 +147,7 @@ impl<'a> From> for Annotation<'a> { label, level, } = val; - Label::new(level, label).span(range) + level.span(range).label(label) } } diff --git a/tests/formatter.rs b/tests/formatter.rs index c905b1cf..76dcb27c 100644 --- a/tests/formatter.rs +++ b/tests/formatter.rs @@ -1,11 +1,11 @@ -use annotate_snippets::{Label, Level, Renderer, Snippet}; +use annotate_snippets::{Level, Renderer, Snippet}; #[test] fn test_i_29() { let snippets = Level::Error.title("oops").snippet( Snippet::source("First line\r\nSecond oops line") .origin("") - .annotation(Label::error("oops").span(19..23)) + .annotation(Level::Error.span(19..23).label("oops")) .fold(true), ); let expected = r#"error: oops @@ -25,7 +25,7 @@ fn test_point_to_double_width_characters() { let snippets = Level::Error.title("").snippet( Snippet::source("こんにちは、世界") .origin("") - .annotation(Label::error("world").span(12..16)), + .annotation(Level::Error.span(12..16).label("world")), ); let expected = r#"error @@ -44,7 +44,7 @@ fn test_point_to_double_width_characters_across_lines() { let snippets = Level::Error.title("").snippet( Snippet::source("おはよう\nございます") .origin("") - .annotation(Label::error("Good morning").span(4..15)), + .annotation(Level::Error.span(4..15).label("Good morning")), ); let expected = r#"error @@ -65,8 +65,8 @@ fn test_point_to_double_width_characters_multiple() { let snippets = Level::Error.title("").snippet( Snippet::source("お寿司\n食べたい🍣") .origin("") - .annotation(Label::error("Sushi1").span(0..6)) - .annotation(Label::note("Sushi2").span(11..15)), + .annotation(Level::Error.span(0..6).label("Sushi1")) + .annotation(Level::Note.span(11..15).label("Sushi2")), ); let expected = r#"error @@ -87,7 +87,7 @@ fn test_point_to_double_width_characters_mixed() { let snippets = Level::Error.title("").snippet( Snippet::source("こんにちは、新しいWorld!") .origin("") - .annotation(Label::error("New world").span(12..23)), + .annotation(Level::Error.span(12..23).label("New world")), ); let expected = r#"error From 4ceb316f8d9e9aefdc84c6dfe33d2c6618ee881b Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 11 Mar 2024 16:24:07 -0500 Subject: [PATCH 12/13] feat: Allow bulk-adding of snippets, footers, annotations This will make it easier for diagnostic systems to convert their messages to a `Message` --- src/snippet.rs | 15 +++++++++++++++ tests/fixtures/deserialize.rs | 14 +++----------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/snippet.rs b/src/snippet.rs index 7b214028..9ce10318 100644 --- a/src/snippet.rs +++ b/src/snippet.rs @@ -34,10 +34,20 @@ impl<'a> Message<'a> { self } + pub fn snippets(mut self, slice: impl IntoIterator>) -> Self { + self.snippets.extend(slice); + self + } + pub fn footer(mut self, footer: Label<'a>) -> Self { self.footer.push(footer); self } + + pub fn footers(mut self, footer: impl IntoIterator>) -> Self { + self.footer.extend(footer); + self + } } pub struct Label<'a> { @@ -116,6 +126,11 @@ impl<'a> Snippet<'a> { self } + pub fn annotations(mut self, annotation: impl IntoIterator>) -> Self { + self.annotations.extend(annotation); + self + } + pub fn fold(mut self, fold: bool) -> Self { self.fold = fold; self diff --git a/tests/fixtures/deserialize.rs b/tests/fixtures/deserialize.rs index a22f2157..6bfe76f9 100644 --- a/tests/fixtures/deserialize.rs +++ b/tests/fixtures/deserialize.rs @@ -42,12 +42,8 @@ impl<'a> From> for Message<'a> { if let Some(id) = id { message = message.id(id); } - message = snippets - .into_iter() - .fold(message, |message, snippet| message.snippet(snippet)); - message = footer - .into_iter() - .fold(message, |message, label| message.footer(label)); + message = message.snippets(snippets); + message = message.footers(footer); message } } @@ -111,11 +107,7 @@ impl<'a> From> for Snippet<'a> { if let Some(origin) = origin { snippet = snippet.origin(origin) } - snippet = annotations - .into_iter() - .fold(snippet, |snippet, annotation| { - snippet.annotation(annotation) - }); + snippet = snippet.annotations(annotations); snippet } } From 9cdd503a0a52961b9536a460e3bbc325f6efdc69 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Tue, 12 Mar 2024 11:13:12 -0500 Subject: [PATCH 13/13] docs: Describe fold --- src/snippet.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/snippet.rs b/src/snippet.rs index 9ce10318..6ba2d2d8 100644 --- a/src/snippet.rs +++ b/src/snippet.rs @@ -131,6 +131,7 @@ impl<'a> Snippet<'a> { self } + /// Hide lines without [`Annotation`]s pub fn fold(mut self, fold: bool) -> Self { self.fold = fold; self