on master help sign in
lsp: Fix pos_to_lsp_pos calculation.
commit3cbab2090839ef0d0ca7d7d6ef01404e48ddc8fa authorBlaž Hrastnik <blaz@mxxn.io> dateFri 25 Dec 2020, 17:42 (6 years ago) parent2ab069b
5 files changed+19−26
helix-lsp/src/client.rs+10−21 helix-lsp/src/lib.rs+1−1 helix-lsp/src/transport.rs+2−1 helix-term/src/application.rs+1−1 helix-view/src/document.rs+5−2
helix-lsp/src/client.rs+10−21
@@ -5,7 +5,7 @@ use crate::{
55
66type Result<T> = core::result::Result<T, Error>;
77
8−use helix_core::{ChangeSet, Rope, Transaction};
8+use helix_core::{ChangeSet, Rope, RopeSlice, Transaction};
99
1010// use std::collections::HashMap;
1111use std::sync::atomic::{AtomicU64, Ordering};
@@ -244,7 +244,10 @@ impl Client {
244244 .await
245245 }
246246
247− fn to_changes(changeset: &ChangeSet) -> Vec<lsp::TextDocumentContentChangeEvent> {
247+ fn to_changes(
248+ old_text: &Rope,
249+ changeset: &ChangeSet,
250+ ) -> Vec<lsp::TextDocumentContentChangeEvent> {
248251 let mut iter = changeset.changes().iter().peekable();
249252 let mut old_pos = 0;
250253
@@ -253,9 +256,9 @@ impl Client {
253256 use crate::util::pos_to_lsp_pos;
254257 use helix_core::Operation::*;
255258
256− // TEMP
257− let rope = helix_core::Rope::from("");
258− let old_text = rope.slice(..);
259+ let old_text = old_text.slice(..);
260+
261+ // TODO: verify this function, specifically line num counting
259262
260263 while let Some(change) = iter.next() {
261264 let len = match change {
@@ -290,21 +293,6 @@ impl Client {
290293 };
291294 }
292295 Insert(s) => {
293− // TODO:
294− // thread 'main' panicked at 'Attempt to index past end of slice: char index 1211, slice char length 0', /home/speed/.cargo/registry/src/github.com-1ecc6299db9ec823/ropey-1.2.0/src/slice.rs:301:9
295− // stack backtrace:
296− // 0: rust_begin_unwind
297− // at /rustc/b32e6e6ac8921035177256ab6806e6ab0d4b9b94/library/std/src/panicking.rs:493:5
298− // 1: std::panicking::begin_panic_fmt
299− // at /rustc/b32e6e6ac8921035177256ab6806e6ab0d4b9b94/library/std/src/panicking.rs:435:5
300− // 2: ropey::slice::RopeSlice::char_to_line
301− // at /home/speed/.cargo/registry/src/github.com-1ecc6299db9ec823/ropey-1.2.0/src/slice.rs:301:9
302− // 3: helix_lsp::util::pos_to_lsp_pos
303− // at /home/speed/src/helix/helix-lsp/src/lib.rs:39:20
304− // 4: helix_lsp::client::Client::to_changes
305− // at /home/speed/src/helix/helix-lsp/src/client.rs:293:33
306− // 5: helix_lsp::client::Client::text_document_did_change::{{closure}}
307− // at /home/speed/src/helix/helix-lsp/src/client.rs:338:55
308296 let start = pos_to_lsp_pos(&old_text, old_pos);
309297
310298 // insert
@@ -325,6 +313,7 @@ impl Client {
325313 pub async fn text_document_did_change(
326314 &self,
327315 text_document: lsp::VersionedTextDocumentIdentifier,
316+ old_text: &Rope,
328317 changes: &ChangeSet,
329318 ) -> Result<()> {
330319 // figure out what kind of sync the server supports
@@ -350,7 +339,7 @@ impl Client {
350339 text: "".to_string(),
351340 }] // TODO: probably need old_state here too?
352341 }
353− lsp::TextDocumentSyncKind::Incremental => Self::to_changes(changes),
342+ lsp::TextDocumentSyncKind::Incremental => Self::to_changes(old_text, changes),
354343 lsp::TextDocumentSyncKind::None => return Ok(()),
355344 };
356345
helix-lsp/src/lib.rs+1−1
@@ -37,7 +37,7 @@ pub mod util {
3737 }
3838 pub fn pos_to_lsp_pos(doc: &RopeSlice, pos: usize) -> lsp::Position {
3939 let line = doc.char_to_line(pos);
40− let line_start = doc.char_to_utf16_cu(line);
40+ let line_start = doc.char_to_utf16_cu(doc.line_to_char(line));
4141 let col = doc.char_to_utf16_cu(pos) - line_start;
4242
4343 lsp::Position::new(line as u32, col as u32)
helix-lsp/src/transport.rs+2−1
@@ -1,6 +1,6 @@
11use std::collections::HashMap;
22
3−use log::debug;
3+use log::{debug, error};
44
55use crate::{Error, Notification};
66
@@ -199,6 +199,7 @@ impl Transport {
199199 // server <- client
200200 msg = Self::recv(&mut self.reader, &mut self.headers).fuse() => {
201201 if msg.is_err() {
202+ error!("err: <- {:?}", msg);
202203 break;
203204 }
204205 let msg = msg.unwrap();
helix-term/src/application.rs+1−1
@@ -207,7 +207,7 @@ impl Application {
207207 }),
208208 );
209209 }
210− _ => unreachable!(),
210+ e => unreachable!("{:?}", e),
211211 }
212212 }
213213
helix-view/src/document.rs+5−2
@@ -193,8 +193,11 @@ impl Document {
193193
194194 // emit lsp notification
195195 if let Some(language_server) = &self.language_server {
196− let notify = language_server
197− .text_document_did_change(self.versioned_identifier(), transaction.changes());
196+ let notify = language_server.text_document_did_change(
197+ self.versioned_identifier(),
198+ &old_doc,
199+ transaction.changes(),
200+ );
198201
199202 smol::block_on(notify).expect("failed to emit textDocument/didChange");
200203 }