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 Bartek Iwańczuk
parent 9905ff9514
commit e7d005f95a
No known key found for this signature in database
GPG Key ID: 0C6BCDDC3B3AD750

View File

@ -1126,9 +1126,11 @@ impl CodeActionCollection {
/// Prepend the whitespace characters found at the start of line_content to content. /// Prepend the whitespace characters found at the start of line_content to content.
fn prepend_whitespace(content: String, line_content: Option<String>) -> String { fn prepend_whitespace(content: String, line_content: Option<String>) -> String {
if let Some(line) = line_content { if let Some(line) = line_content {
let whitespaces = let whitespace_end = line
line.chars().position(|c| !c.is_whitespace()).unwrap_or(0); .char_indices()
let whitespace = &line[0..whitespaces]; .find_map(|(i, c)| (!c.is_whitespace()).then_some(i))
.unwrap_or(0);
let whitespace = &line[0..whitespace_end];
format!("{}{}", &whitespace, content) format!("{}{}", &whitespace, content)
} else { } else {
content content
@ -1271,4 +1273,13 @@ mod tests {
"utils/sub_utils" "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"
);
}
} }