fix(lsp): slice strings by byte index in code actions (#23387)

Fixes #23361.
This commit is contained in:
Nayeem Rahman 2024-04-16 00:07:32 +01:00 committed by GitHub
parent 0b8d7d1d4b
commit 9a31698207
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1126,9 +1126,11 @@ impl CodeActionCollection {
/// Prepend the whitespace characters found at the start of line_content to content.
fn prepend_whitespace(content: String, line_content: Option<String>) -> String {
if let Some(line) = line_content {
let whitespaces =
line.chars().position(|c| !c.is_whitespace()).unwrap_or(0);
let whitespace = &line[0..whitespaces];
let whitespace_end = line
.char_indices()
.find_map(|(i, c)| (!c.is_whitespace()).then_some(i))
.unwrap_or(0);
let whitespace = &line[0..whitespace_end];
format!("{}{}", &whitespace, content)
} else {
content
@ -1271,4 +1273,13 @@ mod tests {
"utils/sub_utils"
);
}
#[test]
fn test_prepend_whitespace() {
// Regression test for https://github.com/denoland/deno/issues/23361.
assert_eq!(
&prepend_whitespace("foo".to_string(), Some("\u{a0}bar".to_string())),
"\u{a0}foo"
);
}
}