Skip to content

Commit 244bd35

Browse files
authored
Merge pull request #251 from Muscraft/snippet-no-path
fix: Show Group/File start for Snippets without a path
2 parents ca9f5e5 + f1fcdda commit 244bd35

File tree

3 files changed

+140
-17
lines changed

3 files changed

+140
-17
lines changed

src/renderer/mod.rs

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -288,10 +288,14 @@ impl Renderer {
288288
}
289289
let mut message_iter = group.elements.iter().enumerate().peekable();
290290
let mut last_was_suggestion = false;
291+
let mut first_was_title = false;
291292
while let Some((i, section)) = message_iter.next() {
292293
let peek = message_iter.peek().map(|(_, s)| s).copied();
293294
match &section {
294295
Element::Title(title) => {
296+
if i == 0 {
297+
first_was_title = true;
298+
}
295299
let title_style = match (i == 0, g == 0) {
296300
(true, true) => TitleStyle::MainHeader,
297301
(true, false) => TitleStyle::Header,
@@ -329,11 +333,13 @@ impl Renderer {
329333
if let Some((source_map, annotated_lines)) =
330334
source_map_annotated_lines.pop_front()
331335
{
336+
let is_primary = primary_path == cause.path.as_ref()
337+
&& i == first_was_title as usize;
332338
self.render_snippet_annotations(
333339
&mut buffer,
334340
max_line_num_len,
335341
cause,
336-
primary_path,
342+
is_primary,
337343
&source_map,
338344
&annotated_lines,
339345
max_depth,
@@ -722,7 +728,7 @@ impl Renderer {
722728
buffer: &mut StyledBuffer,
723729
max_line_num_len: usize,
724730
snippet: &Snippet<'_, Annotation<'_>>,
725-
primary_path: Option<&Cow<'_, str>>,
731+
is_primary: bool,
726732
sm: &SourceMap<'_>,
727733
annotated_lines: &[AnnotatedLineInfo<'_>],
728734
multiline_depth: usize,
@@ -732,7 +738,7 @@ impl Renderer {
732738
let mut origin = Origin::path(path.as_ref());
733739
// print out the span location and spacer before we print the annotated source
734740
// to do this, we need to know if this span will be primary
735-
let is_primary = primary_path == Some(&origin.path);
741+
//let is_primary = primary_path == Some(&origin.path);
736742

737743
if is_primary {
738744
origin.primary = true;
@@ -776,11 +782,54 @@ impl Renderer {
776782
}
777783
let buffer_msg_line_offset = buffer.num_lines();
778784
self.render_origin(buffer, max_line_num_len, &origin, buffer_msg_line_offset);
779-
}
785+
// Put in the spacer between the location and annotated source
786+
self.draw_col_separator_no_space(
787+
buffer,
788+
buffer_msg_line_offset + 1,
789+
max_line_num_len + 1,
790+
);
791+
} else {
792+
let buffer_msg_line_offset = buffer.num_lines();
793+
if is_primary {
794+
if self.theme == OutputTheme::Unicode {
795+
buffer.puts(
796+
buffer_msg_line_offset,
797+
max_line_num_len,
798+
self.file_start(),
799+
ElementStyle::LineNumber,
800+
);
801+
} else {
802+
self.draw_col_separator_no_space(
803+
buffer,
804+
buffer_msg_line_offset,
805+
max_line_num_len + 1,
806+
);
807+
}
808+
} else {
809+
// Add spacing line, as shown:
810+
// --> $DIR/file:54:15
811+
// |
812+
// LL | code
813+
// | ^^^^
814+
// | (<- It prints *this* line)
815+
// ::: $DIR/other_file.rs:15:5
816+
// |
817+
// LL | code
818+
// | ----
819+
self.draw_col_separator_no_space(
820+
buffer,
821+
buffer_msg_line_offset,
822+
max_line_num_len + 1,
823+
);
780824

781-
// Put in the spacer between the location and annotated source
782-
let buffer_msg_line_offset = buffer.num_lines();
783-
self.draw_col_separator_no_space(buffer, buffer_msg_line_offset, max_line_num_len + 1);
825+
buffer.puts(
826+
buffer_msg_line_offset + 1,
827+
max_line_num_len,
828+
self.secondary_file_start(),
829+
ElementStyle::LineNumber,
830+
);
831+
}
832+
}
784833

785834
// Contains the vertical lines' positions for active multiline annotations
786835
let mut multilines = Vec::new();

tests/color/issue_9.term.svg

Lines changed: 10 additions & 6 deletions
Loading

tests/formatter.rs

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2053,7 +2053,7 @@ error: title
20532053

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

20942094
let expected_unicode = str![[r#"
20952095
error: expected item, found `?`
2096-
2096+
╭▸
20972097
1 │ … 宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。*/?
20982098
│ ━ expected item
20992099
@@ -2130,7 +2130,7 @@ error: expected item, found `?`
21302130

21312131
let expected_unicode = str![[r#"
21322132
error: expected item, found `?`
2133-
2133+
╭▸
21342134
1 │ … 的。这是宽的。这是宽的。这是宽的。…
21352135
│ ━━ expected item
21362136
@@ -2167,7 +2167,7 @@ error: expected item, found `?`
21672167

21682168
let expected_unicode = str![[r#"
21692169
error: expected item, found `?`
2170-
2170+
╭▸
21712171
1 │ …aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/?
21722172
│ ━ expected item
21732173
@@ -2753,3 +2753,73 @@ fn main() {
27532753
let renderer = Renderer::plain();
27542754
renderer.render(input);
27552755
}
2756+
2757+
#[test]
2758+
fn snippet_no_path() {
2759+
// Taken from: https://docs.python.org/3/library/typing.html#annotating-callable-objects
2760+
2761+
let source = "def __call__(self, *vals: bytes, maxlen: int | None = None) -> list[bytes]: ...";
2762+
let input = &[Group::with_title(Level::ERROR.title("")).element(
2763+
Snippet::source(source).annotation(AnnotationKind::Primary.span(4..12).label("annotation")),
2764+
)];
2765+
2766+
let expected_ascii = str![[r#"
2767+
error:
2768+
|
2769+
1 | def __call__(self, *vals: bytes, maxlen: int | None = None) -> list[bytes]: ...
2770+
| ^^^^^^^^ annotation
2771+
"#]];
2772+
let renderer = Renderer::plain();
2773+
assert_data_eq!(renderer.render(input), expected_ascii);
2774+
2775+
let expected_unicode = str![[r#"
2776+
error:
2777+
╭▸
2778+
1 │ def __call__(self, *vals: bytes, maxlen: int | None = None) -> list[bytes]: ...
2779+
╰╴ ━━━━━━━━ annotation
2780+
"#]];
2781+
let renderer = Renderer::plain().theme(OutputTheme::Unicode);
2782+
assert_data_eq!(renderer.render(input), expected_unicode);
2783+
}
2784+
2785+
#[test]
2786+
fn multiple_snippet_no_path() {
2787+
// Taken from: https://docs.python.org/3/library/typing.html#annotating-callable-objects
2788+
2789+
let source = "def __call__(self, *vals: bytes, maxlen: int | None = None) -> list[bytes]: ...";
2790+
let input = &[Group::with_title(Level::ERROR.title(""))
2791+
.element(
2792+
Snippet::source(source)
2793+
.annotation(AnnotationKind::Primary.span(4..12).label("annotation")),
2794+
)
2795+
.element(
2796+
Snippet::source(source)
2797+
.annotation(AnnotationKind::Primary.span(4..12).label("annotation")),
2798+
)];
2799+
2800+
let expected_ascii = str![[r#"
2801+
error:
2802+
|
2803+
1 | def __call__(self, *vals: bytes, maxlen: int | None = None) -> list[bytes]: ...
2804+
| ^^^^^^^^ annotation
2805+
|
2806+
:::
2807+
1 | def __call__(self, *vals: bytes, maxlen: int | None = None) -> list[bytes]: ...
2808+
| ^^^^^^^^ annotation
2809+
"#]];
2810+
let renderer = Renderer::plain();
2811+
assert_data_eq!(renderer.render(input), expected_ascii);
2812+
2813+
let expected_unicode = str![[r#"
2814+
error:
2815+
╭▸
2816+
1 │ def __call__(self, *vals: bytes, maxlen: int | None = None) -> list[bytes]: ...
2817+
│ ━━━━━━━━ annotation
2818+
2819+
2820+
1 │ def __call__(self, *vals: bytes, maxlen: int | None = None) -> list[bytes]: ...
2821+
╰╴ ━━━━━━━━ annotation
2822+
"#]];
2823+
let renderer = Renderer::plain().theme(OutputTheme::Unicode);
2824+
assert_data_eq!(renderer.render(input), expected_unicode);
2825+
}

0 commit comments

Comments
 (0)