Virtual Files are cached so how do I reload them from a change in content?

The context of the problem is:

I have a file I am editing, and this file has code completion capabilities based on python scripts loaded as Artifacts in the project. These scripts are used for import statements, function definitions, and constants. The python file I am editing has an "Import Statement Inspection" and can add the import statement psi typed to one of the scripts in the Artifacts since this is the proper location for import statements.

The python script from the Artifacts is updated with the new import from an "Actual File" perspective, but the Virtual File which is cached does not have the most updated content.

How do I reload a Virtual file from a file on disk?

I load it initially by (thanks to Dmitry)

            VirtualFile vfile = LocalFileSystem.getInstance().findFileByPath(ActualFile.getAbsolutePath());
            PsiFile importPsi = PsiManager.getInstance(prj).findFile(vfile);


For a virtual file I see there is a refresh() method and a listener I can add to the virtual file, getFileSystem().addVirtualFileListener() and listen for content changes by
implementing:

new VirtualFileAdapter() {
@Override
contentsChanged() { ... }
}

Is this correct too (the contentsChanged() method) for reloading the changed file?

Thanks for any help guys?
2 comments
Comment actions Permalink

Hello Charlie,

If you update the content of the file through (either through the PSI, through
documents or by setting the content of a virtual file), you don't need to
refresh anything. The refresh is only necessary if you update the content
using the java.io APIs, which is not really recommended.

If you do modify the content externally, then yes, you can use refresh()
to synchronize the VirtualFile content with disk content, and the contentsChanged()
method to get notifications on changes of content.

The context of the problem is:

I have a file I am editing, and this file has code completion
capabilities based on python scripts loaded as Artifacts in the
project. These scripts are used for import statements, function
definitions, and constants. The python file I am editing has an
"Import Statement Inspection" and can add the import statement psi
typed to one of the scripts in the Artifacts since this is the proper
location for import statements.

The python script from the Artifacts is updated with the new import
from an "Actual File" perspective, but the Virtual File which is
cached does not have the most updated content.

How do I reload a Virtual file from a file on disk?

I load it initially by (thanks to Dmitry)
VirtualFile vfile =
LocalFileSystem.getInstance().findFileByPath(ActualFile.getAbsolutePat
h());
PsiFile importPsi =
PsiManager.getInstance(prj).findFile(vfile);
For a virtual file I see there is a refresh() method and a listener I
can add to the virtual file, getFileSystem().addVirtualFileListener()
and listen for content changes by

implementing:

new VirtualFileAdapter() {
@Override
contentsChanged() { ... }
}
Is this correct too (the contentsChanged() method) for reloading the
changed file?

Thanks for any help guys?

---
Original message URL:
http://devnet.jetbrains.net/message/5266882#5266882

--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0
Comment actions Permalink

Thanks for the clarification.

Changing the file's PSI via the APIs versus changing the file externally (like through a VCS's update operation)

0

Please sign in to leave a comment.