From 3e5f24a9d5cec26697a75e515bff46de418b32da Mon Sep 17 00:00:00 2001 From: Blaž Hrastnik Date: Wed, 14 Apr 2021 15:30:15 +0900 Subject: lsp: support both utf-8 and utf-16 offsets. Still need to implement the clangd encoding negotiation, but it's a start. Should also manually override to utf8 for pyls. --- helix-lsp/src/client.rs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'helix-lsp/src/client.rs') diff --git a/helix-lsp/src/client.rs b/helix-lsp/src/client.rs index 64070be0..c70e6e78 100644 --- a/helix-lsp/src/client.rs +++ b/helix-lsp/src/client.rs @@ -1,6 +1,6 @@ use crate::{ transport::{Payload, Transport}, - Call, Error, Result, + Call, Error, OffsetEncoding, Result, }; use helix_core::{ChangeSet, Rope}; @@ -29,8 +29,7 @@ pub struct Client { pub request_counter: AtomicU64, capabilities: Option, - // TODO: handle PublishDiagnostics Version - // diagnostics: HashMap>, + offset_encoding: OffsetEncoding, } impl Client { @@ -70,6 +69,7 @@ impl Client { capabilities: None, // diagnostics: HashMap::new(), + offset_encoding: OffsetEncoding::Utf8, }; // TODO: async client.initialize() @@ -100,6 +100,10 @@ impl Client { .expect("language server not yet initialized!") } + pub fn offset_encoding(&self) -> OffsetEncoding { + self.offset_encoding + } + /// Execute a RPC request on the language server. pub async fn request(&self, params: R::Params) -> Result where @@ -291,6 +295,7 @@ impl Client { old_text: &Rope, new_text: &Rope, changeset: &ChangeSet, + offset_encoding: OffsetEncoding, ) -> Vec { let mut iter = changeset.changes().iter().peekable(); let mut old_pos = 0; @@ -340,7 +345,7 @@ impl Client { new_pos += i; } Delete(_) => { - let start = pos_to_lsp_pos(new_text, new_pos); + let start = pos_to_lsp_pos(new_text, new_pos, offset_encoding); let end = traverse(start, old_text.slice(old_pos..old_end)); // deletion @@ -351,7 +356,7 @@ impl Client { }); } Insert(s) => { - let start = pos_to_lsp_pos(new_text, new_pos); + let start = pos_to_lsp_pos(new_text, new_pos, offset_encoding); new_pos += s.chars().count(); @@ -413,7 +418,7 @@ impl Client { }] } lsp::TextDocumentSyncKind::Incremental => { - Self::changeset_to_changes(old_text, new_text, changes) + Self::changeset_to_changes(old_text, new_text, changes, self.offset_encoding) } lsp::TextDocumentSyncKind::None => return Ok(()), }; -- cgit v1.2.3-70-g09d2