mirror of
https://github.com/JetBrains/intellij-sdk-code-samples.git
synced 2025-07-30 18:27:49 +08:00
156 lines
10 KiB
HTML
156 lines
10 KiB
HTML
|
|
|
|
|
|
<!doctype html>
|
|
<html lang="en-US">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<title>PSI Files / IntelliJ Platform SDK DevGuide</title>
|
|
<link rel="stylesheet" href="/intellij/sdk/docs/app/css/styles.min.css">
|
|
|
|
<!-- non-retina iPad pre iOS 7 -->
|
|
<link rel="apple-touch-icon" href="/intellij/sdk/docs/apple-touch-icon-72x72.png" sizes="72x72">
|
|
|
|
<!-- retina iPhone pre iOS 7 -->
|
|
<link rel="apple-touch-icon" href="/intellij/sdk/docs/apple-touch-icon-114x114.png" sizes="114x114">
|
|
|
|
<!-- retina iPad pre iOS 7 -->
|
|
<link rel="apple-touch-icon" href="/intellij/sdk/docs/apple-touch-icon-144x144.png" sizes="144x144">
|
|
<link rel="apple-touch-icon-precomposed" sizes="144x144" href="/intellij/sdk/docs/apple-touch-icon-precomposed.png">
|
|
|
|
<!-- normal favicon -->
|
|
<link rel="shortcut icon" type="image/x-icon" href="/intellij/sdk/docs/favicon.ico">
|
|
<link rel="icon" type="image/png" href="/intellij/sdk/docs/favicon.png">
|
|
<link rel="stylesheet" href="/intellij/sdk/docs/styles/styles.css"></head>
|
|
<body data-id="basics/architectural_overview/psi_files">
|
|
<div class="wrapper">
|
|
<section class="panel _nav">
|
|
<header class="panel__header">
|
|
<div class="container">
|
|
<form class="search-box">
|
|
<label for="search-box__input" class="search-box__label">
|
|
<input type="text" class="search-box__input" id="search-box__input" placeholder="Search IntelliJ Platform SDK DevGuide">
|
|
</label>
|
|
<div class="search-box__clear" title="Clear"></div>
|
|
</form>
|
|
</div>
|
|
</header>
|
|
<nav class="panel__content">
|
|
<div class="container _nav">
|
|
<menu class="nav-tree"></menu>
|
|
</div>
|
|
<div class="container _footer panel__footer">
|
|
<p><a href="https://youtrack.jetbrains.com/issues/IJSDK">Send feedback</a></p>
|
|
<p>© 2000–2016 <a href="//www.jetbrains.com">JetBrains</a> s.r.o.<br>
|
|
All rights reserved.</p>
|
|
</div>
|
|
</nav>
|
|
</section>
|
|
|
|
<main class="panel _main" role="main">
|
|
<header class="panel__header">
|
|
<div class="container">
|
|
<h3>IntelliJ Platform SDK DevGuide</h3>
|
|
|
|
<div class="shortcuts-switcher"><label for="switch-shortcuts">Keymap:</label><select id="switch-shortcuts" class="select _shortcuts" height="1">
|
|
<option data-group="primary" value="default" selected>Default</option>
|
|
<option data-group="primary" value="default_for_gnome">GNOME</option>
|
|
<option data-group="primary" value="default_for_kde">KDE</option>
|
|
<option data-group="primary" value="default_for_xwin">XWindow</option>
|
|
<option data-group="primary" value="emacs">Emacs</option>
|
|
<option data-group="primary" value="jbuilder">JBuilder</option>
|
|
<option data-group="primary" value="visual_studio">Visual Studio</option>
|
|
<option data-group="primary" value="netbeans_6.5">NetBeans 6.5</option>
|
|
<option data-group="primary" value="eclipse">Eclipse</option>
|
|
<option data-group="secondary" value="mac_os_x_10.5_">OS X 10.5+</option>
|
|
<option data-group="secondary" value="mac_os_x">OS X</option>
|
|
<option data-group="secondary" value="eclipse_mac_os_x">OS X Eclipse</option></select>
|
|
</div>
|
|
|
|
<div class="panel-trigger"></div>
|
|
</div>
|
|
</header>
|
|
<section class="panel__content">
|
|
<div class="container">
|
|
<article class="article" data-shortcut-switcher="false">
|
|
|
|
<h1>PSI Files</h1>
|
|
<p>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.</p>
|
|
|
|
<p>The <a href="https://upsource.jetbrains.com/idea-ce/file/idea-ce-1731d054af4ca27aa827c03929e27eeb0e6a8366/platform/core-api/src/com/intellij/psi/PsiFile.java" data-bypass="yes" target="_blank"><span>PsiFile</span></a> 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 <a href="https://upsource.jetbrains.com/idea-ce/file/idea-ce-1731d054af4ca27aa827c03929e27eeb0e6a8366/java/java-psi-api/src/com/intellij/psi/PsiJavaFile.java" data-bypass="yes" target="_blank"><span>PsiJavaFile</span></a> class represents a Java file, and the <a href="https://upsource.jetbrains.com/idea-ce/file/idea-ce-1731d054af4ca27aa827c03929e27eeb0e6a8366/xml/xml-psi-api/src/com/intellij/psi/xml/XmlFile.java" data-bypass="yes" target="_blank"><span>XmlFile</span></a> class represents an XML file.</p>
|
|
|
|
<p>Unlike <code class="code highlight language-text">VirtualFile</code> and <code class="code highlight language-text">Document</code>, which have application scope (even if multiple projects are open, each file is represented by the same <code class="code highlight language-text">VirtualFile</code> instance), PSI has project scope (the same file is represented by multiple <code class="code highlight language-text">PsiFile</code> instances if the file belongs to multiple projects open at the same time).</p>
|
|
|
|
<a name="how-do-i-get-a-psi-file" class="elem-anchor"></a>
|
|
<h2>How do I get a PSI file?<a href="#how-do-i-get-a-psi-file" class="anchor-link"><span></span></a></h2>
|
|
|
|
<ul>
|
|
<li>From an action: <code class="code highlight language-text">e.getData(LangDataKeys.PSI_FILE)</code>.</li>
|
|
<li>From a VirtualFile: <code class="code highlight language-text">PsiManager.getInstance(project).findFile()</code></li>
|
|
<li>From a Document: <code class="code highlight language-text">PsiDocumentManager.getInstance(project).getPsiFile()</code></li>
|
|
<li>From an element inside the file: <code class="code highlight language-text">psiElement.getContainingFile()</code></li>
|
|
<li>To find files with a specific name anywhere in the project, use <code class="code highlight language-text">FilenameIndex.getFilesByName(project, name, scope)</code></li>
|
|
</ul>
|
|
|
|
<a name="what-can-i-do-with-a-psi-file" class="elem-anchor"></a>
|
|
<h2>What can I do with a PSI file?<a href="#what-can-i-do-with-a-psi-file" class="anchor-link"><span></span></a></h2>
|
|
|
|
<p>Most interesting modification operations are performed on the level of individual PSI elements, not files as a whole.</p>
|
|
|
|
<p>To iterate over the elements in a file, use <code class="code highlight language-text">psiFile.accept(new PsiRecursiveElementWalkingVisitor()...);</code></p>
|
|
|
|
<a name="where-does-it-a-psi-file-come-from" class="elem-anchor"></a>
|
|
<h2>Where does it a PSI file come from?<a href="#where-does-it-a-psi-file-come-from" class="anchor-link"><span></span></a></h2>
|
|
|
|
<p>As PSI is language-dependent, PSI files are created through the <a href="https://upsource.jetbrains.com/idea-ce/file/idea-ce-1731d054af4ca27aa827c03929e27eeb0e6a8366/platform/core-api/src/com/intellij/lang/Language.java" data-bypass="yes" target="_blank"><span>Language</span></a> object, by using the <code class="code highlight language-text">LanguageParserDefinitions.INSTANCE.forLanguage(language).createFile(fileViewProvider)</code> method.</p>
|
|
|
|
<p>Like documents, PSI files are created on demand when the PSI is accessed for a particular file.</p>
|
|
|
|
<a name="how-long-do-psi-files-persist" class="elem-anchor"></a>
|
|
<h2>How long do PSI files persist?<a href="#how-long-do-psi-files-persist" class="anchor-link"><span></span></a></h2>
|
|
|
|
<p>Like documents, PSI files are weakly referenced from the corresponding <code class="code highlight language-text">VirtualFile</code> instances and can be garbage-collected if not referenced by anyone.</p>
|
|
|
|
<a name="how-do-i-create-a-psi-file" class="elem-anchor"></a>
|
|
<h2>How do I create a PSI file?<a href="#how-do-i-create-a-psi-file" class="anchor-link"><span></span></a></h2>
|
|
|
|
<p>The <a href="https://upsource.jetbrains.com/idea-ce/file/idea-ce-1731d054af4ca27aa827c03929e27eeb0e6a8366/platform/core-api/src/com/intellij/psi/PsiFileFactory.java" data-bypass="yes" target="_blank"><span><code class="code highlight language-text">PsiFileFactory</code></span></a><code class="code highlight language-text">.getInstance(project).createFileFromText()</code> method creates an in-memory PSI file with the specified contents.</p>
|
|
|
|
<p>To save the PSI file to disk, use the <a href="https://upsource.jetbrains.com/idea-ce/file/idea-ce-1731d054af4ca27aa827c03929e27eeb0e6a8366/platform/core-api/src/com/intellij/psi/PsiDirectory.java" data-bypass="yes" target="_blank"><span><code class="code highlight language-text">PsiDirectory</code></span></a><code class="code highlight language-text">.add()</code> method.</p>
|
|
|
|
<a name="how-do-i-get-notified-when-psi-files-change" class="elem-anchor"></a>
|
|
<h2>How do I get notified when PSI files change?<a href="#how-do-i-get-notified-when-psi-files-change" class="anchor-link"><span></span></a></h2>
|
|
|
|
<p><code class="code highlight language-text">PsiManager.getInstance(project).addPsiTreeChangeListener()</code> allows you to receive notifications about all changes to the PSI tree of a project.</p>
|
|
|
|
<a name="how-do-i-extend-psi" class="elem-anchor"></a>
|
|
<h2>How do I extend PSI?<a href="#how-do-i-extend-psi" class="anchor-link"><span></span></a></h2>
|
|
|
|
<p>PSI can be extended to support additional languages through custom language plugins. For more details on developing custom language plugins, see the <a href="/intellij/sdk/docs/reference_guide/custom_language_support.html"><span>Custom Language Support</span></a> reference guide.</p>
|
|
|
|
<a name="what-are-the-rules-for-working-with-psi" class="elem-anchor"></a>
|
|
<h2>What are the rules for working with PSI?<a href="#what-are-the-rules-for-working-with-psi" class="anchor-link"><span></span></a></h2>
|
|
|
|
<p>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.</p>
|
|
|
|
|
|
<div class="last-modified">
|
|
Last modified: 26 November 2015
|
|
</div>
|
|
</article>
|
|
|
|
<section class="disqus">
|
|
<div id="disqus_thread"></div>
|
|
</section>
|
|
</div>
|
|
</section>
|
|
</main>
|
|
</div>
|
|
|
|
|
|
<script data-main="/intellij/sdk/docs/app/js/main.build" data-baseurl="/intellij/sdk/docs/" src="/intellij/sdk/docs/app/js/vendor/requirejs/require.js"></script>
|
|
|
|
</body>
|
|
</html>
|
|
|