Retrieve PSI tree of external file


I'd like to parse code files (all java for now, but I would not like to restrict it to that) that do not belong to the current project in order to get their PSI tree. I have the file path (or the content of the file if you wish) ready as string, and just want to have it parsed. Motivation: I want to find comments and the code that belongs to them in these files, and would like to use the already existing parser mechanisms to extract the AST and comment strings.

Is there a way to do that? If yes, which classes / APIs do I have to use?


What prevents you from (temporarily) adding them for the parsing phase?


Nothing probably, only that I didn't know that this was a possibility. Could you give me a hint on how to add the file to the project and then retrieve the PsiFile of it?


Please see com.intellij.util.indexing.IndexableSetContributor for adding them, use LocalFileSystem.getInstance().findFileByPath to find roots


Please sign in to leave a comment.