Skip to content

fix: Show Group/File start for Snippets without a path #251

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
Jul 3, 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
63 changes: 56 additions & 7 deletions src/renderer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,14 @@ impl Renderer {
}
let mut message_iter = group.elements.iter().enumerate().peekable();
let mut last_was_suggestion = false;
let mut first_was_title = false;
while let Some((i, section)) = message_iter.next() {
let peek = message_iter.peek().map(|(_, s)| s).copied();
match &section {
Element::Title(title) => {
if i == 0 {
first_was_title = true;
}
let title_style = match (i == 0, g == 0) {
(true, true) => TitleStyle::MainHeader,
(true, false) => TitleStyle::Header,
Expand Down Expand Up @@ -329,11 +333,13 @@ impl Renderer {
if let Some((source_map, annotated_lines)) =
source_map_annotated_lines.pop_front()
{
let is_primary = primary_path == cause.path.as_ref()
&& i == first_was_title as usize;
self.render_snippet_annotations(
&mut buffer,
max_line_num_len,
cause,
primary_path,
is_primary,
&source_map,
&annotated_lines,
max_depth,
Expand Down Expand Up @@ -722,7 +728,7 @@ impl Renderer {
buffer: &mut StyledBuffer,
max_line_num_len: usize,
snippet: &Snippet<'_, Annotation<'_>>,
primary_path: Option<&Cow<'_, str>>,
is_primary: bool,
sm: &SourceMap<'_>,
annotated_lines: &[AnnotatedLineInfo<'_>],
multiline_depth: usize,
Expand All @@ -732,7 +738,7 @@ impl Renderer {
let mut origin = Origin::path(path.as_ref());
// 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_path == Some(&origin.path);
//let is_primary = primary_path == Some(&origin.path);

if is_primary {
origin.primary = true;
Expand Down Expand Up @@ -776,11 +782,54 @@ impl Renderer {
}
let buffer_msg_line_offset = buffer.num_lines();
self.render_origin(buffer, max_line_num_len, &origin, buffer_msg_line_offset);
}
// Put in the spacer between the location and annotated source
self.draw_col_separator_no_space(
buffer,
buffer_msg_line_offset + 1,
max_line_num_len + 1,
);
} else {
let buffer_msg_line_offset = buffer.num_lines();
if is_primary {
if self.theme == OutputTheme::Unicode {
buffer.puts(
buffer_msg_line_offset,
max_line_num_len,
self.file_start(),
ElementStyle::LineNumber,
);
} else {
self.draw_col_separator_no_space(
buffer,
buffer_msg_line_offset,
max_line_num_len + 1,
);
}
} else {
// Add spacing line, as shown:
// --> $DIR/file:54:15
// |
// LL | code
// | ^^^^
// | (<- It prints *this* line)
// ::: $DIR/other_file.rs:15:5
// |
// LL | code
// | ----
self.draw_col_separator_no_space(
buffer,
buffer_msg_line_offset,
max_line_num_len + 1,
);

// Put in the spacer between the location and annotated source
let buffer_msg_line_offset = buffer.num_lines();
self.draw_col_separator_no_space(buffer, buffer_msg_line_offset, max_line_num_len + 1);
buffer.puts(
buffer_msg_line_offset + 1,
max_line_num_len,
self.secondary_file_start(),
ElementStyle::LineNumber,
);
}
}

// Contains the vertical lines' positions for active multiline annotations
let mut multilines = Vec::new();
Expand Down
16 changes: 10 additions & 6 deletions tests/color/issue_9.term.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
78 changes: 74 additions & 4 deletions tests/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2053,7 +2053,7 @@ error: title

let expected_unicode = str![[r#"
error: title
╭▸
1 │ version = "0.1.0"
2 │ # Ensure that the spans from toml handle utf-8 correctly
3 │ authors = [
Expand Down Expand Up @@ -2093,7 +2093,7 @@ error: expected item, found `?`

let expected_unicode = str![[r#"
error: expected item, found `?`
╭▸
1 │ … 宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。*/?
│ ━ expected item
Expand Down Expand Up @@ -2130,7 +2130,7 @@ error: expected item, found `?`

let expected_unicode = str![[r#"
error: expected item, found `?`
╭▸
1 │ … 的。这是宽的。这是宽的。这是宽的。…
│ ━━ expected item
Expand Down Expand Up @@ -2167,7 +2167,7 @@ error: expected item, found `?`

let expected_unicode = str![[r#"
error: expected item, found `?`
╭▸
1 │ …aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/?
│ ━ expected item
Expand Down Expand Up @@ -2753,3 +2753,73 @@ fn main() {
let renderer = Renderer::plain();
renderer.render(input);
}

#[test]
fn snippet_no_path() {
// Taken from: https://docs.python.org/3/library/typing.html#annotating-callable-objects

let source = "def __call__(self, *vals: bytes, maxlen: int | None = None) -> list[bytes]: ...";
let input = &[Group::with_title(Level::ERROR.title("")).element(
Snippet::source(source).annotation(AnnotationKind::Primary.span(4..12).label("annotation")),
)];

let expected_ascii = str![[r#"
error:
|
1 | def __call__(self, *vals: bytes, maxlen: int | None = None) -> list[bytes]: ...
| ^^^^^^^^ annotation
"#]];
let renderer = Renderer::plain();
assert_data_eq!(renderer.render(input), expected_ascii);

let expected_unicode = str![[r#"
error:
╭▸
1 │ def __call__(self, *vals: bytes, maxlen: int | None = None) -> list[bytes]: ...
╰╴ ━━━━━━━━ annotation
"#]];
let renderer = Renderer::plain().theme(OutputTheme::Unicode);
assert_data_eq!(renderer.render(input), expected_unicode);
}

#[test]
fn multiple_snippet_no_path() {
// Taken from: https://docs.python.org/3/library/typing.html#annotating-callable-objects

let source = "def __call__(self, *vals: bytes, maxlen: int | None = None) -> list[bytes]: ...";
let input = &[Group::with_title(Level::ERROR.title(""))
.element(
Snippet::source(source)
.annotation(AnnotationKind::Primary.span(4..12).label("annotation")),
)
.element(
Snippet::source(source)
.annotation(AnnotationKind::Primary.span(4..12).label("annotation")),
)];

let expected_ascii = str![[r#"
error:
|
1 | def __call__(self, *vals: bytes, maxlen: int | None = None) -> list[bytes]: ...
| ^^^^^^^^ annotation
|
:::
1 | def __call__(self, *vals: bytes, maxlen: int | None = None) -> list[bytes]: ...
| ^^^^^^^^ annotation
"#]];
let renderer = Renderer::plain();
assert_data_eq!(renderer.render(input), expected_ascii);

let expected_unicode = str![[r#"
error:
╭▸
1 │ def __call__(self, *vals: bytes, maxlen: int | None = None) -> list[bytes]: ...
│ ━━━━━━━━ annotation
1 │ def __call__(self, *vals: bytes, maxlen: int | None = None) -> list[bytes]: ...
╰╴ ━━━━━━━━ annotation
"#]];
let renderer = Renderer::plain().theme(OutputTheme::Unicode);
assert_data_eq!(renderer.render(input), expected_unicode);
}