How to make VS Code to treat other file extensions as certain language?
还是有一种方法可以切换当前文件的语言以使用语法突出显示功能?
例如,
在Visual Studio Code中,您可以将用于语言突出显示的持久文件关联添加到
1 2 3 4 5 6 7 8 9 10 11 | // Place your settings in this file to overwrite the default settings { "some_setting": custom_value, ... "files.associations": { "*.thor":"ruby", "*.jsx":"javascript", "Jenkinsfile*":"groovy" } } |
您可以使用Ctrl + Shift + p,然后键入
按住Ctrl + Shift + P(在Mac上为cmd),选择"更改语言模式"即可。
但是我仍然找不到一种方法来制作具有某些特定语言的特定扩展名的VS Code识别文件。
对于全局关联,我找到的最简单的方法就是将ctrl + k m(或ctrl + shift + p并输入"更改语言模式")与您要关联的文件类型打开。
在第一个选择中将是"为'x'配置文件关联"(无论文件类型-参见附件)
选择此选项将使文件类型关联永久化
自从最初的问题和接受的答案(我不知道何时更改)以来,这可能已经改变(可能已经改变),但是它比接受的答案和其他一些答案中的手动编辑步骤容易得多,并且完全避免了带有可能不太明显的ID的音乐。
例如:
1 2 3 4 5 6 7 8 9 | // .vscode/settings.json in workspace { "files.associations": { "*Container.js":"javascriptreact", "**/components/*/*.js":"javascriptreact", "**/config/routes.js":"javascriptreact" } } |
这对我有用。
1 2 3 | { "files.associations": {"*.bitesize":"yaml"} } |
例如,这将使以

(如果您想知道,语义UI使用这些奇怪的扩展名)
我在这里找到了解决方案:https://code.visualstudio.com/docs/customization/colorizer
转到
遵循https://code.visualstudio.com/docs/customization/colorizer#_common-questions上的步骤对我来说效果很好:
To extend an existing colorizer, you would create a simple
package.json in a new folder under .vscode/extensions and provide the
extensionDependencies attribute specifying the customization you want
to add to. In the example below, an extension .mmd is added to the
markdown colorizer. Note that not only must the extensionDependency
name match the customization but also the language id must match the
language id of the colorizer you are extending.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | { "name":"MyMarkdown", "version":"0.0.1", "engines": { "vscode":"0.10.x" }, "publisher":"none", "extensionDependencies": [ "markdown" ], "contributes": { "languages": [{ "id":"markdown", "aliases": ["mmd"], "extensions": [".mmd"] }] } } |
我采用了不同的方法来解决几乎相同的问题,就我而言,我做了一个新扩展,增加了对Drupal特定文件(例如.module和.inc)的PHP语法突出显示支持:https://github.com。 com / mastazi / VS-code-drupal
如您在代码中所见,我创建了一个新扩展而不是修改现有的PHP扩展。显然,我在Drupal扩展中声明了对PHP扩展的依赖。
这样做的好处是,如果PHP扩展有更新,则我对Drupal的自定义支持不会在更新过程中丢失。