mirror of
https://github.com/denoland/deno.git
synced 2024-11-21 20:38:55 +00:00
fix(lsp): slice strings by byte index in code actions (#23387)
Fixes #23361.
This commit is contained in:
parent
9905ff9514
commit
e7d005f95a
@ -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"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user