lsp: generate_transaction_from_text_edits

pull/8/head
Blaž Hrastnik 4 years ago
parent 56f2193811
commit cd16df19c1

11
Cargo.lock generated

@ -594,6 +594,7 @@ dependencies = [
"num_cpus", "num_cpus",
"once_cell", "once_cell",
"smol", "smol",
"smol-timeout",
"tui", "tui",
] ]
@ -1142,6 +1143,16 @@ dependencies = [
"once_cell", "once_cell",
] ]
[[package]]
name = "smol-timeout"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "847d777e2c6c166bad26264479e80a9820f3d364fcb4a0e23cd57bbfa8e94961"
dependencies = [
"async-io",
"pin-project-lite",
]
[[package]] [[package]]
name = "socket2" name = "socket2"
version = "0.3.18" version = "0.3.18"

@ -28,19 +28,44 @@ pub enum Error {
pub mod util { pub mod util {
use super::*; 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 = doc.line_to_char(pos.line as usize);
let line_start = doc.char_to_utf16_cu(line); let line_start = doc.char_to_utf16_cu(line);
doc.utf16_cu_to_char(pos.character as usize + line_start) 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 = doc.char_to_line(pos);
let line_start = doc.char_to_utf16_cu(line); let line_start = doc.char_to_utf16_cu(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)
} }
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)] #[derive(Debug, PartialEq, Clone)]

@ -107,6 +107,8 @@ impl Transport {
// read data // read data
debug!("<- {}", msg);
// try parsing as output (server response) or call (server request) // try parsing as output (server response) or call (server request)
let output: serde_json::Result<Message> = serde_json::from_str(&msg); let output: serde_json::Result<Message> = serde_json::from_str(&msg);
@ -201,8 +203,6 @@ impl Transport {
} }
let msg = msg.unwrap(); let msg = msg.unwrap();
debug!("<- {:?}", msg);
self.recv_msg(msg).await.unwrap(); self.recv_msg(msg).await.unwrap();
} }
} }

@ -20,6 +20,7 @@ anyhow = "1"
once_cell = "1.4" once_cell = "1.4"
smol = "1" smol = "1"
smol-timeout = "0.6"
num_cpus = "1" num_cpus = "1"
# tui = { version = "0.12", default-features = false, features = ["crossterm"] } # tui = { version = "0.12", default-features = false, features = ["crossterm"] }
tui = { git = "https://github.com/fdehau/tui-rs", default-features = false, features = ["crossterm"] } tui = { git = "https://github.com/fdehau/tui-rs", default-features = false, features = ["crossterm"] }

@ -837,17 +837,32 @@ pub fn completion(cx: &mut Context) {
let language_server = cx.language_servers.get("rust", &cx.executor).unwrap(); let language_server = cx.language_servers.get("rust", &cx.executor).unwrap();
use log::info; use log::info;
use smol_timeout::TimeoutExt;
use std::time::Duration;
// TODO: blocking here is not ideal // TODO: blocking here is not ideal
let res = smol::block_on(language_server.completion(&cx.view.doc)).expect("completion failed!"); let res = smol::block_on(
language_server
.completion(&cx.view.doc)
.timeout(Duration::from_secs(2)),
)
.expect("completion failed!")
.expect("completion failed!");
let picker = ui::Picker::new( let picker = ui::Picker::new(
res, res,
|item| { |item| {
// format_fn // format_fn
item.label.as_str().into() item.label.as_str().into()
// TODO: use item.filter_text for filtering
}, },
|editor: &mut Editor, item| { |editor: &mut Editor, item| {
// if item.text_edit is Some we use that, else
// let insert_text = &item.insert_text.unwrap_or(item.label);
// and we insert at position.
// //
// merge this with additional_text_edits
}, },
); );

Loading…
Cancel
Save