Search code examples
vscode-extensions

How to assign more than one file type in VS Code


I'm working on a VS code extension which 'augments' a particular language - specifically it's for textmate grammars, which can be written in JSON format. I want to enable the extension for .tmlanguage.json files but without disabling VS Code's support for .json. In package.json I have

 "languages": [
      {
        "id": "json-tmlanguage",
        "extensions": [
          ".tmLanguage.json",
          ".JSON-tmLanguage"
        ],
        ...
      }, ...
]

and

 "activationEvents": [
    "onLanguage:json-tmlanguage",
    ...
  ],

This activates when a .tmlanguage.json file is opened but seems to prevent normal JSON processing (e.g. Shift-Alt-F Format Document)

I don't want the tmlanguage stuff to activate on any JSON though, just the ones with the specific extension.


Solution

  • It is not possible to assign more than one language at the same time to a single file.
    Every extension you wish to use on a file, needs to support the language that's being assigned.

    The built-in JSON extension recently had the PR merged.
    Extension owners can now add the following code to their package.json file.
    Which will enable JSON language features on their languageId.

    "main": "",
    "extensionDependencies": [
        "vscode.json-language-features"
    ],
    "contributes": {
        "jsonLanguageParticipants": [
            {
                "languageId": "foobar"
            }
        ]
    }
    

    extensionDependencies activates the json-language-features extension.
    main is required for extensionDependencies to work.