Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
fix: improper handling of multi-column unicode characters
Elijah Potter1ae12b94911d
diff --git a/Cargo.lock b/Cargo.lockindex 1fa0e5ba3..5073125e0 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -584,6 +584,7 @@ dependencies = ["tree-sitter-swift","tree-sitter-toml","tree-sitter-typescript",+ "unicode-width",][[package]]diff --git a/harper-ls/Cargo.toml b/harper-ls/Cargo.tomlindex 166b553d9..b3e53e4c5 100644--- a/harper-ls/Cargo.toml+++ b/harper-ls/Cargo.toml@@ -35,3 +35,4 @@ tracing-subscriber = "0.3.18"resolve-path = "0.1.0"open = "5.1.1"futures = "0.3.30"+unicode-width = "0.1.11"diff --git a/harper-ls/src/pos_conv.rs b/harper-ls/src/pos_conv.rsindex 367765946..256356c78 100644--- a/harper-ls/src/pos_conv.rs+++ b/harper-ls/src/pos_conv.rs@@ -1,5 +1,6 @@use harper_core::Span;use tower_lsp::lsp_types::{Position, Range};+use unicode_width::UnicodeWidthChar;/// This module includes various conversions from the index-based [`Span`]s that/// Harper uses, and the Ranges that the LSP uses.@@ -20,11 +21,17 @@ fn index_to_position(source: &[char], index: usize) -> Position {.collect();let lines = newline_indices.len();- let cols = index - newline_indices.last().copied().unwrap_or(0);++ let last_newline_idx = newline_indices.last().copied().unwrap_or(0);++ let cols: usize = source[last_newline_idx..index]+ .iter()+ .map(|c| c.width().unwrap_or(0))+ .sum();Position {line: lines as u32,- character: cols as u32+ character: cols as u32,}}@@ -40,7 +47,17 @@ fn position_to_index(source: &[char], position: Position) -> usize {.last().unwrap_or(0);- line_start_idx + position.character as usize+ let mut traversed_cols = 0;++ for (traversed_chars, c) in source[line_start_idx..].iter().enumerate() {+ if traversed_cols == position.character as usize {+ return line_start_idx + traversed_chars;+ }++ traversed_cols += c.width().unwrap_or(0);+ }++ line_start_idx}pub fn range_to_span(source: &[char], range: Range) -> Span {@@ -62,7 +79,7 @@ mod tests {let start = Position {line: 0,- character: 4+ character: 4,};let i = position_to_index(&source, start);@@ -82,7 +99,7 @@ mod tests {let a = Position {line: 1,- character: 2+ character: 2,};let b = position_to_index(&source, a);