Is it possible to associate a given language with

2020-02-12 02:03发布

问题:

Is it currently possible to associate a given language with a file extension that is not typically associated with that language?

Say I have a filetype *.foo, and I want to associate it with JavaScript for syntax highlighting. Does anyone know if this is currently possible with VSCode?

I am working in a language that is syntactically similar to Visual Basic, and want to associate it with that language type. I understand that you can assign a language after the file is opened, however this is cumbersome to do each time a file is opened.

In Sublime Text for example it is possible to select "Open all with current extension as...". Does this exist yet for VSCode?

EDIT: The Visual Studio Code team has added a proper way to add both themes and languages.

https://code.visualstudio.com/updates#_yo-code-streamlined-customizations-for-vs-code

回答1:

VSCode v1.0 officially adds the File to Language Association feature. Add the following into .vscode/settings.json:

"files.associations": {
    "*.foo": "javascript"
}

You may find more details in the Visual Studio Code 1.0.0 release notes “File to language association” section.



回答2:

You can do it yourself: For this example I'll add the ".ino" files to the C++ plugin.

Navigate to the folder containing the corresponding plugin: C:\Users\username\AppData\Local\Code\app-0.1.0\resources\app\plugins\vs.language.cpp

Open the ticino.plugin.json file and edit contributes.language.extension. In this case, you go from:

"extensions": [ ".cpp", ".c", ".cc", ".cxx", ".h", ".hpp", ".hh"],

to

"extensions": [ ".cpp", ".c", ".cc", ".cxx", ".h", ".hpp", ".hh", ".ino" ],


回答3:

There is an update to the answer of this question, so I wanted to update it.

The Visual Studio Code team released a proper way to add new Languages and Themes to the application using a Yeoman generator with TextMate tmBundles which is documented here:

https://code.visualstudio.com/updates#_yo-code-streamlined-customizations-for-vs-code

and here: https://code.visualstudio.com/updates#_customization-adding-language-colorization-bracket-matching



回答4:

[Edit: The above answer didn't work for me (strange) - but I reworked it to the following]

Open the file

C:\Users\<user>\AppData\Local\Code\app-0.1.3\resources\app\client\vs\workbench\workbench.main.js

replace

t.knownTextMimes={

with

t.knownTextMimes={".twig":"text/html",

We're simply adding the mime-type to the array of known text mimes.



回答5:

  • Go to File > Preferences > Settings
  • In the right tree-view, expand Text Editor and choose Files
  • In the Associations section click Edit in settings.json

In the right editor, you can add your associations. Here is a sample that adds the extension .hpp

{
  "files.associations": {
    "c++ header files": ".hpp"
  }
}