Skip to content

Commit 894f734

Browse files
committed
fix(render): On fold, strip prefix/suffix rather than fold
As we don't show `...` on non-first lines without fold, it seems to make sense to do the same thing for fold. To show this, we likely would want to add more nuance than just `fold`. This has the benefit of offering a "magic mode" where the annotated lines get selected automatically. This was implemented by pre-processing things which should keep overhead low as other calculations down the line aren't done.
1 parent a741b6b commit 894f734

File tree

4 files changed

+57
-29
lines changed

4 files changed

+57
-29
lines changed

src/renderer/display_list.rs

+49-6
Original file line numberDiff line numberDiff line change
@@ -696,17 +696,19 @@ impl<'a> Iterator for CursorLines<'a> {
696696
}
697697

698698
fn format_message(
699-
snippet::Message {
699+
message: snippet::Message<'_>,
700+
term_width: usize,
701+
anonymized_line_numbers: bool,
702+
primary: bool,
703+
) -> Vec<DisplaySet<'_>> {
704+
let snippet::Message {
700705
level,
701706
id,
702707
title,
703708
footer,
704709
snippets,
705-
}: snippet::Message<'_>,
706-
term_width: usize,
707-
anonymized_line_numbers: bool,
708-
primary: bool,
709-
) -> Vec<DisplaySet<'_>> {
710+
} = message;
711+
710712
let mut sets = vec![];
711713
let body = if !snippets.is_empty() || primary {
712714
vec![format_title(level, id, title)]
@@ -715,6 +717,7 @@ fn format_message(
715717
};
716718

717719
for (idx, snippet) in snippets.into_iter().enumerate() {
720+
let snippet = fold_prefix_suffix(snippet);
718721
sets.push(format_snippet(
719722
snippet,
720723
idx == 0,
@@ -876,6 +879,46 @@ fn format_header<'a>(
876879
None
877880
}
878881

882+
fn fold_prefix_suffix(mut snippet: snippet::Snippet<'_>) -> snippet::Snippet<'_> {
883+
if !snippet.fold {
884+
return snippet;
885+
}
886+
887+
let ann_start = snippet
888+
.annotations
889+
.iter()
890+
.map(|ann| ann.range.start)
891+
.min()
892+
.unwrap_or(0);
893+
if let Some(before_new_start) = snippet.source[0..ann_start].rfind('\n') {
894+
let new_start = before_new_start + 1;
895+
896+
let line_offset = snippet.source[..new_start].lines().count();
897+
snippet.line_start += line_offset;
898+
899+
snippet.source = &snippet.source[new_start..];
900+
901+
for ann in &mut snippet.annotations {
902+
let range_start = ann.range.start - new_start;
903+
let range_end = ann.range.end - new_start;
904+
ann.range = range_start..range_end;
905+
}
906+
}
907+
908+
let ann_end = snippet
909+
.annotations
910+
.iter()
911+
.map(|ann| ann.range.end)
912+
.max()
913+
.unwrap_or(snippet.source.len());
914+
if let Some(end_offset) = snippet.source[ann_end..].find('\n') {
915+
let new_end = ann_end + end_offset;
916+
snippet.source = &snippet.source[..new_end];
917+
}
918+
919+
snippet
920+
}
921+
879922
fn fold_body(mut body: Vec<DisplayLine<'_>>) -> Vec<DisplayLine<'_>> {
880923
enum Line {
881924
Fold(usize),

tests/fixtures/no-color/fold_bad_origin_line.svg

+4-6
Loading

tests/fixtures/no-color/fold_leading.svg

+4-16
Loading

tests/formatter.rs

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ fn test_i_29() {
1414
error: oops
1515
--> <current file>:2:8
1616
|
17-
1 | First line
1817
2 | Second oops line
1918
| ^^^^ oops
2019
|"#]]

0 commit comments

Comments
 (0)