Ask questionsRegistering/Providing custom Commands/CodeActions
I am creating an editor which uses some language server implementation in JS. It provides two methods: getCodeActions(...): Command[] and executeCommand(...): .... The first one gets the possible code actions on a position. This is essentially what the CodeActionprovider#provideCodeActions method does. The latter gets the TextEdits when a command is actually executed (and applies them to the workspace using applyEdit). There is no pendant in the public monaco API for that.
As provideCodeActions can also return CodeAction[], it is possible to compute the TextEdits while providing the code actions. However, as this happens on basically every key press, this could get really heavy.
I've tried using non-public API. In this approach, I'm registering the commands and the handlers as soon as the editor is initialized:
(editor as any)._commandService.addCommand({
id: "command-id",
handler: (_: any, ...args: any[]) => {
return myLspService.executeCommand("command-id", args);
},
});
As this is a private API this feels very hacky. Is there a better solution for this? If not, what is the recommended way of doing this?
I think the easiest way for Monaco's API consumers would be to extend the CodeActionProvider to something like this:
export interface CodeActionProvider {
provideCodeActions(
model: editor.ITextModel,
range: Range,
context: CodeActionContext,
token: CancellationToken
): (Command | CodeAction)[] | Thenable<(Command | CodeAction)[]>;
executeCommand(
model: editor.ITextModel,
command: string,
args: any[],
token: CancellationToken
): CodeAction[] | Thenable<CodeAction[]>; // or return void | Thenable<void> and apply the command using model.pushEditOperations()
}
Answer questions
ToonvanStrijp
@jrieken, @nikeee can you explain me how I add conditions to the command when using:
(editor as any)._commandService.addCommand({
id: "command-id",
handler: (_: any, ...args: any[]) => {
return myLspService.executeCommand("command-id", args);
},
});
Related questions