View provider for newly generated file

My plugin generates a PsiFile using PsiFileFactory#createFileFromText and I want to use FileViewProvider immediately after. How do I make it generate a psi tree/virtual file? I tried adding the file to some PsiDirectory (temporarily) and then using VirtualFileManager#refreshAndFindFileByUrl, but the file still doesn't seem to have a view provider.

Also, is there a way to get rid of whitespace left by programmatically removing xml tags/attributes via psi?



A PsiFile _is_ a PsiElement.  It's children are the PSI tree that you want to traverse.

If you need a FileViewProvider: `psiFile.getFileViewProvider();`

Look at PsiElement.delete() to remove elements you don't want.


No...PsiFile.getFileViewProvider doesn't work if I do it on the newly made file.

I'm deleting the xml attributes and tags just fine but they leave whitespace where they used to be.



You can create a FileViewProvider the hard way:

new SingleRootFileViewProvider(PsiManager.getInstance(project), 
isPhysical) {
public boolean supportsIncrementalReparse(@NotNull Language rootLanguage) {
return false;


Whitespace is another PSI element just like the XML you are manipulating.  Use PsiElement.getPreviousSibling() and getNextSibling() to determine whether there are extra white spaces that you want to remove.


Please sign in to leave a comment.