在 Visual Studio 代码中,有没有办法将设置和键绑定的范围限定为语言模式?

In Visual Studio Code is there a way to scope settings and keybindings to a language mode?

VS Code 中,我知道您可以创建包含全局用户键绑定和设置的文件,并且您可以为键绑定和设置创建特定于工作区的文件,但它是吗?可以定义特定于语言模式的设置或键绑定吗?

例如,当我处于 F# 模式时,我希望 Alt + / 表示 FSI: Send Line,但不是当我处于降价模式或 JS 模式时。

而且我希望我的制表符在 Elm 模式下为 2 个空格,但在 C# 模式下为 4 个空格。

我知道您可以使用 when 子句定义键绑定,如下所示:

{
    "key": "alt+/",
    "command": "fsi.SendLine",
    "when": "resourceLangId == fsharp"
}

这是实现我所追求的目标的唯一方法吗?

似乎能够在他们自己的文件中的某处为模式 X 定义 settings/keybindings 是有意义的。我不喜欢语言模式行为分散在像这样的大全局文件中。

Visual Studio 代码已经声明,目前(2016-10-19)不支持语言模式设置,但正在考虑中。 (https://twitter.com/code/status/788301380557561857)

VS Code Github 存储库中有几个问题请求此功能的变体。

https://github.com/Microsoft/vscode/issues/13532

https://github.com/Microsoft/vscode/issues/1073

要根据编程语言自定义 VS Code 设置和键绑定,您可以使用 language identifier、文件扩展名或扩展名标识符。

对于设置,您可以使用 language entry(在 settings.json 中):

    "[fsharp]": {
        "editor.suggest.insertMode": "replace"
    }

对于键绑定,您可以使用以下 when clause contexts(在 keybindings.json 中):

editorLangId True when the editor's associated language Id matches.

    {
        "key": "ctrl+e",
        "command": "workbench.action.files.saveAs",
        "when": "editorLangId == fsharp"
    }

resourceLangId True when the Explorer or editor title language Id matches.

    {
        "key": "ctrl+e",
        "command": "workbench.action.files.saveAs",
        "when": "resourceLangId == fsharp"
    }

resourceExtname True when the Explorer or editor filename extension matches.

    {
        "key": "ctrl+e",
        "command": "workbench.action.files.saveAs",
        "when": "resourceExtname == .fs"
    }

extension True when the extension's ID matches.

    {
        "key": "ctrl+e",
        "command": "workbench.action.files.saveAs",
        "when": "extension == ionide.ionide-fsharp"
    }


注: 我没有测试所有这些条款,但 editorLangIdresourceExtname 对我来说很好。