Skip to content

Commit

Permalink
fix issue helix-editor#1136
Browse files Browse the repository at this point in the history
  • Loading branch information
RustyStriker committed Nov 22, 2021
1 parent 4238a84 commit 533b710
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
9 changes: 9 additions & 0 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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(())
}

Expand Down
47 changes: 47 additions & 0 deletions helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
4 changes: 4 additions & 0 deletions temp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() {
let x : i32 = 0.1;
}

0 comments on commit 533b710

Please sign in to comment.