You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Allow language plugins to overrule built-in language modes only if the file extension *and* the first line match #53350
For the purpose of easier differentiation between file extension and extension (in the sense of extension for VS Code), I am calling the latter plugin.
I am currently developing a language plugin 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 only use my plugin when the first line matches a specific string when opening .txt files. Afaik, this is currently not possible (hence the feature request). I have not found any information that tells me otherwise and the contribution points article does not go into details about how exactly VS Code chooses a language mode when applying the three mentioned rules.
From what I have read here and here, there have been some changes last year that allow language plugins that match the file extension and the first line of a file to get precedence over a plugin that just matches one of the two. But this does not seem to work with any built-in language modes. As soon as I set a file extension match for .txt (or other file extensions I do not have specific plugins installed for) in my plugin, VS Code always activates my plugin when opening such a file and disregards the first line match I have also set up specifically to make sure VS Code does not activate my plugin for "any old" .txt file.
What I think should happen is that when my plugin is the only one that contributes to .txt files and I have a file extension match and a first line match set up, these two matches should behave like a logical AND (&&); only if both are true, VS Code should use my extension over the built-in language mode for plain text. The more sophisticated solution would probably be to let the plugin developer choose if he wants to apply a AND or an OR rule to these matches. Or a weight-based system as described in #10915.
I hope it is somewhat clear what the issue is. If not, let me know and I will provide an example extension that demonstrates what I am talking about.
changed the title [-]Allow language extensions to overrule built-in language modes only if the file extension *and* the first line match[/-][+]Allow language plugins to overrule built-in language modes only if the file extension *and* the first line match[/+]on Jun 29, 2018
As a side note: I thought there might exist a workaround for my specific issue by only setting up a first line match. However, this does not work; in such a case (where I specifically omit the .txt extension match from my plugin), VS Code never chooses my plugin when opening .txt files where the first line matches what I have specified. It only chooses my plugin if I open a file without any extension (e.g., some-file) or with an extension that there are no built-in modes for (e.g., some-file.abcdef1234).
So, what I currently do to work around the limitation is:
Set the language mode manually after opening a .txt file
or change the workspace settings to always use my plugin for .txt files (if I limit it to the workspace it is not a big issue since there likely will not be any "normal" .txt files)
But this requires some manual input and gets annoying when dealing with many multiple workspaces and/or single files. Having an automated solution would be preferable.
I will try to see if I can summarise the request... Today, the semantics of contributes.languages.extensions and contributes.languages.firstLine is OR.
That is to say, take for example the registration for XML:
Then an .xml file which begins with an empty first line (does not match firstLine regex) will still be opened as XML.
You would like for the semantics between these two to be AND. I don't think we will ever change the existing semantics, but perhaps we could add some sort of configuration option in the registration.
But IMHO this is pretty niche request, it seems to me that if you invent a new language, your new language deserves a new file extension.
But IMHO this is pretty niche request, it seems to me that if you invent a new language, your new language deserves a new file extension.
I do agree. The issue mainly came up because there is already a huge amount of code in the language the plugin is for out there that uses the txt extension.
I am not the inventor of the language, but I think this probably happened because of two reasons:
There has not been much in the terms of syntax highlighting or even IDE support for it (aside from a plugin for Sublime Text, one for Notepad++ and mine for VS Code, I do not know of any others), thus making a custom extension pretty "unnecessary".
The runtime natively only runs on Windows where txt is just a convenient default extension to use – files with custom extensions (or files with no extension) require at least a one-time manual setup to be able to open them by double-clicking. Given that the language is (intended to be) simple enough to pick up for people who have not touched anything else programming-related before, the developer might have thought that it is best to keep it as simple as possible for beginners, therefore just choosing txt.
Afaik, the custom extension that is now available came a while later, but people still tend to use txt – probably because they are used to it or older tutorials do not mention the custom extension.
[…] but perhaps we could add some sort of configuration option in the registration.
This would be neat. The simplest thing I can think of is a flag like only override built-in language modes if extension AND firstLine match – I am not sure how to word that more efficiently though. :D
As I have mentioned, this apparently works out of the box for plugins, where a plugin that matches both gets precedence over one that only matches either/or.
But after some consideration, I can also see how your current default for overriding built-ins makes sense, because it is generally safe to assume that a language plugin provides more/additional functionality than a built-in mode, so it should always get precedence.
In any case, it is certainly not a huge issue and the easier thing to do might probably be just educating developers to use the custom extension. Feel free to close the issue if you do not think such a flag/option is something you are going to add – and thank you for the great code editor! :)
This feature request is now a candidate for our backlog. The community has 60 days to upvote the issue. If it receives 20 upvotes we will move it to our backlog. If not, we will close it. To learn more about how we handle feature requests, please see our documentation.
This feature request has not yet received the 20 community upvotes it takes to make to our backlog. 10 days to go. To learn more about how we handle feature requests, please see our documentation.
🙁 In the last 60 days, this feature request has received less than 20 community upvotes and we closed it. Still a big Thank You to you for taking the time to create this issue! To learn more about how we handle feature requests, please see our documentation.
Activity
[-]Allow language extensions to overrule built-in language modes only if the file extension *and* the first line match[/-][+]Allow language plugins to overrule built-in language modes only if the file extension *and* the first line match[/+]a1e7de48f89e commentedon Jun 29, 2018
As a side note: I thought there might exist a workaround for my specific issue by only setting up a first line match. However, this does not work; in such a case (where I specifically omit the
.txtextension match from my plugin), VS Code never chooses my plugin when opening.txtfiles where the first line matches what I have specified. It only chooses my plugin if I open a file without any extension (e.g.,some-file) or with an extension that there are no built-in modes for (e.g.,some-file.abcdef1234).So, what I currently do to work around the limitation is:
.txtfile.txtfiles (if I limit it to the workspace it is not a big issue since there likely will not be any "normal".txtfiles)But this requires some manual input and gets annoying when dealing with many multiple workspaces and/or single files. Having an automated solution would be preferable.
alexdima commentedon Jul 27, 2018
I will try to see if I can summarise the request... Today, the semantics of
contributes.languages.extensionsandcontributes.languages.firstLineis OR.That is to say, take for example the registration for XML:
Then an
.xmlfile which begins with an empty first line (does not matchfirstLineregex) will still be opened as XML.You would like for the semantics between these two to be AND. I don't think we will ever change the existing semantics, but perhaps we could add some sort of configuration option in the registration.
But IMHO this is pretty niche request, it seems to me that if you invent a new language, your new language deserves a new file extension.
a1e7de48f89e commentedon Jul 27, 2018
alexandrudima
Thank you for taking the time to have a look.
I do agree. The issue mainly came up because there is already a huge amount of code in the language the plugin is for out there that uses the
txtextension.I am not the inventor of the language, but I think this probably happened because of two reasons:
txtis just a convenient default extension to use – files with custom extensions (or files with no extension) require at least a one-time manual setup to be able to open them by double-clicking. Given that the language is (intended to be) simple enough to pick up for people who have not touched anything else programming-related before, the developer might have thought that it is best to keep it as simple as possible for beginners, therefore just choosingtxt.Afaik, the custom extension that is now available came a while later, but people still tend to use
txt– probably because they are used to it or older tutorials do not mention the custom extension.This would be neat. The simplest thing I can think of is a flag like
only override built-in language modes if extension AND firstLine match– I am not sure how to word that more efficiently though. :DAs I have mentioned, this apparently works out of the box for plugins, where a plugin that matches both gets precedence over one that only matches either/or.
But after some consideration, I can also see how your current default for overriding built-ins makes sense, because it is generally safe to assume that a language plugin provides more/additional functionality than a built-in mode, so it should always get precedence.
In any case, it is certainly not a huge issue and the easier thing to do might probably be just educating developers to use the custom extension. Feel free to close the issue if you do not think such a flag/option is something you are going to add – and thank you for the great code editor! :)
vscodebot commentedon Jan 23, 2020
This feature request is now a candidate for our backlog. The community has 60 days to upvote the issue. If it receives 20 upvotes we will move it to our backlog. If not, we will close it. To learn more about how we handle feature requests, please see our documentation.
Happy Coding!
vscodebot commentedon Mar 13, 2020
This feature request has not yet received the 20 community upvotes it takes to make to our backlog. 10 days to go. To learn more about how we handle feature requests, please see our documentation.
Happy Coding
vscodebot commentedon Mar 23, 2020
🙁 In the last 60 days, this feature request has received less than 20 community upvotes and we closed it. Still a big Thank You to you for taking the time to create this issue! To learn more about how we handle feature requests, please see our documentation.
Happy Coding!