Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Monaco Editor - Difference between `Action` and `Command`?

In a monaco editor instance, you can call addAction oraddCommand

Both provide the ability to execute a function based on key events

The online playground offers examples of how to add a command or how to add an action to an editor instance.

Example of an Action

editor.addAction({
    id: 'my-unique-id',
    label: 'My Label!!!',
    keybindings: [monaco.KeyMod.CtrlCmd | monaco.KeyCode.Enter],
    run: function() { alert('action called') }
});

Example of a Command

editor.addCommand(
  monaco.KeyMod.CtrlCmd | monaco.KeyCode.Enter,
  function() { alert('called command') }
)

The documentation describes the type definitions, properties, and methods.

However, it's lacking when it comes to providing the context of when to use each, where they show up within the editor, and what each provides.

What's the difference between actions and commands?

like image 532
KyleMit Avatar asked Jun 21 '26 18:06

KyleMit


2 Answers

My personal impression is that actions are kinda evolved commands. Commands have been introduced first and later it was recognized that more functionality was needed (e.g. context menu integration), so Action was created. For compatibility reasons Command could not be removed by then, so both still exist today.

like image 146
Mike Lischke Avatar answered Jun 24 '26 08:06

Mike Lischke


There are some key differences between Action and Command:

  • Action is editor-specific. You add an action with the addAction method of monaco.editor.IStandaloneCodeEditor. Command can be added with the addCommand method of IStandaloneCodeEditor which makes it editor-specific too (the added command is anonymous and can only be triggered by a key binding); however, there is another (more common) way to register a command: via the global monaco.editor.registerCommand function. This way you can register a named command for all the editors;
  • Action appears and can be triggered from the command palette (F1 in monaco editor, or Ctrl+Shift+P in vscode), while Command does not (despite it being called a command palette);
  • Some language providers, such as monaco.languages.CodeActionProvider, can only utilize Commands. E.g. to provide a custom refactor feature, you have to register a Command and provide it in the CodeActionProvider to make it appear in the refactor menu.

Therefore Action and Command are different abstractions, one does not replace another.

like image 33
hillin Avatar answered Jun 24 '26 09:06

hillin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!