Skip to content

Commit

Permalink
fix(language_server): calculate correct column when Unicode chars (#7484
Browse files Browse the repository at this point in the history
  • Loading branch information
Sysix authored Nov 26, 2024
1 parent 87c893f commit 571d7e2
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions crates/oxc_language_server/src/linter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,12 +360,14 @@ impl IsolatedLintHandler {
#[allow(clippy::cast_possible_truncation)]
fn offset_to_position(offset: usize, source_text: &str) -> Option<Position> {
let rope = Rope::from_str(source_text);
let line = rope.try_byte_to_line(offset).ok()?;
let first_char_of_line = rope.try_line_to_char(line).ok()?;
// Original offset is byte, but Rope uses char offset
let offset = rope.try_byte_to_char(offset).ok()?;
let column = offset - first_char_of_line;
Some(Position::new(line as u32, column as u32))
// Get line number and byte offset of start of line
let line_index = rope.try_byte_to_line(offset).ok()?;
let line_offset = rope.try_line_to_byte(line_index).ok()?;

// Get column number
let column_index = source_text[line_offset..offset].encode_utf16().count();

Some(Position::new(line_index as u32, column_index as u32))
}

pub struct ServerLinter {
Expand Down

0 comments on commit 571d7e2

Please sign in to comment.