Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix didChange notification offset encoding #6921

Merged
merged 1 commit into from
May 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions helix-lsp/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 }
Expand All @@ -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 {
Expand All @@ -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();

Expand Down