Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix issue #1136 #1141

Merged
merged 9 commits into from
Nov 28, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1816,6 +1816,9 @@ mod cmd {
let jobs = &mut cx.jobs;
let (_, doc) = current!(cx.editor);

// Should refresh lang server or not
let refresh_ls = path.is_some();

if let Some(path) = path {
doc.set_path(Some(path.as_ref()))
.context("invalid filepath")?;
Expand All @@ -1836,6 +1839,11 @@ mod cmd {
});
let future = doc.format_and_save(fmt);
cx.jobs.add(Job::new(future).wait_before_exiting());

if refresh_ls {
RustyStriker marked this conversation as resolved.
Show resolved Hide resolved
let id = doc.id();
let _ = cx.editor.refresh_language_server(id);
}
Ok(())
}

Expand Down
82 changes: 52 additions & 30 deletions helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,57 @@ impl Editor {
Ok(())
}

/// Refreshes the language server for a given document
pub fn refresh_language_server(&mut self, doc_id: DocumentId) -> Option<()> {
let doc = self.documents.get_mut(&doc_id)?;
doc.detect_language(Some(&self.theme), &self.syn_loader);
Editor::launch_language_server(&mut self.language_servers, doc)
}

fn launch_language_server(ls: &mut helix_lsp::Registry, doc: &mut Document) -> Option<()> {
let language_server = doc.language.as_ref().and_then(|language| {
ls.get(language)
.map_err(|e| {
log::error!(
"Failed to initialize the LSP for `{}` {{ {} }}",
language.scope(),
e
)
})
.ok()
});
if let Some(language_server) = language_server {
// only spawn a new lang server if the servers aren't the same
if language_server.id()
!= doc
.language_server()
.map(|ls| ls.id())
.unwrap_or(usize::MAX)
RustyStriker marked this conversation as resolved.
Show resolved Hide resolved
{
// we also want to kill the previous server
RustyStriker marked this conversation as resolved.
Show resolved Hide resolved
if let Some(ls) = doc.language_server() {
tokio::spawn(ls.text_document_did_close(doc.identifier()));
RustyStriker marked this conversation as resolved.
Show resolved Hide resolved
}
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));
}
}
Some(())
}

fn _refresh(&mut self) {
for (view, _) in self.tree.views_mut() {
let doc = &self.documents[&view.doc];
Expand Down Expand Up @@ -404,36 +455,7 @@ impl Editor {
let mut doc = Document::open(&path, None, Some(&self.theme), Some(&self.syn_loader))?;

// try to find a language server based on the language name
RustyStriker marked this conversation as resolved.
Show resolved Hide resolved
let language_server = doc.language.as_ref().and_then(|language| {
self.language_servers
.get(language)
.map_err(|e| {
log::error!(
"Failed to initialize the LSP for `{}` {{ {} }}",
language.scope(),
e
)
})
.ok()
});

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));
}
let _ = Editor::launch_language_server(&mut self.language_servers, &mut doc);
RustyStriker marked this conversation as resolved.
Show resolved Hide resolved

let id = DocumentId(self.next_document_id);
self.next_document_id += 1;
Expand Down