From 533b710417a070531865078d817b2a903cfd1d70 Mon Sep 17 00:00:00 2001 From: "Aviv \"RustyStriker\" Romem" Date: Mon, 22 Nov 2021 19:14:09 +0200 Subject: [PATCH] fix issue #1136 --- helix-term/src/commands.rs | 9 ++++++++ helix-view/src/editor.rs | 47 ++++++++++++++++++++++++++++++++++++++ temp.rs | 4 ++++ 3 files changed, 60 insertions(+) create mode 100644 temp.rs diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index fde505fdc015..f5554a2eb367 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -1816,9 +1816,13 @@ mod cmd { let jobs = &mut cx.jobs; let (_, doc) = current!(cx.editor); + // Should refresh lang server or not + let mut refresh_ls = false; + if let Some(path) = path { doc.set_path(Some(path.as_ref())) .context("invalid filepath")?; + refresh_ls = true; // we want to refresh the lang server } if doc.path().is_none() { bail!("cannot write a buffer without a filename"); @@ -1836,6 +1840,11 @@ mod cmd { }); let future = doc.format_and_save(fmt); cx.jobs.add(Job::new(future).wait_before_exiting()); + + if refresh_ls { + let id = doc.id(); + let _ = cx.editor.refresh_language_server(id); + } Ok(()) } diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index 1ce33760ed10..5ac177e5c426 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -271,6 +271,53 @@ impl Editor { Ok(()) } + /// Refreshes the language server for a given document + pub fn refresh_language_server(&mut self, doc_id: DocumentId) -> Option<()> { + // don' know why but it feels good using this pattern :) + let Editor { + documents, + language_servers, + theme, + syn_loader, + .. + } = self; + + let doc = documents.get_mut(&doc_id)?; + doc.detect_language(Some(&theme), &syn_loader); + let language_server = doc.language.as_ref().and_then(|language| { + language_servers + .get(language) + .map_err(|e| { + log::error!( + "Failed to initialize the LSP for `{}` {{ {} }}", + language.scope(), + e + ) + }) + .ok() + }); + + log::info!("ls is {:?}", language_server); + + if let Some(language_server) = language_server { + let language_id = doc + .language() + .and_then(|s| s.split('.').last()) // source.rust + .map(ToOwned::to_owned) + .unwrap_or_default(); + + // TODO: this now races with on_init code if the init happens too quickly + tokio::spawn(language_server.text_document_did_open( + doc.url().unwrap(), + doc.version(), + doc.text(), + language_id, + )); + + doc.set_language_server(Some(language_server)); + } + } + fn _refresh(&mut self) { for (view, _) in self.tree.views_mut() { let doc = &self.documents[&view.doc]; diff --git a/temp.rs b/temp.rs new file mode 100644 index 000000000000..162d0fe2eb53 --- /dev/null +++ b/temp.rs @@ -0,0 +1,4 @@ +fn main() { + let x : i32 = 0.1; +} +