on master help sign in
lsp: Fix pos_to_lsp_pos calculation.
3cbab20Blaž Hrastnik
helix-lsp/src/client.rs+10−21
@@ -5,7 +5,7 @@ use crate::{
5
5
6type Result<T> = core::result::Result<T, Error>;
6type Result<T> = core::result::Result<T, Error>;
7
7
8−use helix_core::{ChangeSet, Rope, Transaction};
8+use helix_core::{ChangeSet, Rope, RopeSlice, Transaction};
9
9
10// use std::collections::HashMap;
10// use std::collections::HashMap;
11use std::sync::atomic::{AtomicU64, Ordering};
11use std::sync::atomic::{AtomicU64, Ordering};
@@ -244,7 +244,10 @@ impl Client {
244 .await
244 .await
245 }
245 }
246
246
247+ fn to_changes(
248+ old_text: &Rope,
249+ changeset: &ChangeSet,
247− fn to_changes(changeset: &ChangeSet) -> Vec<lsp::TextDocumentContentChangeEvent> {
250+ ) -> Vec<lsp::TextDocumentContentChangeEvent> {
248 let mut iter = changeset.changes().iter().peekable();
251 let mut iter = changeset.changes().iter().peekable();
249 let mut old_pos = 0;
252 let mut old_pos = 0;
250
253
@@ -253,9 +256,9 @@ impl Client {
253 use crate::util::pos_to_lsp_pos;
256 use crate::util::pos_to_lsp_pos;
254 use helix_core::Operation::*;
257 use helix_core::Operation::*;
255
258
256− // TEMP
259+ let old_text = old_text.slice(..);
257− let rope = helix_core::Rope::from("");
260+
258− let old_text = rope.slice(..);
261+ // TODO: verify this function, specifically line num counting
259
262
260 while let Some(change) = iter.next() {
263 while let Some(change) = iter.next() {
261 let len = match change {
264 let len = match change {
@@ -290,21 +293,6 @@ impl Client {
290 };
293 };
291 }
294 }
292 Insert(s) => {
295 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
308 let start = pos_to_lsp_pos(&old_text, old_pos);
296 let start = pos_to_lsp_pos(&old_text, old_pos);
309
297
310 // insert
298 // insert
@@ -325,6 +313,7 @@ impl Client {
325 pub async fn text_document_did_change(
313 pub async fn text_document_did_change(
326 &self,
314 &self,
327 text_document: lsp::VersionedTextDocumentIdentifier,
315 text_document: lsp::VersionedTextDocumentIdentifier,
316+ old_text: &Rope,
328 changes: &ChangeSet,
317 changes: &ChangeSet,
329 ) -> Result<()> {
318 ) -> Result<()> {
330 // figure out what kind of sync the server supports
319 // figure out what kind of sync the server supports
@@ -350,7 +339,7 @@ impl Client {
350 text: "".to_string(),
339 text: "".to_string(),
351 }] // TODO: probably need old_state here too?
340 }] // TODO: probably need old_state here too?
352 }
341 }
353− lsp::TextDocumentSyncKind::Incremental => Self::to_changes(changes),
342+ lsp::TextDocumentSyncKind::Incremental => Self::to_changes(old_text, changes),
354 lsp::TextDocumentSyncKind::None => return Ok(()),
343 lsp::TextDocumentSyncKind::None => return Ok(()),
355 };
344 };
356
345
helix-lsp/src/lib.rs+1−1
@@ -37,7 +37,7 @@ pub mod util {
37 }
37 }
38 pub fn pos_to_lsp_pos(doc: &RopeSlice, pos: usize) -> lsp::Position {
38 pub fn pos_to_lsp_pos(doc: &RopeSlice, pos: usize) -> lsp::Position {
39 let line = doc.char_to_line(pos);
39 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));
41 let col = doc.char_to_utf16_cu(pos) - line_start;
41 let col = doc.char_to_utf16_cu(pos) - line_start;
42
42
43 lsp::Position::new(line as u32, col as u32)
43 lsp::Position::new(line as u32, col as u32)
helix-lsp/src/transport.rs+2−1
@@ -1,6 +1,6 @@
1use std::collections::HashMap;
1use std::collections::HashMap;
2
2
3−use log::debug;
3+use log::{debug, error};
4
4
5use crate::{Error, Notification};
5use crate::{Error, Notification};
6
6
@@ -199,6 +199,7 @@ impl Transport {
199 // server <- client
199 // server <- client
200 msg = Self::recv(&mut self.reader, &mut self.headers).fuse() => {
200 msg = Self::recv(&mut self.reader, &mut self.headers).fuse() => {
201 if msg.is_err() {
201 if msg.is_err() {
202+ error!("err: <- {:?}", msg);
202 break;
203 break;
203 }
204 }
204 let msg = msg.unwrap();
205 let msg = msg.unwrap();
helix-term/src/application.rs+1−1
@@ -207,7 +207,7 @@ impl Application {
207 }),
207 }),
208 );
208 );
209 }
209 }
210− _ => unreachable!(),
210+ e => unreachable!("{:?}", e),
211 }
211 }
212 }
212 }
213
213
helix-view/src/document.rs+5−2
@@ -193,8 +193,11 @@ impl Document {
193
193
194 // emit lsp notification
194 // emit lsp notification
195 if let Some(language_server) = &self.language_server {
195 if let Some(language_server) = &self.language_server {
196− let notify = language_server
196+ let notify = language_server.text_document_did_change(
197− .text_document_did_change(self.versioned_identifier(), transaction.changes());
197+ self.versioned_identifier(),
198+ &old_doc,
199+ transaction.changes(),
200+ );
198
201
199 smol::block_on(notify).expect("failed to emit textDocument/didChange");
202 smol::block_on(notify).expect("failed to emit textDocument/didChange");
200 }
203 }