VS Code中根据第一行内容自定义语言关联

Custom language associations in VS Code based on the contents of the first line

使用 VS Code (v1.39.2) 当我打开第一行是 #!/bin/bash 的文件时,编辑器知道将语言设置为 Shell Script。有没有一种方法可以配置我自己的语言关联,以便将任意第一行与内置语言关联(例如 /bin/hello-world 选择 Perl)?

#!/bin/bash 部分称为 shebang。我不认为 VSCode 默认支持自定义 shebang,但您可以使用 Shebang Language Associator 来做您想做的事。您只需输入其设置并设置您想要的任何模式和语言。

示例

"shebang.associations": [
    {
        "pattern": "^#!/bin/bash$",
        "language": "shellscript"
    }
]

Shebang Language Associator 很棒,但是,您可以在应用程序中配置语言关联。在配置文件中添加:

    "files.associations": {
        "*.myphp": "php"
    }

    "languages": [{
        "id": "java",
        "extensions": [ ".java", ".jav" ],
        "aliases": [ "Java", "java" ]
    }]

使用语言标识符添加语言支持:

    "grammars": [{
        "language": "groovy",
        "scopeName": "source.groovy",
        "path": "./syntaxes/Groovy.tmLanguage.json"
    }],
    "snippets": [{
        "language": "groovy",
        "path": "./snippets/groovy.json"
    }]

希望对您有所帮助!