【问题标题】:Can I conditionally take over a key binding in vscode extension?我可以有条件地接管 vscode 扩展中的键绑定吗?
【发布时间】:2020-04-18 10:07:43
【问题描述】:

我正在使用与大多数语言不同的处理 cmets 的语法。它是 BASIC,这意味着评论切换来自:

10 PRINT "Hi"

到

10 REM PRINT "Hi"

所以目前我使用了以下(类型)的代码:

// register on vscode's default comment toggle
vscode.commands.registerCommand('editor.action.commentLine', () => {
  if (vscode.window.activeTextEditor.document.languageId !== 'nextbasic') {
    // if this isn't our language give control back…?
    return;
  }

  // … handle my own bespoke comment method
})

我遇到的问题是,一旦激活了扩展程序,all 评论切换会通过我的代码,而早期的 return 语句只会导致评论根本不起作用(在我的 @987654324 之外@ 语言)。

有谁知道是否有办法说:如果语言不匹配,让原来的command.action.commentLine 处理这个?

【问题讨论】:

    标签: visual-studio-code vscode-extensions


    【解决方案1】:

    我认为更简洁的解决方案是使用不同的 ID 创建命令,然后告诉您的用户为其添加键绑定或在 package.json 中使用 contributes.keybindings。此键绑定应具有检查活动语言 ID 的when 条件。

    "contributes": {
        "keybindings": [
            {
                "command": "myExtension.commentLine",
                "key": "ctrl+oem_2",
                "when": "editorLangId == 'nextbasic'"
            }
        ]
    }
    

    请注意,这不包括为内置 commentLine 命令自定义键绑定的用户。

    除此之外,您还可以监听 onDidChangeActiveEditor 事件并根据语言 ID 动态取消注册/注册您的命令(您可以使用注册时返回的 Disposable 取消注册)。我不确定内置命令是否会在注销时恢复,因为使用已经存在的 ID 注册命令有点麻烦。

    【讨论】:

    • 谢谢 - 采用这种方法并对其进行排序?(包贡献中的贡献键绑定)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-23
    • 1970-01-01
    • 2020-04-28
    • 1970-01-01
    • 2017-06-29
    • 2019-11-03
    • 2021-03-31
    相关资源
    最近更新 更多