Editor syntax highlighting without specifying extension

Answered

If I create an Editor specifying 

FileDocumentManager.getInstance().getDocument(ScratchFileService.getInstance().findFile(rootType, "myfile.json"))

as the document and

new JsonFileType()

as fileType, I get an editor with correct JSON syntax highlighting.

But, since I want to create an Editor that will not store its data in a physical file, but rather in file attributes, I use only

new DocumentImpl("initial text data");

when creating the editor. After that, syntax highlighting becomes limited (see picture):

The 2nd editor is created without using a VirtualFile.

I guess the reason highlighting works in the 1st case is because of specifying the ".json" extension.

How can I get syntax highlighting with using only Document, without VirtualFile? When saving editor data, I use document.getText().  

3 comments
Comment actions Permalink

Have you considered using com.intellij.testFramework.LightVirtualFile ?

1
Comment actions Permalink

Thanks, worked like a charm!

For future reference, here's what I had to do:

LightVirtualFile lightVirtualFile = new LightVirtualFile("", new JsonFileType(), params);
Document document = FileDocumentManager.getInstance().getDocument(lightVirtualFile);
Editor myEditor = createEditor(project, document);
0
Comment actions Permalink

It's also better to use com.intellij.json.JsonFileType#INSTANCE

Filetypes may be compared with == 

1

Please sign in to leave a comment.