From 635eef76315c389017b1d41f443db50c89a8a2ba Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Mon, 30 Aug 2021 13:45:21 -0700 Subject: [PATCH] Fix panic computing indentation. --- src/lib.rs | 2 +- tests/edge-cases/indented_whitespace.json | 60 +++++++++++++++++++++++ tests/edge_cases.rs | 1 + 3 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 tests/edge-cases/indented_whitespace.json diff --git a/src/lib.rs b/src/lib.rs index 0be5f52..64d2f28 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -100,7 +100,7 @@ fn parse_snippet(span: &DiagnosticSpan) -> Option { .chars() .take_while(|&c| char::is_whitespace(c)) .count(); - std::cmp::min(indent, line.highlight_start) + std::cmp::min(indent, line.highlight_start - 1) }) .min()?; diff --git a/tests/edge-cases/indented_whitespace.json b/tests/edge-cases/indented_whitespace.json new file mode 100644 index 0000000..b25189a --- /dev/null +++ b/tests/edge-cases/indented_whitespace.json @@ -0,0 +1,60 @@ +{ + "message": "non-ASCII whitespace symbol '\\u{a0}' is not skipped", + "code": null, + "level": "warning", + "spans": + [ + { + "file_name": "lib.rs", + "byte_start": 26, + "byte_end": 28, + "line_start": 2, + "line_end": 2, + "column_start": 1, + "column_end": 2, + "is_primary": false, + "text": + [ + { + "text": " indented\";", + "highlight_start": 1, + "highlight_end": 2 + } + ], + "label": "non-ASCII whitespace symbol '\\u{a0}' is not skipped", + "suggested_replacement": null, + "suggestion_applicability": null, + "expansion": null + }, + { + "file_name": "lib.rs", + "byte_start": 24, + "byte_end": 28, + "line_start": 1, + "line_end": 2, + "column_start": 25, + "column_end": 2, + "is_primary": true, + "text": + [ + { + "text": "pub static FOO: &str = \"\\", + "highlight_start": 25, + "highlight_end": 26 + }, + { + "text": " indented\";", + "highlight_start": 1, + "highlight_end": 2 + } + ], + "label": null, + "suggested_replacement": null, + "suggestion_applicability": null, + "expansion": null + } + ], + "children": + [], + "rendered": "warning: non-ASCII whitespace symbol '\\u{a0}' is not skipped\n --> lib.rs:1:25\n |\n1 | pub static FOO: &str = \"\\\n | _________________________^\n2 | |  indented\";\n | | ^ non-ASCII whitespace symbol '\\u{a0}' is not skipped\n | |_|\n | \n\n" +} diff --git a/tests/edge_cases.rs b/tests/edge_cases.rs index 1b981ef..42d1e40 100644 --- a/tests/edge_cases.rs +++ b/tests/edge_cases.rs @@ -22,3 +22,4 @@ expect_empty_json_test! {out_of_bounds_test, "out_of_bounds.recorded.json"} expect_empty_json_test! {utf8_identifiers_test, "utf8_idents.recorded.json"} expect_empty_json_test! {empty, "empty.json"} expect_empty_json_test! {no_main, "no_main.json"} +expect_empty_json_test! {indented_whitespace, "indented_whitespace.json"}