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

feat: lsp-restart #533

Closed
wants to merge 2 commits into from
Closed
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
21 changes: 11 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1739,6 +1739,42 @@ mod cmd {
doc.reload(view.id)
}

fn lsp_restart(
cx: &mut compositor::Context,
_args: &[&str],
_event: PromptEvent,
) -> anyhow::Result<()> {
let view = cx.editor.tree.get(cx.editor.tree.focus);
let doc_id = view.doc;
let doc = cx.editor.document(doc_id).unwrap();
let language_server = match doc.language_server_arc() {
Some(language_server) => language_server,
None => return Err(anyhow!("running lsp server not found")),
};

log::debug!("got lspserver");
let fut = async move {
log::debug!("trying shutdown");
let shutdown_result = language_server.shutdown_and_exit().await;
if let Err(err) = shutdown_result {
log::debug!("shutdown & exit request returned error: {}", err); // we're not doing anything except logging, if LSP crashes then this error is expected
}
let callback: job::Callback = Box::new(move |editor: &mut Editor, _compositor| {
let doc = editor.document(doc_id).unwrap();
let language_config = doc.language_config_arc().unwrap();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for an arc here.

let language_server = editor.language_servers.get(&language_config).unwrap();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're getting the failures because language_servers.get() returns the same server you just shut down. We maintain one server instance per language because it's shared across files. I think what you really want to do is add a language_server.restart() method that will do this inside the server.

It's hard to get mutable access though, so I imagine we actually want to use arc-swap here to start a new server, arc-swap the new server in. It's a bit trickier than that, you also need to loop over all the documents and re-send textDocument/open events for all open files with that language server, as well as replay any changes since last file save (you can do this by taking the last saved version id, then replaying all the changes in the tree from that id to current id).

log::debug!("got new language server");
let doc = editor.document_mut(doc_id).unwrap();
doc.set_language_server(Some(language_server));
log::debug!("set new language server");
});
Ok::<job::Callback, anyhow::Error>(callback)
};
cx.jobs.callback(fut);

Ok(())
}

pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
TypableCommand {
name: "quit",
Expand Down Expand Up @@ -1935,6 +1971,13 @@ mod cmd {
doc: "Discard changes and reload from the source file.",
fun: reload,
completer: None,
},
TypableCommand {
name: "lsp-restart",
alias: None,
doc: "Restart current LSP connection.",
fun: lsp_restart,
completer: None,
}
];

Expand Down
9 changes: 9 additions & 0 deletions helix-view/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -973,6 +973,10 @@ impl Document {
self.language.as_deref()
}

pub fn language_config_arc(&self) -> Option<Arc<LanguageConfiguration>> {
self.language.clone()
}

/// Current document version, incremented at each change.
pub fn version(&self) -> i32 {
self.version
Expand All @@ -983,6 +987,11 @@ impl Document {
self.language_server.as_deref()
}

#[inline]
pub fn language_server_arc(&self) -> Option<Arc<helix_lsp::Client>> {
self.language_server.clone()
}

#[inline]
/// Tree-sitter AST tree
pub fn syntax(&self) -> Option<&Syntax> {
Expand Down