Assigning template language to html files based on presence of framework
I am writing a plugin that interprets template expressions in html files: https://plugins.jetbrains.com/plugin/10021-svelte
I've been able to parse template expressions using MultiplePsiFilesPerDocumentFileViewProvider in html files containing elements of the template language, by defining a SvelteFileType and applying it to files with the html extension. I can also define a svelte framework and let the user activate it manually (might also be detected automatically based on package.json, but that is a different story). Two questions:
- how can I find out if the svelte framework is active for the current project from within the FileTypeFactory, so that I do not activate the svelte parser in html files of all projects, but only in svelte projects? The way described in the faq with getDataContextFromFocus does not work, I get a datacontext, but the project is null in the FileTypeFactory:
DataContext dataContext =
DataManager.getInstance().getDataContextFromFocus().getResult();
Project project = DataKeys.PROJECT.getData(dataContext);
In the ui I see that the project is still being loaded when my FileTypeFactory is called, so probably there is really no project at that time. Or can I redefine the language of an html file later, after the project exists, so that my MultiplePsiFilesPerDocumentFileViewProvider kicks in?
- Assuming that I have the current project: How do I find out if my framework is present on the project?
Please sign in to leave a comment.
1. File types shouldn't know about projects, because project-independent file-based indices depend on those. You can create project-aware LanguageSubstitutor that replaces HTML with the language of your choice.
2. I don't know how you find out whether your framework is present :) It's your framework, after all, and you probably know better what you mean by framework being present in the project. If it's a facet, then you can query FacetManager. If it's a library, you can search for it in the dependencies.