From 75f62bc86f2ccf2f529d4faa45aed08adfefef78 Mon Sep 17 00:00:00 2001 From: Pascal Kuthe Date: Sun, 30 Apr 2023 23:44:06 +0200 Subject: fix didChange notifaction offset encoding --- helix-lsp/src/client.rs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) (limited to 'helix-lsp') diff --git a/helix-lsp/src/client.rs b/helix-lsp/src/client.rs index 89b714e2..840e7382 100644 --- a/helix-lsp/src/client.rs +++ b/helix-lsp/src/client.rs @@ -645,7 +645,11 @@ impl Client { // Calculation is therefore a bunch trickier. use helix_core::RopeSlice; - fn traverse(pos: lsp::Position, text: RopeSlice) -> lsp::Position { + fn traverse( + pos: lsp::Position, + text: RopeSlice, + offset_encoding: OffsetEncoding, + ) -> lsp::Position { let lsp::Position { mut line, mut character, @@ -662,7 +666,11 @@ impl Client { line += 1; character = 0; } else { - character += ch.len_utf16() as u32; + character += match offset_encoding { + OffsetEncoding::Utf8 => ch.len_utf8() as u32, + OffsetEncoding::Utf16 => ch.len_utf16() as u32, + OffsetEncoding::Utf32 => 1, + }; } } lsp::Position { line, character } @@ -683,7 +691,7 @@ impl Client { } Delete(_) => { let start = pos_to_lsp_pos(new_text, new_pos, offset_encoding); - let end = traverse(start, old_text.slice(old_pos..old_end)); + let end = traverse(start, old_text.slice(old_pos..old_end), offset_encoding); // deletion changes.push(lsp::TextDocumentContentChangeEvent { @@ -700,7 +708,8 @@ impl Client { // a subsequent delete means a replace, consume it let end = if let Some(Delete(len)) = iter.peek() { old_end = old_pos + len; - let end = traverse(start, old_text.slice(old_pos..old_end)); + let end = + traverse(start, old_text.slice(old_pos..old_end), offset_encoding); iter.next(); -- cgit v1.2.3-70-g09d2