Skip to content

Commit 26665f8

Browse files
davxysagnibak-imc
authored andcommitted
Allow LSP server to be stopped (helix-editor#5964)
1 parent a1f1c3b commit 26665f8

3 files changed

Lines changed: 49 additions & 0 deletions

File tree

book/src/generated/typable-cmd.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
| `:update` | Write changes only if the file has been modified. |
5050
| `:lsp-workspace-command` | Open workspace command picker |
5151
| `:lsp-restart` | Restarts the Language Server that is in use by the current doc |
52+
| `:lsp-stop` | Stops the Language Server that is in use by the current doc |
5253
| `:tree-sitter-scopes` | Display tree sitter scopes, primarily for theming and development. |
5354
| `:debug-start`, `:dbg` | Start a debug session from a given template with given parameters. |
5455
| `:debug-remote`, `:dbg-tcp` | Connect to a debug adapter by TCP address and start a debugging session from a given template with given parameters. |

helix-lsp/src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,16 @@ impl Registry {
476476
}
477477
}
478478

479+
pub fn stop(&mut self, language_config: &LanguageConfiguration) {
480+
let scope = language_config.scope.clone();
481+
482+
if let Some((_, client)) = self.inner.remove(&scope) {
483+
tokio::spawn(async move {
484+
let _ = client.force_shutdown().await;
485+
});
486+
}
487+
}
488+
479489
pub fn get(
480490
&mut self,
481491
language_config: &LanguageConfiguration,

helix-term/src/commands/typed.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,6 +1354,37 @@ fn lsp_restart(
13541354
Ok(())
13551355
}
13561356

1357+
fn lsp_stop(
1358+
cx: &mut compositor::Context,
1359+
_args: &[Cow<str>],
1360+
event: PromptEvent,
1361+
) -> anyhow::Result<()> {
1362+
if event != PromptEvent::Validate {
1363+
return Ok(());
1364+
}
1365+
1366+
let doc = doc!(cx.editor);
1367+
1368+
let ls_id = doc
1369+
.language_server()
1370+
.map(|ls| ls.id())
1371+
.context("LSP not running for the current document")?;
1372+
1373+
let config = doc
1374+
.language_config()
1375+
.context("LSP not defined for the current document")?;
1376+
cx.editor.language_servers.stop(config);
1377+
1378+
for doc in cx.editor.documents_mut() {
1379+
if doc.language_server().map_or(false, |ls| ls.id() == ls_id) {
1380+
doc.set_language_server(None);
1381+
doc.set_diagnostics(Default::default());
1382+
}
1383+
}
1384+
1385+
Ok(())
1386+
}
1387+
13571388
fn tree_sitter_scopes(
13581389
cx: &mut compositor::Context,
13591390
_args: &[Cow<str>],
@@ -2349,6 +2380,13 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
23492380
fun: lsp_restart,
23502381
completer: None,
23512382
},
2383+
TypableCommand {
2384+
name: "lsp-stop",
2385+
aliases: &[],
2386+
doc: "Stops the Language Server that is in use by the current doc",
2387+
fun: lsp_stop,
2388+
completer: None,
2389+
},
23522390
TypableCommand {
23532391
name: "tree-sitter-scopes",
23542392
aliases: &[],

0 commit comments

Comments
 (0)