Reputation: 61
I am currently developing a language extension for VS Code. The language I am developing it for usually comes with either its own file extension (let's call it .myext
) or simply .txt
(it's just a scripting language).
I have run into the issue that I am not sure how to tell VS Code to use my extension when the first line matches a specific string when opening .txt
files.
This is what I am trying (excerpt from my package.json
):
"contributes": {
"languages": [
{
"id": "mylang",
"aliases": [
"mylang"
],
"extensions": [
".myext"
],
"firstLine": "^MyLang.*",
"configuration": "./language-configuration.json"
}
]
}
This does not work when I open a .txt
file that starts with MyLang
. It does however work if I open a file that has an unknown (e.g., file.foobar1234
) or no extension at all.
When I now change the configuration to include .txt
files, it will activate my extension for any .txt
file I open:
"contributes": {
"languages": [
{
"id": "mylang",
"aliases": [
"mylang"
],
"extensions": [
".myext",
".txt"
],
"firstLine": "^MyLang.*",
"configuration": "./language-configuration.json"
}
]
}
However, I do not want this, I want to keep the default plain text
setting when opening normal .txt
files.
In short, what I want to achieve:
.myext
(this already works without any issues).txt
files that start with the string specified via firstLine
.txt
files that do not start with the string specified via firstLine
Is there a way to do this?
Upvotes: 2
Views: 1674
Reputation: 61
After some extensive search: this is currently not possible.
There was a recent change (mid-late 2017) that allows language extensions that match the file extension and the first line of a file to get precedence over an extension that just matches one of the two.
This does, however, not work with the built-in language modes of VS Code. Matching a file extension will make it so VS Code always chooses your language extension for such files if there if there is no other extension available for that language, regardless of any first line rule that might have been set in addition.
See here and here for additional information.
Upvotes: 3
Reputation: 1212
If you have the latest version of VS Code, I say this because it's what I have might be probably is supported in earlier versions. On the bottom toolbar you should see the language that is auto-detected for the file you have open. For example, if you have a .js file it will auto-config for JavaScript. Whatever language defaults for you file, just click on it and it will bring you into 'Select Language Mode'.
From there you'll have options. You can configure/customize what happens and/or Search for an extension to fulfill the Language mode. So, I think you'd want to do the latter especially if you're creating your own language file.
Upvotes: 0