Child pages
  • IntelliJ IDEA Architectural Overview

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: fix/update links to github for highlighting

...

This topic assumes that the reader is familiar with the basic concepts of IntelliJ IDEA plugin development. If you don't know anything at all about plugin development, you should start with the live demo and tutorials at http://www.jetbrains.com/idea/plugins/plugin_developersindex.html, and then return to this document.

...

Virtual Files

A virtual file (com.intellij.openapi.vfs.VirtualFile ) is IntelliJ IDEA's representation of a file in a file system (VFS). The most common case of a virtual file is a file in your local file system. However, IDEA supports multiple pluggable file system implementations, so virtual files can also represent classes in a JAR file, old revisions of files loaded from the CVS repository and so on.

...

To provide an alternative file system implementation (for example, an FTP file system), implement the VirtualFileSystem class (most likely you'll also need to implement VirtualFile), and register your implementation as an application component . To hook into operations performed in the local file system (for example, if you deal with development of a version control system integration that needs custom rename/move handling), implement the LocalFileOperationsHandler interface and register it through the LocalFileSystem.registerAuxiliaryFileOperationsHandler method.

...

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. 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, the PsiJavaFile class represents a Java file, and the XmlFile class represents an XML file.

Unlike VirtualFile}}s and {{Document}}s, which have application scope (even if multiple projects are open, each file is represented by the same {{VirtualFile instance), 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).

...

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

...

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

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.

How do I get notified when it changes?

...

A file view provider (see the FileViewProvider class) is a new concept in IntelliJ IDEA beginning with vesrion version 6.0. Its main purpose is managing access to multiple PSI trees within a single file. For example, a JSPX page has a separate PSI tree for the Java code in it (PsiJavaFile), a separate tree for the XML code (XmlFile), and a separate tree for JSP as a whole (JspFile ). Each of the PSI trees covers the entire contents of the file, and contains special "outer language elements" in the places where contents in a different language can be found.

...

  • To get the list of all languages for which PSI trees exist in a file: fileViewProvider.getLanguages()
  • To get the PSI tree for a particular language: fileViewProvider.getPsi(language), where the language parameter can take values of the Language type defined in StdLanguages class. For example, to get the PSI tree for XML, use fileViewProvider.getPsi(StdLanguages.XML).
  • To find an element of a particular language at the specified offset in the file: fileViewProvider.findElementAt(offset,language)

...

To create a file type that has multiple interspersing trees for different languages, your plugin must contain an extension to the fileType.fileViewProviderFactory extension point available in the IntelliJ IDEA core. This extension point is declared using the FileTypeExtensionPoint bean class. To access this extension point, create a Java class that implements the FileViewProviderFactory interface, and in this class, override the createFileViewProvider method.
To declare the extension to the fileType.fileViewProviderFactory extension point, to the <extensions> section of the plugin.xml file, add the following syntax:

...

A PSI (Program Structure Interface) file represents a hierarchy of PSI elements (so called PSI trees). A single PSI file may include several PSI trees in a particular programming language. A PSI element, in its turn, 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 as it is interpreted by IntelliJ IDEA. For example, you can use PSI elements to perform the code analysis, such as code inspections or intention actions .
The PsiElement class is the common base class for PSI elements.

...