Skip to content

Improve clarity arount Origin and Snippet #227

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn simple() -> String {
Group::new().element(
Snippet::source(source)
.line_start(51)
.origin("src/format.rs")
.path("src/format.rs")
.annotation(
AnnotationKind::Context
.span(5..19)
Expand Down Expand Up @@ -73,7 +73,7 @@ fn fold(bencher: divan::Bencher<'_, '_>, context: usize) {
Group::new().element(
Snippet::source(&input)
.fold(true)
.origin("src/format.rs")
.path("src/format.rs")
.annotation(
AnnotationKind::Context
.span(span)
Expand Down
2 changes: 1 addition & 1 deletion examples/custom_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub static C: u32 = 0 - 1;
.group(
Group::new().element(
Snippet::source(source)
.origin("$DIR/err.rs")
.path("$DIR/err.rs")
.fold(true)
.annotation(
AnnotationKind::Primary
Expand Down
4 changes: 2 additions & 2 deletions examples/custom_level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn main() {
Group::new().element(
Snippet::source(source)
.line_start(1)
.origin("$DIR/issue-114529-illegal-break-with-value.rs")
.path("$DIR/issue-114529-illegal-break-with-value.rs")
.fold(true)
.annotation(
AnnotationKind::Primary
Expand All @@ -60,7 +60,7 @@ fn main() {
.element(
Snippet::source(source)
.line_start(1)
.origin("$DIR/issue-114529-illegal-break-with-value.rs")
.path("$DIR/issue-114529-illegal-break-with-value.rs")
.fold(true)
.patch(Patch::new(483..581, "break")),
),
Expand Down
2 changes: 1 addition & 1 deletion examples/expected_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn main() {
Group::new().element(
Snippet::source(source)
.line_start(26)
.origin("examples/footer.rs")
.path("examples/footer.rs")
.fold(true)
.annotation(AnnotationKind::Primary.span(193..195).label(
"expected struct `annotate_snippets::snippet::Slice`, found reference",
Expand Down
2 changes: 1 addition & 1 deletion examples/footer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fn main() {
Group::new().element(
Snippet::source(" slices: vec![\"A\",")
.line_start(13)
.origin("src/multislice.rs")
.path("src/multislice.rs")
.annotation(AnnotationKind::Primary.span(21..24).label(
"expected struct `annotate_snippets::snippet::Slice`, found reference",
)),
Expand Down
2 changes: 1 addition & 1 deletion examples/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn main() {
Group::new().element(
Snippet::source(source)
.line_start(51)
.origin("src/format.rs")
.path("src/format.rs")
.annotation(
AnnotationKind::Context
.span(5..19)
Expand Down
2 changes: 1 addition & 1 deletion examples/highlight_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn main() {}
.element(
Snippet::source(source)
.fold(true)
.origin("$DIR/E0010-teach.rs")
.path("$DIR/E0010-teach.rs")
.annotation(
AnnotationKind::Primary
.span(72..85)
Expand Down
4 changes: 2 additions & 2 deletions examples/highlight_title.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ fn main() {
.element(
Snippet::source(source)
.fold(true)
.origin("$DIR/highlighting.rs")
.path("$DIR/highlighting.rs")
.annotation(
AnnotationKind::Primary
.span(553..563)
Expand All @@ -69,7 +69,7 @@ fn main() {
.element(
Snippet::source(source)
.fold(true)
.origin("$DIR/highlighting.rs")
.path("$DIR/highlighting.rs")
.annotation(AnnotationKind::Context.span(200..333).label(""))
.annotation(AnnotationKind::Primary.span(194..199)),
),
Expand Down
4 changes: 2 additions & 2 deletions examples/multislice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ fn main() {
.element(
Snippet::<Annotation<'_>>::source("Foo")
.line_start(51)
.origin("src/format.rs"),
.path("src/format.rs"),
)
.element(
Snippet::<Annotation<'_>>::source("Faa")
.line_start(129)
.origin("src/display.rs"),
.path("src/display.rs"),
),
);

Expand Down
52 changes: 26 additions & 26 deletions src/renderer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
//! .group(
//! Group::new().element(
//! Snippet::source(source)
//! .origin("temp.rs")
//! .path("temp.rs")
//! .line_start(1)
//! .fold(true)
//! .annotation(
Expand Down Expand Up @@ -232,21 +232,21 @@ impl Renderer {
) -> Result<String, fmt::Error> {
let mut out_string = String::new();

let og_primary_origin = message
let og_primary_path = message
.groups
.iter()
.find_map(|group| {
group.elements.iter().find_map(|s| match &s {
Element::Cause(cause) => {
if cause.markers.iter().any(|m| m.kind.is_primary()) {
Some(cause.origin)
Some(cause.path)
} else {
None
}
}
Element::Origin(origin) => {
if origin.primary {
Some(Some(origin.origin))
Some(Some(origin.path))
} else {
None
}
Expand All @@ -260,8 +260,8 @@ impl Renderer {
.iter()
.find_map(|group| {
group.elements.iter().find_map(|s| match &s {
Element::Cause(cause) => Some(cause.origin),
Element::Origin(origin) => Some(Some(origin.origin)),
Element::Cause(cause) => Some(cause.path),
Element::Origin(origin) => Some(Some(origin.path)),
_ => None,
})
})
Expand All @@ -270,20 +270,20 @@ impl Renderer {
let group_len = message.groups.len();
for (g, group) in message.groups.into_iter().enumerate() {
let mut buffer = StyledBuffer::new();
let primary_origin = group
let primary_path = group
.elements
.iter()
.find_map(|s| match &s {
Element::Cause(cause) => {
if cause.markers.iter().any(|m| m.kind.is_primary()) {
Some(cause.origin)
Some(cause.path)
} else {
None
}
}
Element::Origin(origin) => {
if origin.primary {
Some(Some(origin.origin))
Some(Some(origin.path))
} else {
None
}
Expand All @@ -295,8 +295,8 @@ impl Renderer {
.elements
.iter()
.find_map(|s| match &s {
Element::Cause(cause) => Some(cause.origin),
Element::Origin(origin) => Some(Some(origin.origin)),
Element::Cause(cause) => Some(cause.path),
Element::Origin(origin) => Some(Some(origin.path)),
_ => None,
})
.unwrap_or_default(),
Expand Down Expand Up @@ -357,7 +357,7 @@ impl Renderer {
&mut buffer,
max_line_num_len,
cause,
primary_origin,
primary_path,
&source_map,
&annotated_lines,
max_depth,
Expand Down Expand Up @@ -396,7 +396,7 @@ impl Renderer {
suggestion,
max_line_num_len,
&source_map,
primary_origin.or(og_primary_origin),
primary_path.or(og_primary_path),
last_was_suggestion,
);
last_was_suggestion = true;
Expand Down Expand Up @@ -490,8 +490,8 @@ impl Renderer {
labels = Some(labels_inner);
}

if let Some(origin) = cause.origin {
let mut origin = Origin::new(origin);
if let Some(path) = cause.path {
let mut origin = Origin::new(path);
origin.primary = true;

let source_map = SourceMap::new(cause.source, cause.line_start);
Expand Down Expand Up @@ -764,10 +764,10 @@ impl Renderer {

let str = match (&origin.line, &origin.char_column) {
(Some(line), Some(col)) => {
format!("{}:{}:{}", origin.origin, line, col)
format!("{}:{}:{}", origin.path, line, col)
}
(Some(line), None) => format!("{}:{}", origin.origin, line),
_ => origin.origin.to_owned(),
(Some(line), None) => format!("{}:{}", origin.path, line),
_ => origin.path.to_owned(),
};

buffer.append(buffer_msg_line_offset, &str, ElementStyle::LineAndColumn);
Expand All @@ -784,17 +784,17 @@ impl Renderer {
buffer: &mut StyledBuffer,
max_line_num_len: usize,
snippet: &Snippet<'_, Annotation<'_>>,
primary_origin: Option<&str>,
primary_path: Option<&str>,
sm: &SourceMap<'_>,
annotated_lines: &[AnnotatedLineInfo<'_>],
multiline_depth: usize,
is_cont: bool,
) {
if let Some(origin) = snippet.origin {
let mut origin = Origin::new(origin);
if let Some(path) = snippet.path {
let mut origin = Origin::new(path);
// print out the span location and spacer before we print the annotated source
// to do this, we need to know if this span will be primary
let is_primary = primary_origin == Some(origin.origin);
let is_primary = primary_path == Some(origin.path);

if is_primary {
origin.primary = true;
Expand Down Expand Up @@ -1648,7 +1648,7 @@ impl Renderer {
suggestion: &Snippet<'_, Patch<'_>>,
max_line_num_len: usize,
sm: &SourceMap<'_>,
primary_origin: Option<&str>,
primary_path: Option<&str>,
is_cont: bool,
) {
let suggestions = sm.splice_lines(suggestion.markers.clone());
Expand All @@ -1671,14 +1671,14 @@ impl Renderer {
ElementStyle::LineNumber,
);
}
if suggestion.origin != primary_origin {
if let Some(origin) = suggestion.origin {
if suggestion.path != primary_path {
if let Some(path) = suggestion.path {
let (loc, _) = sm.span_to_locations(parts[0].span.clone());
// --> file.rs:line:col
// |
let arrow = self.file_start();
buffer.puts(row_num - 1, 0, arrow, ElementStyle::LineNumber);
let message = format!("{}:{}:{}", origin, loc.line, loc.char + 1);
let message = format!("{}:{}:{}", path, loc.line, loc.char + 1);
if is_cont {
buffer.append(row_num - 1, &message, ElementStyle::LineAndColumn);
} else {
Expand Down
26 changes: 18 additions & 8 deletions src/snippet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,11 @@ pub struct Title<'a> {
}

/// A source view [`Element`] in a [`Group`]
///
/// If you do not have [source][Snippet::source] available, see instead [`Origin`]
#[derive(Clone, Debug)]
pub struct Snippet<'a, T> {
pub(crate) origin: Option<&'a str>,
pub(crate) path: Option<&'a str>,
pub(crate) line_start: usize,
pub(crate) source: &'a str,
pub(crate) markers: Vec<T>,
Expand All @@ -183,7 +185,7 @@ impl<'a, T: Clone> Snippet<'a, T> {
/// </div>
pub fn source(source: &'a str) -> Self {
Self {
origin: None,
path: None,
line_start: 1,
source,
markers: vec![],
Expand All @@ -207,8 +209,8 @@ impl<'a, T: Clone> Snippet<'a, T> {
/// not allowed to be passed to this function.
///
/// </div>
pub fn origin(mut self, origin: &'a str) -> Self {
self.origin = Some(origin);
pub fn path(mut self, path: &'a str) -> Self {
self.path = Some(path);
self
}

Expand Down Expand Up @@ -375,10 +377,12 @@ impl<'a> Patch<'a> {
}
}

/// The location of the [`Snippet`] (e.g. a path)
/// The referenced location (e.g. a path)
///
/// If you have source available, see instead [`Snippet`]
#[derive(Clone, Debug)]
pub struct Origin<'a> {
pub(crate) origin: &'a str,
pub(crate) path: &'a str,
pub(crate) line: Option<usize>,
pub(crate) char_column: Option<usize>,
pub(crate) primary: bool,
Expand All @@ -392,9 +396,9 @@ impl<'a> Origin<'a> {
/// not allowed to be passed to this function.
///
/// </div>
pub fn new(origin: &'a str) -> Self {
pub fn new(path: &'a str) -> Self {
Self {
origin,
path,
line: None,
char_column: None,
primary: false,
Expand All @@ -412,6 +416,12 @@ impl<'a> Origin<'a> {
/// Set the default column to display
///
/// Otherwise this will be inferred from the primary [`Annotation`]
///
/// <div class="warning">
///
/// `char_column` is only be respected if [`Origin::line`] is also set.
///
/// </div>
pub fn char_column(mut self, char_column: usize) -> Self {
self.char_column = Some(char_column);
self
Expand Down
2 changes: 1 addition & 1 deletion tests/color/ann_eof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fn case() {
let input = Level::ERROR.header("expected `.`, `=`").group(
Group::new().element(
Snippet::source("asdf")
.origin("Cargo.toml")
.path("Cargo.toml")
.line_start(1)
.annotation(AnnotationKind::Primary.span(4..4).label("")),
),
Expand Down
2 changes: 1 addition & 1 deletion tests/color/ann_insertion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fn case() {
let input = Level::ERROR.header("expected `.`, `=`").group(
Group::new().element(
Snippet::source("asf")
.origin("Cargo.toml")
.path("Cargo.toml")
.line_start(1)
.annotation(AnnotationKind::Primary.span(2..2).label("'d' belongs here")),
),
Expand Down
2 changes: 1 addition & 1 deletion tests/color/ann_multiline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn case() {
.group(
Group::new().element(
Snippet::source(source)
.origin("src/display_list.rs")
.path("src/display_list.rs")
.line_start(139)
.fold(false)
.annotation(
Expand Down
2 changes: 1 addition & 1 deletion tests/color/ann_multiline2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ to exactly one character on next line.
.group(
Group::new().element(
Snippet::source(source)
.origin("foo.txt")
.path("foo.txt")
.line_start(26)
.fold(false)
.annotation(
Expand Down
Loading
Loading