Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions src/renderer/source_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -779,18 +779,24 @@ pub(crate) fn as_substr<'a>(
original: &'a str,
suggestion: &'a str,
) -> Option<(usize, &'a str, usize)> {
let common_prefix = original
.chars()
.zip(suggestion.chars())
.take_while(|(c1, c2)| c1 == c2)
.map(|(c, _)| c.len_utf8())
.sum();
let original = &original[common_prefix..];
let suggestion = &suggestion[common_prefix..];
if let Some(stripped) = suggestion.strip_suffix(original) {
let common_suffix = original.len();
Some((common_prefix, stripped, common_suffix))
if let Some(stripped) = suggestion.strip_prefix(original) {
Some((original.len(), stripped, 0))
} else if let Some(stripped) = suggestion.strip_suffix(original) {
Some((0, stripped, original.len()))
} else {
None
let common_prefix = original
.chars()
.zip(suggestion.chars())
.take_while(|(c1, c2)| c1 == c2)
.map(|(c, _)| c.len_utf8())
.sum();
let original = &original[common_prefix..];
let suggestion = &suggestion[common_prefix..];
if let Some(stripped) = suggestion.strip_suffix(original) {
let common_suffix = original.len();
Some((common_prefix, stripped, common_suffix))
} else {
None
}
}
}
49 changes: 49 additions & 0 deletions tests/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5015,3 +5015,52 @@ help: consider importing this module
let renderer = renderer.decor_style(DecorStyle::Unicode);
assert_data_eq!(renderer.render(input), expected_unicode);
}

#[test]
fn original_matches_replacement_suffix() {
let source = r#"use sync;"#;
let input = &[
Group::with_level(Level::ERROR).element(
Snippet::source(source).path("/tmp/test.rs").annotation(
AnnotationKind::Primary
.span(4..8)
.label("no `sync` in the root"),
),
),
Level::HELP
.secondary_title("consider importing this module instead")
.element(
Snippet::source(source)
.path("/tmp/test.rs")
.patch(Patch::new(4..8, "std::sync")),
),
];

let expected_ascii = str![[r#"
--> /tmp/test.rs:1:5
|
1 | use sync;
| ^^^^ no `sync` in the root
|
help: consider importing this module instead
|
1 | use std::sync;
| +++++
"#]];
let renderer = Renderer::plain();
assert_data_eq!(renderer.render(input), expected_ascii);

let expected_unicode = str![[r#"
╭▸ /tmp/test.rs:1:5
1 │ use sync;
│ ━━━━ no `sync` in the root
╰╴
help: consider importing this module instead
╭╴
1 │ use std::sync;
╰╴ +++++
"#]];
let renderer = renderer.decor_style(DecorStyle::Unicode);
assert_data_eq!(renderer.render(input), expected_unicode);
}
Loading