Intellij Plugin Develop: How to properly open a file after creation and loaded by IDE

164 views Asked by At

I want to create a java file and open it immediately. I write:

PsiFile file = PsiFileFactory.getInstance(project).createFileFromText(fileName, language, fileContent);
directory.add(file);
FileEditorManager.getInstance(project).openFile(file.getVirtualFile(), true, true);

It works but the opened file does not have any highlights, and the icon is gray. It seems that the IDE has not finished loading it.

So how to open file after IDE loaded it?

I try use CodeStyleManager.getInstance(project).reformat(file); to highlight the file, it works.

But the icon is still gray. And this file cannot use the "Select open file" button to navigate to the target file tree node.

I plan to use JavaDirectoryService.getInstance().createClass(dir, className, true); but I'm not sure how to use it correctly to open the created file ?

0

There are 0 answers