commands

A command is a function with a unique identifier. The function is sometimes also called command handler.

Command contains 2 parts: unique ID and custom function. Commands can be added to the editor using the registerCommand and registerTextEditorCommand functions.

Declare command

Menu: Associate commands to a menu through the menus extension point.

  1. Declare a command through the commands extension point, and then associate it with menus
  1. {
  2. "contributes":{
  3. "commands":[
  4. {
  5. "command":"extension.firstExtension",
  6. "title":"My First Extension"
  7. }
  8. ],
  9. "menus":{
  10. "editor/context":[
  11. {
  12. "command": "extension.firstExtension",
  13. "group": "z_commands",
  14. "when": "editorTextFocus"
  15. }
  16. ]
  17. }
  18. }
  19. }
  1. Register the command in the activation callback (activate) of the extension. This is a sample that registers a command handler and adds an entry for that command to the palette. First register a command handler with the identifier.
  1. hx.commands.registerCommand('extension.firstExtension',()=>{
  2. hx.window.showInformationMessage("Hello My First Extension.");
  3. });

Custom shortcuts

User-defined shortcut keys: Users who use the extension can learn the ID of the command (usually declared in package.json), and then configure a shortcut key through the custom shortcut key.

  1. // Keybindings.json;
  2. [
  3. {
  4. "key":"ctrl+shift+0",
  5. "command":"extension.firstExtension"
  6. }
  7. ]

executeCommand

Execute the command with the specified id. In addition to the commands extended by the extension, you can also execute the built-in commands of HBuilderX. The complete list of built-in commands can be through the top menu of HBuilderX tools-custom shortcut keys, and then on the left of the opened configuration file In the side section, find all the listed command fields, as shown below:

commands - 图1

Parameter
Parameter Type Description
command String Identifier of the command to execute.
Returns
Type Description
Promise Promise Object
Example
  1. //Execute the command for extension
  2. hx.commands.executeCommand('extension.firstExtension')
  3. //Execute built-in commands (close all open editors)
  4. hx.commands.executeCommand('workbench.action.closeAllEditors')

registerCommand

Registers a command that can be invoked via a keyboard shortcut, a menu item, an action, or directly.

Parameters
Parameter Type Description
commandId String A unique identifier for the command.
handler Function A command handler function.
Returns
Type Description
Disposable Disposable which unregisters this command on disposal.
Example
  1. let disposable = hx.commands.registerCommand('extension.firstExtension',()=>{
  2. hx.window.showInformationMessage("Hello My First Extension.");
  3. });
  4. context.subscriptions.push(disposable);