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 all 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
7 changes: 6 additions & 1 deletion helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1816,7 +1816,7 @@ mod cmd {
let jobs = &mut cx.jobs;
let (_, doc) = current!(cx.editor);

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

if path.is_some() {
let id = doc.id();
let _ = cx.editor.refresh_language_server(id);
}
Ok(())
}

Expand Down
79 changes: 48 additions & 31 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<()> {
let doc = self.documents.get_mut(&doc_id)?;
doc.detect_language(Some(&self.theme), &self.syn_loader);
Self::launch_language_server(&mut self.language_servers, doc)
}

/// Launch a language server for a given document
fn launch_language_server(ls: &mut helix_lsp::Registry, doc: &mut Document) -> Option<()> {
// try to find a language server based on the language name
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 Some(language_server.id()) != doc.language_server().map(|server| server.id()) {
if let Some(language_server) = doc.language_server() {
tokio::spawn(language_server.text_document_did_close(doc.identifier()));
}
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 @@ -403,37 +450,7 @@ impl Editor {
} else {
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
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 _ = Self::launch_language_server(&mut self.language_servers, &mut doc);

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