【问题标题】:Can I add a mouse click callback to decorated text?我可以为装饰文本添加鼠标点击回调吗?
【发布时间】:2019-10-28 21:44:12
【问题描述】:

我有一个装饰应用于文本部分(与正则表达式匹配)它可以选择指定hoverMessage 我想知道是否有一种方法可以指定鼠标点击这个元素。我想根据用户点击的装饰文本执行一些功能。

【问题讨论】:

    标签: javascript visual-studio-code vscode-extensions


    【解决方案1】:

    我找不到鼠标点击的任何解决方案。 other SO 问题中提到公开鼠标事件不太可能添加到 API :(

    我能做的是:

    • 添加贡献点:
      • 命令
      • 菜单
        • 编辑/上下文
        • commandPalette
    • 在js中注册命令并从textEditor获取位置

    package.json 中,我正在设置我的命令,因此当打开的文件是.log 文件时,它仅在提供title 的情况下显示在上下文菜单中。

      "contributes": {
        "commands": [
          {
            "command": "gd.my-ext.myCommand",
            "title": "My Command"
          }
        ],
        "menus": {
          "commandPalette": [
            {
              "command": "gd.me-ext.myCommand",
              "when": "false"
            }
          ],
          "editor/context": [
            {
              "command": "gd.me-ext.myCommand",
              "group": "navigation",
              "when": "resourceExtname == .log"
            },
          ]
        },
    

    然后在extension.ts 中注册一个文本编辑器命令并使用textEditorselectiondocument

    const disposable = vscode.commands.registerTextEditorCommand(
        'gd.me-ext.myCommand',
        (textEditor, edit, ...rest): void => {
          const { selection, document } = textEditor;
          console.log(selection.start.line);
          const line: vscode.TextLine = document.lineAt(selection.start.line);
          // The selection will tell me where the cursor is and I can find a line
          // in which I can use the regex I've used to apply decoration and compare if it was selection I wanted :)
        }
      );
      context.subscriptions.push(disposable);
    

    【讨论】:

      猜你喜欢
      • 2022-06-27
      • 1970-01-01
      • 1970-01-01
      • 2016-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多