Skip to content

Instantly share code, notes, and snippets.

@agaricusb
Created December 24, 2012 01:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agaricusb/4367023 to your computer and use it in GitHub Desktop.
Save agaricusb/4367023 to your computer and use it in GitHub Desktop.
Create PsiJavaFile from PsiFile(plain text)
mirror from http://devnet.jetbrains.net/thread/271253
Create PsiJavaFile from PsiFile(plain text)
Hi,
I apologize if this is really basic but, after much searching in the forums, I still haven't found the answer. I have some java source files that exist within my project's content root but outside of any source paths. When examining the virtual files associated to each of the java files, they are all identifed as PsiFile(plain text), and their language is TEXT.
How can I turn such a reference into a PsiJavaFile reference so that I can manipulate/query its structure? The ultimate goal is to obtain the package of the file so that I can resolve the proper parent folder and add it to the source path.
Thank you very much,
Sebastien
Maxim Mossienko (JetBrains)
Nov 16, 2007 8:14 AM in response to: Sebastien Gagnon
Re: Create PsiJavaFile from PsiFile(plain text)
PsiManager.gelElementsFactory().createFileFromText
Sebastien Gagnon wrote:
Hi,
I apologize if this is really basic but, after much searching in the forums, I still haven't found the answer. I have some java source files that exist within my project's content root but outside of any source paths. When examining the virtual files associated to each of the java files, they are all identifed as PsiFile(plain text), and their language is TEXT.
How can I turn such a reference into a PsiJavaFile reference so that I can manipulate/query its structure? The ultimate goal is to obtain the package of the file so that I can resolve the proper parent folder and add it to the source path.
Thank you very much,
Sebastien
--
Best regards,
Maxim Mossienko
IntelliJ Labs / JetBrains Inc.
http://www.intellij.com
"Develop with pleasure!"
Sebastien Gagnon
226 posts since
Mar 19, 2003
Nov 19, 2007 8:02 PM in response to: Maxim Mossienko (JetBrains)
Re: Create PsiJavaFile from PsiFile(plain text)
Thanks Maxim, it worked great.
For the sake of completeness on this post, I offer this example code block which, given a "whatever.java" virtual file, will parse it into a PsiJavaFile if it's not already one.
if ( !virtualFile.isDirectory() && "java".equalsIgnoreCase(virtualFile.getExtension()) )
{
PsiManager psiMgr = PsiManager.getInstance(mProject);
PsiFile file = psiMgr.findFile(virtualFile);
if ( !(file instanceof PsiJavaFile) )
{
PsiJavaFile jFile = PsiJavaFile)psiMgr.getElementFactory().createFileFromText("temp.java", file.getText());
// do whatever you want with your PsiJavaFile
}
}
The file "temp.java" is not created anywhere, it's simply a token name. The extension of the name, however, is important since it will indicate to the factory which parser to use.
Regards,
Sebastien
@agaricusb
Copy link
Author

http://confluence.jetbrains.net/display/IDEADEV/IntelliJ+IDEA+Architectural+Overview#IntelliJIDEAArchitecturalOverview-PsiElements

How do I create one?
The PsiFileFactory .getInstance(project).createFileFromText() method creates an in-memory PSI file with the specified contents. To save the PSI file to disk, use the PsiDirectory .add() method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment