VirtualFiles explained
Can someone please explain the virtual file system? I really would like to know how to best work with files. Simple stuff, like browsing my sources and other files in the content root. I don't understand why this is so hard -- it's like pulling teeth! :)
Please sign in to leave a comment.
Hello Pat,
PL> Can someone please explain the virtual file system? I really would
PL> like to know how to best work with files. Simple stuff, like
PL> browsing my sources and other files in the content root. I don't
PL> understand why this is so hard -- it's like pulling teeth! :)
For simplicity. VirtualFileSystem is wrapper on OS file system and ZIP archives.
In real OS, you work differently with files and ZIP contents, usualy - in
IDEA - you will work with files and archives as same way. Becouse all IO
operation is wrapped by VirtualFileSystem.
There are two implementation now:
LocalFileSystem and JarFileSystem
Usualy you can get it like:
VirtualFileSystem vfs = LocalFilesystem.getInstance();
Or:
VirtualFileSystem vfs = JarFilesystem.getInstance();
The unit of VirtualFileSystem - is VirtualFile, it wrapper to content unit.
This may be file or ZIP entry content.
Using virtual file you can read and write it if it supported by VirtualFileSystem
for it VirtualFile.
To get VirtualFile from VirtualFileSystem you must use method findFileByPath
(or some method from concrete implementations).
If you will implement your own VirtualFilesystem, you must implement all
IO operation and notification engine, that will invoke methods fireXXX from
VirtualFileSystem, when content in changed.
The VFS is described in IDEABook (only in Russion) and you can get samples
from this book on TWiki site:
http://www.intellij.org/twiki/bin/view/Main/PluginDevelopmentSamples
Thanks!
--
Alexey Efimov, Java Developer
Tops BI
http://www.topsbi.ru
Alexey Efimov wrote:
>
A quick note: testing code that works with virtual files
(This is my way, there must be others)
I've replaced all
LocalFileSystem.getInstance()
calls by
LocalFileSystem_getInstance()
, with
public static LocalFileSystem LocalFileSystem_getInstance () {
if (null != fakeLocalFileSystem)
return fakeLocalFileSystem;
return LocalFileSystem.getInstance ();
}
=> In tests, I can use my MockLocalFileSystem, that returns MockVirtualFile.
You'll have to add your own MockProject, MockEditor, MockXXX, but Ctrl-I
makes this a breeze.
Alain