关于vscode设置:如何使VS Code将其他文件扩展名视为某种语言?

How to make VS Code to treat other file extensions as certain language?

还是有一种方法可以切换当前文件的语言以使用语法突出显示功能?

例如,*.jsx实际上使用JavaScript,但是VS Code无法识别它。


在Visual Studio Code中,您可以将用于语言突出显示的持久文件关联添加到settings.json文件中,如下所示:

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,然后键入settings JSON。选择"首选项:打开设置(JSON)"以打开settings.json

Files: Associations功能最初是在Visual Studio Code版本1.0(2016年3月)中引入的。检查发行说明中的??可用通配符模式以及文档中的已知语言字符串。


按住Ctrl + Shift + P(在Mac上为cmd),选择"更改语言模式"即可。

但是我仍然找不到一种方法来制作具有某些特定语言的特定扩展名的VS Code识别文件。


对于全局关联,我找到的最简单的方法就是将ctrl + k m(或ctrl + shift + p并输入"更改语言模式")与您要关联的文件类型打开。

在第一个选择中将是"为'x'配置文件关联"(无论文件类型-参见附件)
选择此选项将使文件类型关联永久化

enter image description here

自从最初的问题和接受的答案(我不知道何时更改)以来,这可能已经改变(可能已经改变),但是它比接受的答案和其他一些答案中的手动编辑步骤容易得多,并且完全避免了带有可能不太明显的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"
  }
}


这对我有用。

enter image description here

1
2
3
{
"files.associations": {"*.bitesize":"yaml"}
 }

例如,这将使以.variables.overrides结尾的文件与任何其他LESS文件一样被对待。就代码着色而言,就(自动)格式而言。根据需要定义用户设置或项目设置。

>
</p>
<p><center> <script src=

(如果您想知道,语义UI使用这些奇怪的扩展名)


我在这里找到了解决方案:https://code.visualstudio.com/docs/customization/colorizer

转到VS_CODE_FOLDER/resources/app/extensions/,然后更新package.json


遵循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的自定义支持不会在更新过程中丢失。