summaryrefslogtreecommitdiff
path: root/cli/lsp/analysis.rs
diff options
context:
space:
mode:
authorNayeem Rahman <nayeemrmn99@gmail.com>2024-04-16 00:07:32 +0100
committerGitHub <noreply@github.com>2024-04-16 01:07:32 +0200
commit9a31698207b8d4aeed0eb7ddd33eaac1da38b72a (patch)
tree347800d7d92b7b2276a6ea72f562c3aedd360664 /cli/lsp/analysis.rs
parent0b8d7d1d4b068df46a14895b2f55c00781bd1eef (diff)
fix(lsp): slice strings by byte index in code actions (#23387)
Fixes #23361.
Diffstat (limited to 'cli/lsp/analysis.rs')
-rw-r--r--cli/lsp/analysis.rs17
1 files changed, 14 insertions, 3 deletions
diff --git a/cli/lsp/analysis.rs b/cli/lsp/analysis.rs
index ce5d0c7f4..9da3fcad7 100644
--- a/cli/lsp/analysis.rs
+++ b/cli/lsp/analysis.rs
@@ -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"
+ );
+ }
}