lsp: Fix pos_to_lsp_pos calculation.

This commit is contained in:
Blaž Hrastnik 2020-12-25 17:42:50 +09:00
parent 2ab069bb3f
commit 3cbab20908
5 changed files with 19 additions and 26 deletions

View file

@ -5,7 +5,7 @@ use crate::{
type Result<T> = core::result::Result<T, Error>; type Result<T> = core::result::Result<T, Error>;
use helix_core::{ChangeSet, Rope, Transaction}; use helix_core::{ChangeSet, Rope, RopeSlice, Transaction};
// use std::collections::HashMap; // use std::collections::HashMap;
use std::sync::atomic::{AtomicU64, Ordering}; use std::sync::atomic::{AtomicU64, Ordering};
@ -244,7 +244,10 @@ impl Client {
.await .await
} }
fn to_changes(changeset: &ChangeSet) -> Vec<lsp::TextDocumentContentChangeEvent> { fn to_changes(
old_text: &Rope,
changeset: &ChangeSet,
) -> Vec<lsp::TextDocumentContentChangeEvent> {
let mut iter = changeset.changes().iter().peekable(); let mut iter = changeset.changes().iter().peekable();
let mut old_pos = 0; let mut old_pos = 0;
@ -253,9 +256,9 @@ impl Client {
use crate::util::pos_to_lsp_pos; use crate::util::pos_to_lsp_pos;
use helix_core::Operation::*; use helix_core::Operation::*;
// TEMP let old_text = old_text.slice(..);
let rope = helix_core::Rope::from("");
let old_text = rope.slice(..); // TODO: verify this function, specifically line num counting
while let Some(change) = iter.next() { while let Some(change) = iter.next() {
let len = match change { let len = match change {
@ -290,21 +293,6 @@ impl Client {
}; };
} }
Insert(s) => { Insert(s) => {
// TODO:
// 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
// stack backtrace:
// 0: rust_begin_unwind
// at /rustc/b32e6e6ac8921035177256ab6806e6ab0d4b9b94/library/std/src/panicking.rs:493:5
// 1: std::panicking::begin_panic_fmt
// at /rustc/b32e6e6ac8921035177256ab6806e6ab0d4b9b94/library/std/src/panicking.rs:435:5
// 2: ropey::slice::RopeSlice::char_to_line
// at /home/speed/.cargo/registry/src/github.com-1ecc6299db9ec823/ropey-1.2.0/src/slice.rs:301:9
// 3: helix_lsp::util::pos_to_lsp_pos
// at /home/speed/src/helix/helix-lsp/src/lib.rs:39:20
// 4: helix_lsp::client::Client::to_changes
// at /home/speed/src/helix/helix-lsp/src/client.rs:293:33
// 5: helix_lsp::client::Client::text_document_did_change::{{closure}}
// at /home/speed/src/helix/helix-lsp/src/client.rs:338:55
let start = pos_to_lsp_pos(&old_text, old_pos); let start = pos_to_lsp_pos(&old_text, old_pos);
// insert // insert
@ -325,6 +313,7 @@ impl Client {
pub async fn text_document_did_change( pub async fn text_document_did_change(
&self, &self,
text_document: lsp::VersionedTextDocumentIdentifier, text_document: lsp::VersionedTextDocumentIdentifier,
old_text: &Rope,
changes: &ChangeSet, changes: &ChangeSet,
) -> Result<()> { ) -> Result<()> {
// figure out what kind of sync the server supports // figure out what kind of sync the server supports
@ -350,7 +339,7 @@ impl Client {
text: "".to_string(), text: "".to_string(),
}] // TODO: probably need old_state here too? }] // TODO: probably need old_state here too?
} }
lsp::TextDocumentSyncKind::Incremental => Self::to_changes(changes), lsp::TextDocumentSyncKind::Incremental => Self::to_changes(old_text, changes),
lsp::TextDocumentSyncKind::None => return Ok(()), lsp::TextDocumentSyncKind::None => return Ok(()),
}; };

View file

@ -37,7 +37,7 @@ pub mod util {
} }
pub fn pos_to_lsp_pos(doc: &RopeSlice, pos: usize) -> lsp::Position { pub fn pos_to_lsp_pos(doc: &RopeSlice, pos: usize) -> lsp::Position {
let line = doc.char_to_line(pos); let line = doc.char_to_line(pos);
let line_start = doc.char_to_utf16_cu(line); let line_start = doc.char_to_utf16_cu(doc.line_to_char(line));
let col = doc.char_to_utf16_cu(pos) - line_start; let col = doc.char_to_utf16_cu(pos) - line_start;
lsp::Position::new(line as u32, col as u32) lsp::Position::new(line as u32, col as u32)

View file

@ -1,6 +1,6 @@
use std::collections::HashMap; use std::collections::HashMap;
use log::debug; use log::{debug, error};
use crate::{Error, Notification}; use crate::{Error, Notification};
@ -199,6 +199,7 @@ impl Transport {
// server <- client // server <- client
msg = Self::recv(&mut self.reader, &mut self.headers).fuse() => { msg = Self::recv(&mut self.reader, &mut self.headers).fuse() => {
if msg.is_err() { if msg.is_err() {
error!("err: <- {:?}", msg);
break; break;
} }
let msg = msg.unwrap(); let msg = msg.unwrap();

View file

@ -207,7 +207,7 @@ impl Application {
}), }),
); );
} }
_ => unreachable!(), e => unreachable!("{:?}", e),
} }
} }

View file

@ -193,8 +193,11 @@ impl Document {
// emit lsp notification // emit lsp notification
if let Some(language_server) = &self.language_server { if let Some(language_server) = &self.language_server {
let notify = language_server let notify = language_server.text_document_did_change(
.text_document_did_change(self.versioned_identifier(), transaction.changes()); self.versioned_identifier(),
&old_doc,
transaction.changes(),
);
smol::block_on(notify).expect("failed to emit textDocument/didChange"); smol::block_on(notify).expect("failed to emit textDocument/didChange");
} }