Faulting a newly created PsiFile to disk

This is an odd one, and I don't seem to find the right incantation.  I'm putting the finishing touches on an automated Java -> Scala translator plugin.  One action is to select a given Java file and translate it.  This creates a new PsiFile using PsiFileFactory.createFileFromText followed by an appropriate directory.add(), and then .delete() the java PsiFile.  So far so good.  I would then like to automatically open up the resulting scala file.  Unfortunately, no matter what I do I seem to get null on return from the scala psiFile.getVirtualFile() call.  The docs seem to indicate that this is because the psiFile only exists in memory.  How do I fault the file to disk, or alternately how do I open an non-faulted file in an editor?

--Dave Griffith

0
4 comments

Hi Dave,

As far as I know PsiDirectory.add() should save it to disk (be sure to use the return value of this method). See also http://www.jetbrains.net/confluence/display/IDEADEV/IntelliJ+IDEA+Architectural+Overview

Bas

0
Avatar
Permanently deleted user

Thanks, that did it.  I didn't even see that .add() had a return value.   You're as helpful as always!

--Dave Griffith
"The specs said Java 1.4 or better, so I coded it in Scala"

0
Avatar
Permanently deleted user

Well almost.  It turns out I do need to fault a file to disk, but not the newly created one.  The problem is that once I create the .scala file, I call .delete() on the .java file.  That works fine, but if I then ask to undo the translation to Scala, I get back the Java file based on the disk contents.  This means that unsaved changes to the .java file are lost in the translate/undo use case. Any idea how I can fault the .java file to disk before I delete it?

--Dave Griffith
"The specs said Java 1.4 or better, so I wrote it in Scala"

0

This is how I would do it:


        final FileDocumentManager fileDocumentManager =

                FileDocumentManager.getInstance();

        fileDocumentManager.saveDocument(fileDocumentManager.getDocument(

                virtualFile));

        // or probably even

        fileDocumentManager.saveAllDocuments();


Bas
0

Please sign in to leave a comment.