注册命令与快捷键

使用 TradingAICommands 注册命令,在 plugin.yaml 中声明快捷键。

注册命令

dart
await context.commands.registerCommand(
  id: 'my_plugin.hello',
  title: 'Say Hello',
  category: 'My Plugin',
  callback: (args) async {
    await context.window.showMessage('Hello from My Plugin!');
  }
);

接收参数

回调可接收可选参数(由调用方传入):

dart
await context.commands.registerCommand(
  id: 'my_plugin.openFile',
  title: 'Open File',
  callback: (args) async {
    final path = args?['path'] as String?;
    if (path != null) {
      // 处理 path
    }
  }
);

在 plugin.yaml 中声明

命令需在 contributes.commands 中声明,以便在命令面板显示;快捷键在 contributes.keybindings 中配置:

yaml
contributes:
  commands:
    - id: my_plugin.hello
      title: Say Hello
      category: My Plugin
    - id: my_plugin.openFile
      title: Open File
  keybindings:
    - command: my_plugin.hello
      key: ctrl+shift+h
    - command: my_plugin.openFile
      key: ctrl+alt+o

状态栏触发命令

StatusBar 项可绑定命令,点击时执行:

dart
await context.statusBar.createItem(
  id: 'my_plugin.status',
  text: 'Click me',
  command: 'my_plugin.hello',
  alignment: 'right'
);

参考

相关 APITradingAICommands

示例:extensions/market_dashboard_plugin/bin/main.dart 的 _registerCommands 方法;plugin.yaml 的 contributes.commandskeybindings 配置。