I have a PSI (python file of import statements) and I want add a PSI to it and save the changes

Hey everyone,

Lets say I have a python file of import statements.

[1] importFile = readIntoString(filename);

and I have a utility function which can create a PSI against that file

[2] PsiElement script = PsiUtil.createDummyFile(IDEAUtils.getProject(),importFile.toString());


and I get the Virtual file for the PsiElement (not sure if I need this yet) like

[3] VirtualFile vFile = script.getVirtualFile();


then I call the add method for script (script is of type PsiElement) to add a Psi element to it

[4] script.add(anotherImportStmt);


now I have script with the added import statement, I want to update the actual file
this is one failing approach I took

[5] FileDocumentManager.getInstance().saveDocument(
       FileDocumentManager.getInstance().getDocument(vFile)   );   // vFile is from above, see [3]



the problem is that the file is not updated with the added import, see [4]

this utility function, see [2] may be the key. I had implemented it 2 ways.
the 1st one [6] does not return a virtual file calling getVirtualFile() (nullPtrException)
and the 2nd one [7] returns a "Light Virtual File." filepath in [7] is the path to the file in [1], the importFile

[6] return (PsiElement) PsiFileFactory.getInstance(IDEAUtils.getProject()).createFileFromText("BLAH.py", GroovyFileType.GROOVY_FILE_TYPE, script_file_path, System.currentTimeMillis(), false);
[7] //return (PsiElement) PsiFileFactory.getInstance(IDEAUtils.getProject()).createFileFromText(filepath, GroovyFileType.GROOVY_FILE_TYPE.getLanguage(), script_file_path);


script.add(anotherImportStmt);


Maybe approach [5] is wrong and/or approach [6] or [7] are incorrect as you need a virtual file to save a PsiElement.

My question again is,
if i change a PSI initially created against a file as in above [1]-[4],
how do I save the changes to the file on in memory and disk too using the openapi framework?


Thanks for your time!

-Charlie
2 comments
Comment actions Permalink

Hello Charlie,

If you initially have a file which already exists in the file system, then
you don't need to read it into a string and create a new file from the string.
Instead of that, you should get the VirtualFile for the existing file (for
example, using LocalFileSystem.findFileByPath()), and then get the PSI for
it using PsiManager.findFile(VirtualFile). Then the modifications performed
on that PsiFile will be saved automatically.

If initially you don't have an existing file, then you need to save the file
that you create from text to some directory using PsiDirectory.add().

Also please refer to this article for more information:
http://confluence.jetbrains.net/display/IDEADEV/IntelliJIDEAArchitectural+Overview

Hey everyone,

Lets say I have a python file of import statements.

importFile = readIntoString(filename);

and I have a utility function which can create a PSI against that file

PsiElement script =
PsiUtil.createDummyFile(IDEAUtils.getProject(),importFile.toString());

and I get the Virtual file for the PsiElement (not sure if I need this
yet) like

VirtualFile vFile = script.getVirtualFile();

then I call the add method for script (script is of type PsiElement)
to add a Psi element to it

script.add(anotherImportStmt);

now I have script with the added import statement, I want to update
the actual file this is one failing approach I took

FileDocumentManager.getInstance().saveDocument(
FileDocumentManager.getInstance().getDocument(vFile)   );   //
vFile is from above, see
the problem is that the file is not updated with the added import, see


this utility function, see may be the key. I had implemented it 2
ways.

the 1st one does not return a virtual file calling
getVirtualFile() (nullPtrException)

and the 2nd one returns a "Light Virtual File." filepath in is
the path to the file in , the importFile

return (PsiElement)
PsiFileFactory.getInstance(IDEAUtils.getProject()).createFileFromText(
"BLAH.py", GroovyFileType.GROOVY_FILE_TYPE, script_file_path,
System.currentTimeMillis(), false);

//return (PsiElement)
PsiFileFactory.getInstance(IDEAUtils.getProject()).createFileFromText(
filepath, GroovyFileType.GROOVY_FILE_TYPE.getLanguage(),
script_file_path);

script.add(anotherImportStmt);

Maybe approach is wrong and/or approach or are incorrect
as you need a virtual file to save a PsiElement.

My question again is,
if i change a PSI initially created against a file as in above
-[4],
how do I save the changes to the file on in memory and disk too using
the openapi framework?
Thanks for your time!

-Charlie

---
Original message URL:
http://devnet.jetbrains.net/message/5266195#5266195

--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0
Comment actions Permalink

Thanks!
This worked perfectly.


~Charlie

0

Please sign in to leave a comment.