How to Inform IDEA about programmatically created files?

Hi all,
I have instances of Project and PsiDirectory classes, as parameters of a method. In the body of this method, a file in the same directory is created (of course with some code, using java.io.File). How should I inform idea about the creation of this new file? Of course, I could not use methods like PsiDirectory.createFile.

Best regards,
<Farshid/>

Message was edited by:
Farshid Zaker

5 comments

I think com.intellij.openapi.vfs.LocalFileSystem.refreshAndFindFileByIoFile(File) is what you need. I'm not sure, but in case the file already existed you may also have to call refresh() on the returned VirtualFile object to make IDEA recognize the change in the file contents. Unfortunately, this aspect is not explicitly documented anywhere.

Also be sure to call these methods within a write-action (See com.intellij.openapi.application.Application#runWriteAction).

HTH,
Sascha

0

Thank you for your reply,
VirtualFile virtualFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(file) returns null. The file exists for sure.

Any help?

<Farshid/>

0

Hmm. Check if the file actually exists in the moment of the call by evaluating file.exists() and file.canRead() in the debugger. Check whether there are any non-standard (non-ASCII) characters or spaces in your filename/path. You can also try using com.intellij.openapi.vfs.VfsUtil.findFileByURL(file.toURL()) and see if that changes anything.

Sascha

0

Hi,

I have the same problem and I try your solution by using : com.intellij.openapi.vfs.VfsUtil.findFileByURL(file.toURL()).

It doesn't work.

The first created file is correctly found but the second not. It is like the filesystem is cached.

Regards,
Sophie

0

Hi,

May be you can try doing vFile.refresh() inside the Application.runWrite method first and then use LocalFileSystem.getInstance().findFileByPath(path).

Hope this helps.

~Priya

0

Please sign in to leave a comment.