on master help sign in
lsp: Work on syncing the state with the language server.
commit2ab069bb3fc815394cccca50378b62932f3146f5 authorBlaž Hrastnik <blaz@mxxn.io> dateFri 25 Dec 2020, 17:20 (6 years ago) parentcd16df1
8 files changed+92−35
Cargo.lock+2−1 helix-lsp/Cargo.toml−1 helix-lsp/src/client.rs+41−26 helix-term/src/application.rs+12−4 helix-term/src/commands.rs+5−1 helix-term/src/ui/editor.rs+2−1 helix-view/Cargo.toml+2 helix-view/src/document.rs+28−1
Cargo.lock+2−1
@@ -553,7 +553,6 @@ dependencies = [
553553 "futures-util",
554554 "glob",
555555 "helix-core",
556− "helix-view",
557556 "jsonrpc-core",
558557 "log",
559558 "lsp-types",
@@ -604,7 +603,9 @@ version = "0.1.0"
604603dependencies = [
605604 "anyhow",
606605 "crossterm",
606+ "futures-util",
607607 "helix-core",
608+ "helix-lsp",
608609 "once_cell",
609610 "smol",
610611 "tui",
helix-lsp/Cargo.toml−1
@@ -8,7 +8,6 @@ edition = "2018"
88
99[dependencies]
1010helix-core = { path = "../helix-core" }
11−helix-view = { path = "../helix-view" }
1211
1312once_cell = "1.4"
1413
helix-lsp/src/client.rs+41−26
@@ -5,8 +5,7 @@ use crate::{
55
66type Result<T> = core::result::Result<T, Error>;
77
8−use helix_core::{ChangeSet, Transaction};
9−use helix_view::Document;
8+use helix_core::{ChangeSet, Rope, Transaction};
109
1110// use std::collections::HashMap;
1211use std::sync::atomic::{AtomicU64, Ordering};
@@ -23,10 +22,6 @@ use smol::{
2322 Executor,
2423};
2524
26−fn text_document_identifier(doc: &Document) -> lsp::TextDocumentIdentifier {
27− lsp::TextDocumentIdentifier::new(lsp::Url::from_file_path(doc.path().unwrap()).unwrap())
28−}
29−
3025pub struct Client {
3126 _process: Child,
3227 stderr: BufReader<ChildStderr>,
@@ -232,13 +227,18 @@ impl Client {
232227 // Text document
233228 // -------------------------------------------------------------------------------------------
234229
235− pub async fn text_document_did_open(&self, doc: &Document) -> Result<()> {
230+ pub async fn text_document_did_open(
231+ &self,
232+ uri: lsp::Url,
233+ version: i32,
234+ doc: &Rope,
235+ ) -> Result<()> {
236236 self.notify::<lsp::notification::DidOpenTextDocument>(lsp::DidOpenTextDocumentParams {
237237 text_document: lsp::TextDocumentItem {
238− uri: lsp::Url::from_file_path(doc.path().unwrap()).unwrap(),
238+ uri,
239239 language_id: "rust".to_string(), // TODO: hardcoded for now
240− version: doc.version,
241− text: String::from(doc.text()),
240+ version,
241+ text: String::from(doc),
242242 },
243243 })
244244 .await
@@ -290,6 +290,21 @@ impl Client {
290290 };
291291 }
292292 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
293308 let start = pos_to_lsp_pos(&old_text, old_pos);
294309
295310 // insert
@@ -309,8 +324,8 @@ impl Client {
309324 // TODO: trigger any time history.commit_revision happens
310325 pub async fn text_document_did_change(
311326 &self,
312− doc: &Document,
313− transaction: &Transaction,
327+ text_document: lsp::VersionedTextDocumentIdentifier,
328+ changes: &ChangeSet,
314329 ) -> Result<()> {
315330 // figure out what kind of sync the server supports
316331
@@ -335,16 +350,12 @@ impl Client {
335350 text: "".to_string(),
336351 }] // TODO: probably need old_state here too?
337352 }
338− lsp::TextDocumentSyncKind::Incremental => Self::to_changes(transaction.changes()),
353+ lsp::TextDocumentSyncKind::Incremental => Self::to_changes(changes),
339354 lsp::TextDocumentSyncKind::None => return Ok(()),
340355 };
341356
342357 self.notify::<lsp::notification::DidChangeTextDocument>(lsp::DidChangeTextDocumentParams {
343− text_document: lsp::VersionedTextDocumentIdentifier::new(
344− // TODO: doc.into() Url
345− lsp::Url::from_file_path(doc.path().unwrap()).unwrap(),
346− doc.version,
347− ),
358+ text_document,
348359 content_changes: changes,
349360 })
350361 .await
@@ -352,9 +363,12 @@ impl Client {
352363
353364 // TODO: impl into() TextDocumentIdentifier / VersionedTextDocumentIdentifier for Document.
354365
355− pub async fn text_document_did_close(&self, doc: &Document) -> Result<()> {
366+ pub async fn text_document_did_close(
367+ &self,
368+ text_document: lsp::TextDocumentIdentifier,
369+ ) -> Result<()> {
356370 self.notify::<lsp::notification::DidCloseTextDocument>(lsp::DidCloseTextDocumentParams {
357− text_document: text_document_identifier(doc),
371+ text_document,
358372 })
359373 .await
360374 }
@@ -365,16 +379,17 @@ impl Client {
365379 unimplemented!()
366380 }
367381
368− pub async fn completion(&self, doc: &Document) -> anyhow::Result<Vec<lsp::CompletionItem>> {
382+ pub async fn completion(
383+ &self,
384+ text_document: lsp::TextDocumentIdentifier,
385+ position: lsp::Position,
386+ ) -> anyhow::Result<Vec<lsp::CompletionItem>> {
369387 // TODO: figure out what should happen when you complete with multiple cursors
370388
371389 let params = lsp::CompletionParams {
372390 text_document_position: lsp::TextDocumentPositionParams {
373− text_document: text_document_identifier(doc),
374− position: crate::util::pos_to_lsp_pos(
375− &doc.text().slice(..),
376− doc.selection().cursor(),
377− ),
391+ text_document,
392+ position,
378393 },
379394 // TODO: support these tokens by async receiving and updating the choice list
380395 work_done_progress_params: lsp::WorkDoneProgressParams {
helix-term/src/application.rs+12−4
@@ -44,16 +44,23 @@ impl Application {
4444 let mut editor = Editor::new();
4545 let size = terminal.size()?;
4646
47+ let language_servers = helix_lsp::Registry::new();
48+ let language_server = language_servers.get("rust", &executor).unwrap();
49+
4750 if let Some(file) = args.values_of_t::<PathBuf>("files").unwrap().pop() {
4851 editor.open(file, (size.width, size.height))?;
52+
53+ // TODO: do this everywhere
54+ editor
55+ .view_mut()
56+ .unwrap()
57+ .doc
58+ .set_language_server(Some(language_server.clone()));
4959 }
5060
5161 let mut compositor = Compositor::new();
5262 compositor.push(Box::new(ui::EditorView::new()));
5363
54− let language_servers = helix_lsp::Registry::new();
55− let language_server = language_servers.get("rust", &executor).unwrap();
56−
5764 let mut app = Self {
5865 editor,
5966 terminal,
@@ -90,8 +97,9 @@ impl Application {
9097 pub async fn event_loop(&mut self) {
9198 let mut reader = EventStream::new();
9299
100+ let doc = &self.editor.view().unwrap().doc;
93101 self.language_server
94− .text_document_did_open(&self.editor.view().unwrap().doc)
102+ .text_document_did_open(doc.url().unwrap(), doc.version, doc.text())
95103 .await
96104 .unwrap();
97105
helix-term/src/commands.rs+5−1
@@ -841,9 +841,13 @@ pub fn completion(cx: &mut Context) {
841841 use std::time::Duration;
842842
843843 // TODO: blocking here is not ideal
844+ let pos = helix_lsp::util::pos_to_lsp_pos(
845+ &cx.view.doc.text().slice(..),
846+ cx.view.doc.selection().cursor(),
847+ );
844848 let res = smol::block_on(
845849 language_server
846− .completion(&cx.view.doc)
850+ .completion(cx.view.doc.identifier(), pos)
847851 .timeout(Duration::from_secs(2)),
848852 )
849853 .expect("completion failed!")
helix-term/src/ui/editor.rs+2−1
@@ -41,7 +41,8 @@ impl EditorView {
4141 self.render_buffer(view, area, surface, theme);
4242
4343 // clear with background color
44− surface.set_style(viewport, theme.get("ui.background"));
44+ // TODO: this seems to prevent setting style later
45+ // surface.set_style(viewport, theme.get("ui.background"));
4546
4647 let area = Rect::new(0, viewport.height - 2, viewport.width, 1);
4748 self.render_statusline(view, area, surface, theme);
helix-view/Cargo.toml+2
@@ -13,6 +13,7 @@ default = ["term"]
1313[dependencies]
1414anyhow = "1"
1515helix-core = { path = "../helix-core" }
16+helix-lsp = { path = "../helix-lsp"}
1617
1718# Conversion traits
1819# tui = { version = "0.12", default-features = false, features = ["crossterm"], optional = true}
@@ -22,3 +23,4 @@ once_cell = "1.4"
2223url = "2"
2324
2425smol = "1"
26+futures-util = "0.3"
helix-view/src/document.rs+28−1
@@ -1,6 +1,7 @@
11use anyhow::Error;
22use std::future::Future;
33use std::path::{Path, PathBuf};
4+use std::sync::Arc;
45
56use helix_core::{
67 syntax::LOADER, ChangeSet, Diagnostic, History, Position, Range, Rope, RopeSlice, Selection,
@@ -35,6 +36,7 @@ pub struct Document {
3536 pub version: i32, // should be usize?
3637
3738 pub diagnostics: Vec<Diagnostic>,
39+ pub language_server: Option<Arc<helix_lsp::Client>>,
3840}
3941
4042/// Like std::mem::replace() except it allows the replacement value to be mapped from the
@@ -53,6 +55,8 @@ where
5355 }
5456}
5557
58+use futures_util::TryFutureExt;
59+use helix_lsp::lsp;
5660use url::Url;
5761
5862impl Document {
@@ -72,6 +76,7 @@ impl Document {
7276 diagnostics: Vec::new(),
7377 version: 0,
7478 history: History::default(),
79+ language_server: None,
7580 }
7681 }
7782
@@ -134,7 +139,7 @@ impl Document {
134139 // TODO: flush?
135140
136141 Ok(())
137− } // and_then(// lsp.send_text_saved_notification())
142+ } // and_then notify save
138143 }
139144
140145 pub fn set_language(&mut self, scope: &str, scopes: &[String]) {
@@ -148,6 +153,10 @@ impl Document {
148153 };
149154 }
150155
156+ pub fn set_language_server(&mut self, language_server: Option<Arc<helix_lsp::Client>>) {
157+ self.language_server = language_server;
158+ }
159+
151160 pub fn set_selection(&mut self, selection: Selection) {
152161 // TODO: use a transaction?
153162 self.state.selection = selection;
@@ -181,6 +190,14 @@ impl Document {
181190 }
182191
183192 // TODO: map state.diagnostics over changes::map_pos too
193+
194+ // emit lsp notification
195+ if let Some(language_server) = &self.language_server {
196+ let notify = language_server
197+ .text_document_did_change(self.versioned_identifier(), transaction.changes());
198+
199+ smol::block_on(notify).expect("failed to emit textDocument/didChange");
200+ }
184201 }
185202 success
186203 }
@@ -263,4 +280,14 @@ impl Document {
263280 // }
264281
265282 // TODO: transact(Fn) ?
283+
284+ // -- LSP methods
285+
286+ pub fn identifier(&self) -> lsp::TextDocumentIdentifier {
287+ lsp::TextDocumentIdentifier::new(self.url().unwrap())
288+ }
289+
290+ pub fn versioned_identifier(&self) -> lsp::VersionedTextDocumentIdentifier {
291+ lsp::VersionedTextDocumentIdentifier::new(self.url().unwrap(), self.version)
292+ }
266293}