Idea Plugin dev - PSI File (Program Structure Interface - Tree Structured Content)

Card Puncher Data Processing


A PSI (Program Structure Interface) file is the root of a structure representing the contents of a file as a hierarchy of elements in a particular programming language.

It represents a hierarchy of PSI elements (so-called PSI tree CST ? ).

A single PSI file may include several PSI trees in a particular programming language. A file may have multiple PSI file (if many language). A File View Providers manages this case ??


Interface / Creation

The PsiFile class is the common base class for all PSI files, while files in a specific language are usually represented by its subclasses.

For example:

As PSI is language-dependent, PSI files are created through the Language object, by using the LanguageParserDefinitions.INSTANCE.forLanguage(language).createFile(fileViewProvider) method.

Like documents, PSI files are created on demand when the PSI is accessed for a particular file.

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.


Scope: PSI has project scope (the same file is represented by multiple PsiFile instances if the file belongs to multiple projects open at the same time).



In bin/ (from Viewing Psi Structure)

Then Tool > help/idea/2016.2/psi-viewer.html


To find files with a specific name anywhere in the project, use FilenameIndex.getFilesByName(project, name, scope)


Most interesting modification operations are performed on the level of individual PSI elements, not files as a whole.

  • To iterate over the elements in a file, use psiFile.accept(new PsiRecursiveElementWalkingVisitor()…);

Any changes done to the content of PSI files are reflected in documents, so all rules for working with documents (read/write actions, commands, read-only status handling) are in effect.


Like documents, PSI files are weakly referenced from the corresponding VirtualFile instances and can be garbage-collected if not referenced by anyone.

Watcher / Notification

PsiManager.getInstance(project).addPsiTreeChangeListener() allows you to receive notifications about all changes to the PSI tree of a project.

How do I extend PSI to support additional languages

PSI can be extended to support additional languages through custom language plugins: IDEA Plugin Dev - Custom Language Plugin

Documentation / Reference

Discover More
Card Puncher Data Processing
Grammar Kit

Grammar-kit adds: BNF Grammars and JFlex files editing support, and a parser/PSI code generator.
Card Puncher Data Processing
IDEA Plugin Dev - File View Providers (Multiple PSI Tree in one file)

A file view provider (see the com/intellij/psi/FileViewProviderFileViewProvider class) manage access to multiple PSI trees within a single file. For example, a JSPX page has: a separate PSI tree for...
Card Puncher Data Processing
IDEA Plugin Dev - Module ( API )

Working with Modules in a plugin API The IntelliJ Platform provides a number of Java classes and interfaces you can use to work with modules within the project and core API Class Class Type API...
Card Puncher Data Processing
IDEA Plugin Dev - PSI Element

A PSI element is an element of a PSI tree. It can have child PSI elements. PSI elements and operations on the level of individual PSI elements are used to explore the internal structure of source code....
Card Puncher Data Processing
Idea - Plugin Development

How to develop IDEA plugin. The most common types of plugins include: Custom language support (File type recognition, Lexical analysis, Syntax highlighting, Formatting, Code insight and code completion,...
Card Puncher Data Processing
Idea Plugin Dev - Documents

A document is an editable sequence of Unicode characters, which typically corresponds to the text contents of a virtual file. Line breaks in a document are always normalized to \n. The IntelliJ...
Card Puncher Data Processing
Idea Plugin Dev - File and File System (Virtual)

A virtual file com.intellij.openapi.vfs.VirtualFile...

Share this page:
Follow us:
Task Runner