-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Allow extensions to add new file extensions to existing languages #2966
Comments
Not needed for Sprint 21 |
Points to consider: maybe we want to make this configurable by the user. |
Yes, we definitely do -- see https://trello.com/card/101-remember-user-syntax-mode-per-file-extension-2/4f90a6d98f77505d7940ce88/338. I guess this falls into the same bucket as menus and keyboard shortcuts -- things that are programmatically, not declaratively, specified by extensions but that we want users to be able to override in their preferences. We don't have a great answer for that yet in general. That's also true of the current language API though, where one extension may create a language A bound to file extension .foo but the user prefs may want to override that and bind it to some other language B. |
Reviewed. Made this a medium priority considering that we would want this for a 1.0. |
If extensions promise to only call the API during startup :-) then it might work to just make Language._addFileExtension() public. If we wanted something that could be done at any time then we'd need to wire up some notifications that Documents can listen to, etc. |
We could print an error if the method is called after APP_READY has been fired. |
Just for the fun of it, I added a language after a 1s delay and called However, I was a bit shocked to see that |
For performance reasons we could buffer all such changes (languages added or changed) and when nothing has changed for a certain amount of time, fire one "updated" event on LanguageManager that should be used by all code that somehow in the past has made a decision based on a language definition or association. |
I'm still fond of somehow expressing these things more directly. Some brainstorming:
I suppose all of these still have the problem of creating memory leaks if the documents implement these themselves. I suppose we could create callbacks in the DocumentManager instead that have a reference to |
Re @DennisKehrig's comment:
I'm not sure how else it would work: any given Document might have a file extension that matches the new language, so we have to check all of them. The check no-ops if it finds that Document.language is unchanged. Definitely agree it'd be nice to get all the core languages loaded before lots of Documents are created, though. Currently, that seems guaranteed because brackets.js doesn't call ProjectManager.openProject() until after LanguageManager.ready resolves. If some extension adds a ton of languages all at once then in theory it might be inefficient, but I suspect the hit would be totally unnoticeable. At the most extreme we're talking maybe ~10 languages added back to back times ~40 open Documents... 400 iterations of a for loop that does some simple string processing is probably negligible. V8 is fast :-) |
Once a document has a language other than "unknown", merely adding a language is not interesting to it anymore, that would never change the language for the file. But we let the document come to the same conclusion over and over again, namely that yes, indeed, it still wants the language it had all along for the file extension it had all along. Also it's more than simple string processing, it's lots of function calls and hash lookups, too. And sure, V8 is fast, but are we not worried about performance already? Especially startup performance? And while an individual project doesn't contain too many languages, usually, the user might still have a lot of extensions installed that provide languages - after all we're planning on removing support for most languages from the core and put them into extensions. Than the incidental optimization by loading ProjectManager after DocumentManager wouldn't help anymore. But right now it's not a problem, I agree. And we can fix it, should it become one, without breaking the API. |
@DennisKehrig If brackets extensions are allowed to extend existing languages as we were discussing in Issue #3044 then an open document may need to be notified that it's current mode should be updated would it not? In regards to performance I think it would be interesting to setup some kind of bench test to see what combinations in the number of languages and open docs would cause a noticeable difference in performance. It would be cool to know what the current implementations can handle before performance begins to suffer in that area. |
@MarkMurphy True, and we already have a |
@peterflynn I'm not sure about the second part in the description just now. You can totally use any CodeMirror mode you want for a new language. That is independent from the file extension. |
@peterflynn The performance considerations are indeed negligible since at boot time, most documents are not actually "open" yet, and adding/modifying an extension at a later time is a rare event for which it would be okay to take long. |
I added a pull request for this, but it all seems super simple, so I'm worried I may have overlooked an important case in which we need to react to languageModified. |
For one, I didn't update the language name in the status bar if the language changes. That was already an issue for language changes on rename, I suppose. |
@peterflynn Assigned to you to close it if you're satisfied |
@DennisKehrig If you havn't already you should also do this for Language.addFileName as well. I believe I made a reference to this same issue in the the LanguageManager when I added the _addFileName method. |
@MarkMurphy Yes, both methods call |
Working well for me -- closing |
Currently, Language._addFileExtension() is private (and it may not work well if not called early enough). We should make it a public fully supported API.
This enables:
The text was updated successfully, but these errors were encountered: