I’m adding a new feature to my plugin that integrates an external HTML-based visualization in a TextEditorWithPreview-based file editor. The issue I’m running into is that I can’t find a sanctioned way to create the HTMLFileEditor for the resulting visualization to use as the preview editor.
The only path I’m finding to create one is via HTMLEditorProvider.openEditor(), but that both creates the editor and opens it, so I end up with an unnecessary tab containing what should only be used as the preview editor.
The following works perfectly, but it uses internal APIs:
Is there a public/sanctioned API that I can use to do this but have perhaps missed/overlooked? If not, could one be added, and in the interim, could I use HTMLVirtualFile.createFile() and WebPreviewFileType.INSTANCE so I can move forward with this feature integration?
Also JcefHtmlEditorProvider has html-editor value for getEditorTypeId() if you can find it in com.intellij.openapi.fileEditor.FileEditorProvider#EP_FILE_EDITOR_PROVIDER extensions.
So you can ask it for createEditor(project, VirtualFile)
Unfortunately clone-and-own doesn’t look like it’s going to work – or at least be slim – because, ultimately, it needs to create HTMLFileEditorImpl which is also internal. I could keep cloning the dependency graph, but I stopped when I reached that point because it felt like it was getting a bit too large.
I tried the option of finding the correct file editor provider by ID, and I can find it that way, but if I just pass in a VirtualFile with file type HtmlFileType and not an HTMLVirtualFile, it throws an exception:
java.lang.IllegalArgumentException: cannot create html editor for non-html file, actual file:///path/to/visualizationFile.html
at com.intellij.openapi.fileEditor.impl.JcefHtmlEditorProvider.createEditor(JcefHtmlEditorProvider.kt:16)
due to:
override fun createEditor(project: Project, file: VirtualFile): FileEditor {
require(file is HTMLVirtualFile) {
"cannot create html editor for non-html file, actual $file"
}
It looks like I’m going to have to create an HTMLVirtualFile to get this to work properly.
Can I just log an enhancement request in YT for a new HTMLEditorProvider.createEditor() (basically the first part of openEditor() that just creates but then doesn’t open the HTML file editor), then go ahead and use the code included above until that new method is available?
Unfortunately. it only works with HTMLVirtualFile which is in-memory file. It is not ready to use physical files on disk. That is why integrating JCEF would be more correct for you, you can check how Markdown plugin does this.
Is there any reason that HTMLEditorProvider.createEditor() shouldn’t exist? It seems like being able to create a FileEditor for a JCEF view of an HTML file that can be used in composite editors is quite useful, no?
Yes, but it is not what you are looking for. It was implemented to show What’s New page in the past and still mostly used like this. With a rare exception to show preview ad-hoc by action.
My point is here is that you do not need a special API here, it is rather minimal code that does this and it is all about JCEF browser, no magic in WebPreviewFileType or HTMLVirtualFile