mirror of
https://github.com/JetBrains/intellij-sdk-code-samples.git
synced 2025-07-30 18:27:49 +08:00
153 lines
11 KiB
HTML
153 lines
11 KiB
HTML
|
||
|
||
|
||
<!doctype html>
|
||
<html lang="en-US">
|
||
<head>
|
||
<meta charset="UTF-8">
|
||
<title>Virtual 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/virtual_file">
|
||
<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>Virtual Files</h1>
|
||
<p>A virtual file <a href="https://upsource.jetbrains.com/idea-ce/file/idea-ce-1731d054af4ca27aa827c03929e27eeb0e6a8366/platform/core-api/src/com/intellij/openapi/vfs/VirtualFile.java" data-bypass="yes" target="_blank"><span>com.intellij.openapi.vfs.VirtualFile</span></a> is the <em>IntelliJ Platform’s</em> representation of a file in a file system (VFS). Most commonly, a virtual file is a file in your local file system. However, the <em>IntelliJ Platform</em> supports multiple pluggable file system implementations, so virtual files can also represent classes in a JAR file, old revisions of files loaded from a version control repository, and so on.</p>
|
||
|
||
<p>The VFS level deals only with binary content. You can get or set the contents of a <code class="code highlight language-text">VirtualFile</code> as a stream of bytes, but concepts like encodings and line separators are handled on higher system levels.</p>
|
||
|
||
<a name="how-do-i-get-a-virtual-file" class="elem-anchor"></a>
|
||
<h2>How do I get a virtual file?<a href="#how-do-i-get-a-virtual-file" class="anchor-link"><span></span></a></h2>
|
||
|
||
<ul>
|
||
<li>From an action: <code class="code highlight language-text">e.getData(PlatformDataKeys.VIRTUAL_FILE)</code>. If you are interested in multiple selection, you can also use <code class="code highlight language-text">e.getData(PlatformDataKeys.VIRTUAL_FILE_ARRAY)</code>.</li>
|
||
<li>From a path in the local file system: <code class="code highlight language-text">LocalFileSystem.getInstance().findFileByIoFile()</code></li>
|
||
<li>From a PSI file: <code class="code highlight language-text">psiFile.getVirtualFile()</code> (may return null if the PSI file exists only in memory)</li>
|
||
<li>From a document: <code class="code highlight language-text">FileDocumentManager.getInstance().getFile()</code></li>
|
||
</ul>
|
||
|
||
<a name="what-can-i-do-with-it" class="elem-anchor"></a>
|
||
<h2>What can I do with it?<a href="#what-can-i-do-with-it" class="anchor-link"><span></span></a></h2>
|
||
|
||
<p>Typical file operations are available, such as traverse the file system, get file contents, rename, move, or delete. Recursive iteration should be performed using <code class="code highlight language-text">VfsUtilCore.iterateChildrenRecursively</code> to prevent endless loops caused by recursive symlinks.</p>
|
||
|
||
<a name="where-does-it-come-from" class="elem-anchor"></a>
|
||
<h2>Where does it come from?<a href="#where-does-it-come-from" class="anchor-link"><span></span></a></h2>
|
||
|
||
<p>The VFS is built incrementally, by scanning the file system up and down starting from the project root. New files appearing in the file system are detected by VFS <em>refreshes</em>. A refresh operation can be initiated programmatically using (<code class="code highlight language-text">VirtualFileManager.getInstance().refresh()</code> or <code class="code highlight language-text">VirtualFile.refresh()</code>). VFS refreshes are also triggered whenever file system watchers receive file system change notifications (available on the Windows and Mac operating systems).</p>
|
||
|
||
<p>As a plugin developer, you may want to invoke a VFS refresh if you need to access a file that has just been created by an external tool through the IntelliJ Platform APIs.</p>
|
||
|
||
<a name="how-long-does-a-virtual-file-persist" class="elem-anchor"></a>
|
||
<h2>How long does a virtual file persist?<a href="#how-long-does-a-virtual-file-persist" class="anchor-link"><span></span></a></h2>
|
||
|
||
<p>A particular file on disk is represented by equal <code class="code highlight language-text">VirtualFile</code> instances for the entire lifetime of the IDEA process. There may be several instances corresponding to the same file, and they can be garbage-collected. The file is a <code class="code highlight language-text">UserDataHolder</code>, and the user data is shared between those equal instances. If a file is deleted, its corresponding VirtualFile instance becomes invalid (the <code class="code highlight language-text">isValid()</code> method returns <code class="code highlight language-text">false</code> and operations cause exceptions).</p>
|
||
|
||
<a name="how-do-i-create-a-virtual-file" class="elem-anchor"></a>
|
||
<h2>How do I create a virtual file?<a href="#how-do-i-create-a-virtual-file" class="anchor-link"><span></span></a></h2>
|
||
|
||
<p>Usually you don’t. As a rule, files are created either through the PSI API or through the regular java.io.File API.</p>
|
||
|
||
<p>If you do need to create a file through VFS, you can use the <code class="code highlight language-text">VirtualFile.createChildData()</code> method to create a <code class="code highlight language-text">VirtualFile</code> instance and the <code class="code highlight language-text">VirtualFile.setBinaryContent()</code> method to write some data to the file.</p>
|
||
|
||
<a name="how-do-i-get-notified-when-vfs-changes" class="elem-anchor"></a>
|
||
<h2>How do I get notified when VFS changes?<a href="#how-do-i-get-notified-when-vfs-changes" class="anchor-link"><span></span></a></h2>
|
||
|
||
<p>The <code class="code highlight language-text">VirtualFileManager.addVirtualFileListener()</code> method allows you to receive notifications about all changes in the VFS.</p>
|
||
|
||
<a name="how-do-i-extend-vfs" class="elem-anchor"></a>
|
||
<h2>How do I extend VFS?<a href="#how-do-i-extend-vfs" class="anchor-link"><span></span></a></h2>
|
||
|
||
<p>To provide an alternative file system implementation (for example, an FTP file system), implement the <a href="https://upsource.jetbrains.com/idea-ce/file/idea-ce-1731d054af4ca27aa827c03929e27eeb0e6a8366/platform/core-api/src/com/intellij/openapi/vfs/VirtualFileSystem.java" data-bypass="yes" target="_blank"><span>VirtualFileSystem</span></a> class (most likely you’ll also need to implement <code class="code highlight language-text">VirtualFile</code>), and register your implementation as an <a href="/intellij/sdk/docs/basics/plugin_structure/plugin_components.html"><span>application component</span></a>.</p>
|
||
|
||
<p>To hook into operations performed in the local file system (for example, if you are developing a version control system integration that needs custom rename/move handling), implement the <a href="https://upsource.jetbrains.com/idea-ce/file/idea-ce-1731d054af4ca27aa827c03929e27eeb0e6a8366/platform/platform-api/src/com/intellij/openapi/vfs/LocalFileOperationsHandler.java" data-bypass="yes" target="_blank"><span>LocalFileOperationsHandler</span></a> interface and register it through the<code class="code highlight language-text">LocalFileSystem.registerAuxiliaryFileOperationsHandler</code> method.</p>
|
||
|
||
<a name="what-are-the-rules-for-working-with-vfs" class="elem-anchor"></a>
|
||
<h2>What are the rules for working with VFS?<a href="#what-are-the-rules-for-working-with-vfs" class="anchor-link"><span></span></a></h2>
|
||
|
||
<p>See <a href="/intellij/sdk/docs/basics/virtual_file_system.html"><span>IntelliJ Platform Virtual File System</span></a> for a detailed description of the VFS architecture and usage guidelines.</p>
|
||
|
||
|
||
<div class="last-modified">
|
||
Last modified: 30 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>
|
||
|