1
0
Fork 0
mirror of https://github.com/helix-editor/helix synced 2024-06-01 07:46:05 +02:00

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 @@
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::sync::atomic::{AtomicU64, Ordering};
@ -244,7 +244,10 @@ pub async fn text_document_did_open(
.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 old_pos = 0;
@ -253,9 +256,9 @@ fn to_changes(changeset: &ChangeSet) -> Vec<lsp::TextDocumentContentChangeEvent>
use crate::util::pos_to_lsp_pos;
use helix_core::Operation::*;
// TEMP
let rope = helix_core::Rope::from("");
let old_text = rope.slice(..);
let old_text = old_text.slice(..);
// TODO: verify this function, specifically line num counting
while let Some(change) = iter.next() {
let len = match change {
@ -290,21 +293,6 @@ fn to_changes(changeset: &ChangeSet) -> Vec<lsp::TextDocumentContentChangeEvent>
};
}
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);
// insert
@ -325,6 +313,7 @@ fn to_changes(changeset: &ChangeSet) -> Vec<lsp::TextDocumentContentChangeEvent>
pub async fn text_document_did_change(
&self,
text_document: lsp::VersionedTextDocumentIdentifier,
old_text: &Rope,
changes: &ChangeSet,
) -> Result<()> {
// figure out what kind of sync the server supports
@ -350,7 +339,7 @@ pub async fn text_document_did_change(
text: "".to_string(),
}] // 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(()),
};

View File

@ -37,7 +37,7 @@ pub fn lsp_pos_to_pos(doc: &RopeSlice, pos: lsp::Position) -> usize {
}
pub fn pos_to_lsp_pos(doc: &RopeSlice, pos: usize) -> lsp::Position {
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;
lsp::Position::new(line as u32, col as u32)

View File

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

View File

@ -207,7 +207,7 @@ pub async fn handle_language_server_message(&mut self, call: Option<helix_lsp::C
}),
);
}
_ => unreachable!(),
e => unreachable!("{:?}", e),
}
}

View File

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