Skip to content

Commit 1e326f9

Browse files
committed
fix: Show Group/File start for Snippets without a path
1 parent e5a05c7 commit 1e326f9

File tree

3 files changed

+74
-19
lines changed

3 files changed

+74
-19
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: 8 additions & 6 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
@@ -2774,7 +2774,7 @@ error:
27742774

27752775
let expected_unicode = str![[r#"
27762776
error:
2777-
2777+
╭▸
27782778
1 │ def __call__(self, *vals: bytes, maxlen: int | None = None) -> list[bytes]: ...
27792779
╰╴ ━━━━━━━━ annotation
27802780
"#]];
@@ -2803,6 +2803,7 @@ error:
28032803
1 | def __call__(self, *vals: bytes, maxlen: int | None = None) -> list[bytes]: ...
28042804
| ^^^^^^^^ annotation
28052805
|
2806+
:::
28062807
1 | def __call__(self, *vals: bytes, maxlen: int | None = None) -> list[bytes]: ...
28072808
| ^^^^^^^^ annotation
28082809
"#]];
@@ -2811,10 +2812,11 @@ error:
28112812

28122813
let expected_unicode = str![[r#"
28132814
error:
2814-
2815+
╭▸
28152816
1 │ def __call__(self, *vals: bytes, maxlen: int | None = None) -> list[bytes]: ...
28162817
│ ━━━━━━━━ annotation
28172818
2819+
28182820
1 │ def __call__(self, *vals: bytes, maxlen: int | None = None) -> list[bytes]: ...
28192821
╰╴ ━━━━━━━━ annotation
28202822
"#]];

0 commit comments

Comments
 (0)