From de0e330ba2295df8697f73b66feca2107b9da430 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Thu, 21 Mar 2024 12:22:05 -0500 Subject: [PATCH 1/4] test(render): Clarify a test name --- .../fixtures/no-color/{issue_52.svg => fold_bad_origin_line.svg} | 0 .../no-color/{issue_52.toml => fold_bad_origin_line.toml} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename tests/fixtures/no-color/{issue_52.svg => fold_bad_origin_line.svg} (100%) rename tests/fixtures/no-color/{issue_52.toml => fold_bad_origin_line.toml} (100%) diff --git a/tests/fixtures/no-color/issue_52.svg b/tests/fixtures/no-color/fold_bad_origin_line.svg similarity index 100% rename from tests/fixtures/no-color/issue_52.svg rename to tests/fixtures/no-color/fold_bad_origin_line.svg diff --git a/tests/fixtures/no-color/issue_52.toml b/tests/fixtures/no-color/fold_bad_origin_line.toml similarity index 100% rename from tests/fixtures/no-color/issue_52.toml rename to tests/fixtures/no-color/fold_bad_origin_line.toml From 22e6c1c5f1363304324e03319c2270110b5ac6fb Mon Sep 17 00:00:00 2001 From: Ed Page Date: Thu, 21 Mar 2024 12:27:20 -0500 Subject: [PATCH 2/4] test(render): Clarify multiline cases As `fold` is special machinery, I made that test under that. The others, I couldn't tell what different cases they were covering, so I kept the names. I did name them with the `ann` prefix to highlight these are testing annotations. --- .../no-color/{multiline_annotation2.svg => ann_multiline.svg} | 0 .../no-color/{multiline_annotation2.toml => ann_multiline.toml} | 0 .../no-color/{multiline_annotation3.svg => ann_multiline2.svg} | 0 .../no-color/{multiline_annotation3.toml => ann_multiline2.toml} | 0 .../no-color/{multiline_annotation.svg => fold_ann_multiline.svg} | 0 .../{multiline_annotation.toml => fold_ann_multiline.toml} | 0 6 files changed, 0 insertions(+), 0 deletions(-) rename tests/fixtures/no-color/{multiline_annotation2.svg => ann_multiline.svg} (100%) rename tests/fixtures/no-color/{multiline_annotation2.toml => ann_multiline.toml} (100%) rename tests/fixtures/no-color/{multiline_annotation3.svg => ann_multiline2.svg} (100%) rename tests/fixtures/no-color/{multiline_annotation3.toml => ann_multiline2.toml} (100%) rename tests/fixtures/no-color/{multiline_annotation.svg => fold_ann_multiline.svg} (100%) rename tests/fixtures/no-color/{multiline_annotation.toml => fold_ann_multiline.toml} (100%) diff --git a/tests/fixtures/no-color/multiline_annotation2.svg b/tests/fixtures/no-color/ann_multiline.svg similarity index 100% rename from tests/fixtures/no-color/multiline_annotation2.svg rename to tests/fixtures/no-color/ann_multiline.svg diff --git a/tests/fixtures/no-color/multiline_annotation2.toml b/tests/fixtures/no-color/ann_multiline.toml similarity index 100% rename from tests/fixtures/no-color/multiline_annotation2.toml rename to tests/fixtures/no-color/ann_multiline.toml diff --git a/tests/fixtures/no-color/multiline_annotation3.svg b/tests/fixtures/no-color/ann_multiline2.svg similarity index 100% rename from tests/fixtures/no-color/multiline_annotation3.svg rename to tests/fixtures/no-color/ann_multiline2.svg diff --git a/tests/fixtures/no-color/multiline_annotation3.toml b/tests/fixtures/no-color/ann_multiline2.toml similarity index 100% rename from tests/fixtures/no-color/multiline_annotation3.toml rename to tests/fixtures/no-color/ann_multiline2.toml diff --git a/tests/fixtures/no-color/multiline_annotation.svg b/tests/fixtures/no-color/fold_ann_multiline.svg similarity index 100% rename from tests/fixtures/no-color/multiline_annotation.svg rename to tests/fixtures/no-color/fold_ann_multiline.svg diff --git a/tests/fixtures/no-color/multiline_annotation.toml b/tests/fixtures/no-color/fold_ann_multiline.toml similarity index 100% rename from tests/fixtures/no-color/multiline_annotation.toml rename to tests/fixtures/no-color/fold_ann_multiline.toml From a741b6bd90662651084067afa936499196381d9a Mon Sep 17 00:00:00 2001 From: Ed Page Date: Thu, 21 Mar 2024 12:46:37 -0500 Subject: [PATCH 3/4] test(render): Show existing leading / trailing behavior --- tests/fixtures/no-color/fold_leading.svg | 45 ++++++++++++++++++++++ tests/fixtures/no-color/fold_leading.toml | 26 +++++++++++++ tests/fixtures/no-color/fold_trailing.svg | 33 ++++++++++++++++ tests/fixtures/no-color/fold_trailing.toml | 25 ++++++++++++ 4 files changed, 129 insertions(+) create mode 100644 tests/fixtures/no-color/fold_leading.svg create mode 100644 tests/fixtures/no-color/fold_leading.toml create mode 100644 tests/fixtures/no-color/fold_trailing.svg create mode 100644 tests/fixtures/no-color/fold_trailing.toml diff --git a/tests/fixtures/no-color/fold_leading.svg b/tests/fixtures/no-color/fold_leading.svg new file mode 100644 index 0000000..c85f9d7 --- /dev/null +++ b/tests/fixtures/no-color/fold_leading.svg @@ -0,0 +1,45 @@ + + + + + + + error[E0308]: invalid type: integer `20`, expected a bool + + --> Cargo.toml:11:13 + + | + + 1 | [workspace] + + 2 | + + 3 | [package] + + 4 | name = "hello" + + ... + + 10 | [lints] + + 11 | workspace = 20 + + | ^^ + + | + + + + diff --git a/tests/fixtures/no-color/fold_leading.toml b/tests/fixtures/no-color/fold_leading.toml new file mode 100644 index 0000000..e3fc696 --- /dev/null +++ b/tests/fixtures/no-color/fold_leading.toml @@ -0,0 +1,26 @@ +[message] +level = "Error" +id = "E0308" +title = "invalid type: integer `20`, expected a bool" + +[[message.snippets]] +source = """ +[workspace] + +[package] +name = "hello" +version = "1.0.0" +license = "MIT" +rust-version = "1.70" +edition = "2021" + +[lints] +workspace = 20 +""" +line_start = 1 +origin = "Cargo.toml" +fold = true +[[message.snippets.annotations]] +label = "" +level = "Error" +range = [132, 134] diff --git a/tests/fixtures/no-color/fold_trailing.svg b/tests/fixtures/no-color/fold_trailing.svg new file mode 100644 index 0000000..15c9850 --- /dev/null +++ b/tests/fixtures/no-color/fold_trailing.svg @@ -0,0 +1,33 @@ + + + + + + + error[E0308]: invalid type: integer `20`, expected a lints table + + --> Cargo.toml:1:9 + + | + + 1 | lints = 20 + + | ^^ + + | + + + + diff --git a/tests/fixtures/no-color/fold_trailing.toml b/tests/fixtures/no-color/fold_trailing.toml new file mode 100644 index 0000000..8ee4c05 --- /dev/null +++ b/tests/fixtures/no-color/fold_trailing.toml @@ -0,0 +1,25 @@ +[message] +level = "Error" +id = "E0308" +title = "invalid type: integer `20`, expected a lints table" + +[[message.snippets]] +source = """ +lints = 20 + +[workspace] + +[package] +name = "hello" +version = "1.0.0" +license = "MIT" +rust-version = "1.70" +edition = "2021" +""" +line_start = 1 +origin = "Cargo.toml" +fold = true +[[message.snippets.annotations]] +label = "" +level = "Error" +range = [8, 10] From 894f734291bb499267d53ec34d8402fea7f18c84 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Thu, 21 Mar 2024 13:05:21 -0500 Subject: [PATCH 4/4] 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. --- src/renderer/display_list.rs | 55 +++++++++++++++++-- .../no-color/fold_bad_origin_line.svg | 10 ++-- tests/fixtures/no-color/fold_leading.svg | 20 ++----- tests/formatter.rs | 1 - 4 files changed, 57 insertions(+), 29 deletions(-) diff --git a/src/renderer/display_list.rs b/src/renderer/display_list.rs index 4d2836a..fa8274c 100644 --- a/src/renderer/display_list.rs +++ b/src/renderer/display_list.rs @@ -696,17 +696,19 @@ impl<'a> Iterator for CursorLines<'a> { } fn format_message( - snippet::Message { + message: snippet::Message<'_>, + term_width: usize, + anonymized_line_numbers: bool, + primary: bool, +) -> Vec> { + let snippet::Message { level, id, title, footer, snippets, - }: snippet::Message<'_>, - term_width: usize, - anonymized_line_numbers: bool, - primary: bool, -) -> Vec> { + } = message; + let mut sets = vec![]; let body = if !snippets.is_empty() || primary { vec![format_title(level, id, title)] @@ -715,6 +717,7 @@ fn format_message( }; for (idx, snippet) in snippets.into_iter().enumerate() { + let snippet = fold_prefix_suffix(snippet); sets.push(format_snippet( snippet, idx == 0, @@ -876,6 +879,46 @@ fn format_header<'a>( None } +fn fold_prefix_suffix(mut snippet: snippet::Snippet<'_>) -> snippet::Snippet<'_> { + if !snippet.fold { + return snippet; + } + + let ann_start = snippet + .annotations + .iter() + .map(|ann| ann.range.start) + .min() + .unwrap_or(0); + if let Some(before_new_start) = snippet.source[0..ann_start].rfind('\n') { + let new_start = before_new_start + 1; + + let line_offset = snippet.source[..new_start].lines().count(); + snippet.line_start += line_offset; + + snippet.source = &snippet.source[new_start..]; + + for ann in &mut snippet.annotations { + let range_start = ann.range.start - new_start; + let range_end = ann.range.end - new_start; + ann.range = range_start..range_end; + } + } + + let ann_end = snippet + .annotations + .iter() + .map(|ann| ann.range.end) + .max() + .unwrap_or(snippet.source.len()); + if let Some(end_offset) = snippet.source[ann_end..].find('\n') { + let new_end = ann_end + end_offset; + snippet.source = &snippet.source[..new_end]; + } + + snippet +} + fn fold_body(mut body: Vec>) -> Vec> { enum Line { Fold(usize), diff --git a/tests/fixtures/no-color/fold_bad_origin_line.svg b/tests/fixtures/no-color/fold_bad_origin_line.svg index 5ed2228..13a0834 100644 --- a/tests/fixtures/no-color/fold_bad_origin_line.svg +++ b/tests/fixtures/no-color/fold_bad_origin_line.svg @@ -1,4 +1,4 @@ - +