Contribute language association from extensions in

2020-04-21 07:45发布

问题:

How can I contribute a language association from an extension in VSCode?

In settings.json it would've looked like this:

"files.associations": {
    "*.something": "markdown"
}

I know that it's possible to use vscode.languages.setTextDocumentLanguage. But that seems excessive to do every time the activeEditor changes, and it's one more event listener.

Using the API to write into user settings doesn't seem right either.

回答1:

Yes, extensions can contribute settings via configurationDefaults. However, I don't think this works for the files.associations setting.

What you can do instead is contribute a new file extension for the markdown language:

"contributes": {
    "languages": [
        {
            "id": "markdown",
            "extensions": [
                "something"
            ]
        }
    ]
}

This won't replace the previous registration of the markdown language, instead it will be merged with it.



回答2:

Make the following change to your package.json, I figured it out by studying an existing extension.

You must add . before something

Also see : How can I write a vsc snippets extension for a language that is not listed on visual studio code

"contributes": {
    "languages": [
      {
        "id": "markdown",
        "extensions": [
          ".something"
        ],
      }
    ]
}