Skip to content

Commit

Permalink
Allow LSP server to be stopped (helix-editor#5964)
Browse files Browse the repository at this point in the history
  • Loading branch information
davxy authored and Sagnik Bhattacharya committed Mar 21, 2023
1 parent a1f1c3b commit 26665f8
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions book/src/generated/typable-cmd.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
| `:update` | Write changes only if the file has been modified. |
| `:lsp-workspace-command` | Open workspace command picker |
| `:lsp-restart` | Restarts the Language Server that is in use by the current doc |
| `:lsp-stop` | Stops the Language Server that is in use by the current doc |
| `:tree-sitter-scopes` | Display tree sitter scopes, primarily for theming and development. |
| `:debug-start`, `:dbg` | Start a debug session from a given template with given parameters. |
| `:debug-remote`, `:dbg-tcp` | Connect to a debug adapter by TCP address and start a debugging session from a given template with given parameters. |
Expand Down
10 changes: 10 additions & 0 deletions helix-lsp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,16 @@ impl Registry {
}
}

pub fn stop(&mut self, language_config: &LanguageConfiguration) {
let scope = language_config.scope.clone();

if let Some((_, client)) = self.inner.remove(&scope) {
tokio::spawn(async move {
let _ = client.force_shutdown().await;
});
}
}

pub fn get(
&mut self,
language_config: &LanguageConfiguration,
Expand Down
38 changes: 38 additions & 0 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1354,6 +1354,37 @@ fn lsp_restart(
Ok(())
}

fn lsp_stop(
cx: &mut compositor::Context,
_args: &[Cow<str>],
event: PromptEvent,
) -> anyhow::Result<()> {
if event != PromptEvent::Validate {
return Ok(());
}

let doc = doc!(cx.editor);

let ls_id = doc
.language_server()
.map(|ls| ls.id())
.context("LSP not running for the current document")?;

let config = doc
.language_config()
.context("LSP not defined for the current document")?;
cx.editor.language_servers.stop(config);

for doc in cx.editor.documents_mut() {
if doc.language_server().map_or(false, |ls| ls.id() == ls_id) {
doc.set_language_server(None);
doc.set_diagnostics(Default::default());
}
}

Ok(())
}

fn tree_sitter_scopes(
cx: &mut compositor::Context,
_args: &[Cow<str>],
Expand Down Expand Up @@ -2349,6 +2380,13 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
fun: lsp_restart,
completer: None,
},
TypableCommand {
name: "lsp-stop",
aliases: &[],
doc: "Stops the Language Server that is in use by the current doc",
fun: lsp_stop,
completer: None,
},
TypableCommand {
name: "tree-sitter-scopes",
aliases: &[],
Expand Down

0 comments on commit 26665f8

Please sign in to comment.