Child pages
  • IntelliJ IDEA Architectural Overview

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The VFS level deals only with binary content. You can get or set the contents of a VirtualFile as a stream of bytes, but concepts like encodings and line separators are handled by higher levels of the system.

How do I get one?

  • From an action: (VirtualFile) dataContext.getData(DataConstants.VIRTUAL_FILE). If you are interested in multiple selection, you can also query for (VirtualFile[]) dataContext.getData(DataConstants.VIRTUAL_FILE_ARRAY).
  • From a path in the local file system: LocalFileSystem.findFileByIoFile()
  • From a PSI file: psiFile.getVirtualFile() (may return null if the PSI file exists only in memory)
  • From a document: FileDocumentManager.getInstance().getFile()

What can I do with one?

Traverse the file system, get file contents, rename, move, delete - typical file operations.

Where does it come from?

The VFS is built incrementally, by walking the filesystem up and down starting from the project root. New files appearing in the file system are detected by VFS refreshes. A refresh can be initiated programmatically (VirtualFileManager.refresh() or VirtualFile.refresh()). VFS refreshes are also caused by file system change notifications received by file system watchers (on Windows and Mac).

As a plugin developer, you may need to invoke a VFS refresh if you need to access through IDEA's APIs a file which has just been created by an external tool.

How long does it live?

A particular file on disk is represented by the same VirtualFile instance for the entire lifetime of the IDEA process. If a file is deleted, its corresponding VirtualFile instance becomes invalid (isValid() returns false and operations cause exceptions).

How do I create one?

Usually you don't: files are created either through the PSI API or through the regular java.io.File API. If you do need to create a file through VFS, you can use VirtualFile.createChildData() to create a VirtualFile instance and VirtualFile.setBinaryContent() to write some data to it.

How do I get notified when it changes?

VirtualFileManager.addVirtualFileListener() allows you to receive notifications about all changes happening in the VFS.

How do I extend it?

To provide an alternative filesystem implementation (for example, an FTP filesystem), implement the VirtualFileSystem interface (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're developing a version control system integration which needs custom rename/move handling), implement LocalFileOperationsHandler and register it through LocalFileSystem.registerAuxiliaryFileOperationsHandler.

What are the rules for working with it?

The general read action/write action rules are in effect. Note that any operation that can cause a synchronous refresh of the VFS (for example, LocalFileSystem.refreshAndFindFileByIoFile()) is a write action and can only performed in the UI thread. If you need to refresh VFS from a background thread, use asynchronous refresh operations with a postRunnable specified.

...

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. IntelliJ IDEA handles encoding and line break conversions when loading and saving documents transparently.

How do I get one?

  • From an action: ((Editor) dataContext.getData(DataConstants.EDITOR)).getDocument()
  • From a virtual file: FileDocumentManager.getDocument(). This call forces the document content to be loaded from disk if it wasn't loaded previously; if you're only interested in open documents or documents which may have been modified, use FileDocumentManager.getCachedDocument() instead.
  • From a PSI file: PsiDocumentManager.getInstance().getDocument() or PsiDocumentManager.getInstance().getCachedDocument()

What can I do with one?

Any operations which access or modify the file contents on "plain text" level (as a sequence of characters, not as a tree of Java elements).

Where does it come from?

Document instances are created when some operation needs to access the text contents of a file (in particular, this is needed to build the PSI for a file). Also, document instances not linked to any virtual files can be created temporarily, for example, to represent the contents of a text editor field in a dialog.

How long does it live?

Document instances are weakly referenced from VirtualFile instances. Thus, an unmodified Document instance can be garbage-collected if it isn't referenced by anyone, and a new instance will be created if the document contents is accessed again later. Storing Document references in long-term data structures of your plugin will cause memory leaks.

How do I create one?

If you need to create a new file on disk, you don't create a document: you create a PSI file and then get its document. If you need to create a document instance which isn't bound to anything, you can use EditorFactory.createDocument.

How do I get notified when it changes?

  • Document.addDocumentListener allows you to receive notifications about changes in a particular Document instance.
  • EditorFactory.getEventMulticaster().addDocumentListener allows you to receive notifications about changes in all open documents.
  • FileDocumentManager.addFileDocumentManagerListener allows you to receive notifications when any document is saved or reloaded from disk.

What are the rules of working with it?

The general read/write action rules are in effect. In addition to that, any operations which modify the contents of the document must be wrapped in a command (CommandProcessor.getInstance().executeCommand()). executeCommand() calls can be nested, and the outermost executeCommand call is added to the undo stack. If multiple documents are modified within a command, undoing this command will by default show a confirmation dialog to the user.

...

All text strings passed to document modification methods (setText, insertString, replaceString) must use only \n as line separators.

PSI Files

A PSI file is the root of a structure representing the contents of a file as a hierarchy of elements in a particular programming language. com.intellij.psi.PsiFile is the common base class for all PSI files, and files in a specific language are usually represented by its subclasses. For example, com.intellij.psi.PsiJavaFile represents a Java file, and com.intellij.psi.xml.XmlFile represents an XML file.

How

to be continued