Skip to content

Commit

Permalink
Make command typable
Browse files Browse the repository at this point in the history
  • Loading branch information
MDeiml committed Oct 29, 2022
1 parent 5ce8f88 commit 865cd1b
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 29 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 @@ -45,6 +45,7 @@
| `:encoding` | Set encoding. Based on `https://encoding.spec.whatwg.org`. |
| `:reload` | Discard changes and reload from the source file. |
| `: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 |
| `: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. |
Expand Down
1 change: 0 additions & 1 deletion helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,6 @@ impl MappableCommand {
file_picker, "Open file picker",
file_picker_in_current_directory, "Open file picker at current working directory",
code_action, "Perform code action",
workspace_command_picker, "Open workspace command picker",
buffer_picker, "Open buffer picker",
jumplist_picker, "Open jumplist picker",
symbol_picker, "Open symbol picker",
Expand Down
28 changes: 0 additions & 28 deletions helix-term/src/commands/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -611,34 +611,6 @@ impl ui::menu::Item for lsp::Command {
}
}

pub fn workspace_command_picker(cx: &mut Context) {
let (_, doc) = current!(cx.editor);

let language_server = language_server!(cx.editor, doc);

let options = match &language_server.capabilities().execute_command_provider {
Some(options) => options,
None => return,
};
let commands = options
.commands
.iter()
.map(|command| lsp::Command {
title: command.clone(),
command: command.clone(),
arguments: None,
})
.collect::<Vec<_>>();
cx.callback = Some(Box::new(
|compositor: &mut Compositor, _cx: &mut compositor::Context| {
let picker = ui::Picker::new(commands, (), |cx, command, _action| {
execute_lsp_command(cx.editor, command.clone());
});
compositor.push(Box::new(overlayed(picker)))
},
));
}

pub fn execute_lsp_command(editor: &mut Editor, cmd: lsp::Command) {
let doc = doc!(editor);
let language_server = language_server!(editor, doc);
Expand Down
59 changes: 59 additions & 0 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1052,6 +1052,58 @@ fn update(
}
}

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

let (_, doc) = current!(cx.editor);

let language_server = match doc.language_server() {
Some(language_server) => language_server,
None => {
cx.editor
.set_status("Language server not active for current buffer");
return Ok(());
}
};

let options = match &language_server.capabilities().execute_command_provider {
Some(options) => options,
None => {
cx.editor
.set_status("Workspace commands are not supported for this language server");
return Ok(());
}
};
let commands = options
.commands
.iter()
.map(|command| helix_lsp::lsp::Command {
title: command.clone(),
command: command.clone(),
arguments: None,
})
.collect::<Vec<_>>();
let callback = async move {
let call: job::Callback = Callback::EditorCompositor(Box::new(
move |_editor: &mut Editor, compositor: &mut Compositor| {
let picker = ui::Picker::new(commands, (), |cx, command, _action| {
execute_lsp_command(cx.editor, command.clone());
});
compositor.push(Box::new(overlayed(picker)))
},
));
Ok(call)
};
cx.jobs.callback(callback);
Ok(())
}

fn lsp_restart(
cx: &mut compositor::Context,
_args: &[Cow<str>],
Expand Down Expand Up @@ -1987,6 +2039,13 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
fun: update,
completer: None,
},
TypableCommand {
name: "lsp-workspace-command",
aliases: &[],
doc: "Open workspace command picker",
fun: lsp_workspace_command,
completer: None,
},
TypableCommand {
name: "lsp-restart",
aliases: &[],
Expand Down

0 comments on commit 865cd1b

Please sign in to comment.