aboutsummaryrefslogtreecommitdiff
path: root/helix-lsp
diff options
context:
space:
mode:
authorBlaž Hrastnik2020-12-23 08:03:20 +0000
committerBlaž Hrastnik2020-12-23 09:16:17 +0000
commitcd16df19c1f951e1ef0c560ae5e084b18bd2c713 (patch)
tree82b1431a96c914033fff8b79fcdcda9f535a16f9 /helix-lsp
parent56f2193811fca2fa20284442c2042fa271464445 (diff)
lsp: generate_transaction_from_text_edits
Diffstat (limited to 'helix-lsp')
-rw-r--r--helix-lsp/src/lib.rs29
-rw-r--r--helix-lsp/src/transport.rs4
2 files changed, 29 insertions, 4 deletions
diff --git a/helix-lsp/src/lib.rs b/helix-lsp/src/lib.rs
index c56721a5..27a5e046 100644
--- a/helix-lsp/src/lib.rs
+++ b/helix-lsp/src/lib.rs
@@ -28,19 +28,44 @@ pub enum Error {
pub mod util {
use super::*;
+ use helix_core::{RopeSlice, State, Transaction};
- pub fn lsp_pos_to_pos(doc: &helix_core::RopeSlice, pos: lsp::Position) -> usize {
+ pub fn lsp_pos_to_pos(doc: &RopeSlice, pos: lsp::Position) -> usize {
let line = doc.line_to_char(pos.line as usize);
let line_start = doc.char_to_utf16_cu(line);
doc.utf16_cu_to_char(pos.character as usize + line_start)
}
- pub fn pos_to_lsp_pos(doc: &helix_core::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_start = doc.char_to_utf16_cu(line);
let col = doc.char_to_utf16_cu(pos) - line_start;
lsp::Position::new(line as u32, col as u32)
}
+
+ pub fn generate_transaction_from_edits(
+ state: &State,
+ edits: Vec<lsp::TextEdit>,
+ ) -> Transaction {
+ let doc = state.doc.slice(..);
+ Transaction::change(
+ state,
+ edits.into_iter().map(|edit| {
+ // simplify "" into None for cleaner changesets
+ let replacement = if !edit.new_text.is_empty() {
+ Some(edit.new_text.into())
+ } else {
+ None
+ };
+
+ let start = lsp_pos_to_pos(&doc, edit.range.start);
+ let end = lsp_pos_to_pos(&doc, edit.range.end);
+ (start, end, replacement)
+ }),
+ )
+ }
+
+ // apply_insert_replace_edit
}
#[derive(Debug, PartialEq, Clone)]
diff --git a/helix-lsp/src/transport.rs b/helix-lsp/src/transport.rs
index 22af1b40..07c89fdd 100644
--- a/helix-lsp/src/transport.rs
+++ b/helix-lsp/src/transport.rs
@@ -107,6 +107,8 @@ impl Transport {
// read data
+ debug!("<- {}", msg);
+
// try parsing as output (server response) or call (server request)
let output: serde_json::Result<Message> = serde_json::from_str(&msg);
@@ -201,8 +203,6 @@ impl Transport {
}
let msg = msg.unwrap();
- debug!("<- {:?}", msg);
-
self.recv_msg(msg).await.unwrap();
}
}