How to get the absolute path of a VirtualFile?

How can I get the absolute path of a VirtualFile?

0
7 comments
Avatar
Stefan Endrullis

At the moment I use

String path = vFile instanceof VirtualFileImpl ? vFile.getPath() : ((LightVirtualFile) vFile).getOriginalFile().getPath();

But that feels kind of hacky.

1

getPath is normally enough. Your current solution implies you have some nonstandard definition of "absolute path" (which light files don't have at all). Which problem are you trying to solve?

0
Avatar
Stefan Endrullis

The problem is that vFile.getPath returns sometimes a relative path to the project root (e.g. "/myJava.postfixTemplates" instead of "/home/stefan/programming/java/testProject/myJava.postfixTemplates").  However, this happens only in 25% of the cases.  Most of the time (75% of the cases) the full is returned by getPath().  And that's a bit confusing for me.

1

Which problem are you solving? What do you use getPath for?

0
Avatar
Stefan Endrullis

vFile.getPath is used in a method that is called from different code positions.  So that might be the reason why it sometimes returns the full path and sometimes only the relative path.

In this method I use this file path to look it up in a HashMap which stores further information about the file.  In the evening I will try to replace the file path String by a VirtualFile in this HashMap.  Maybe that solves the problem.

0

Most likely you get LightVirtualFile during completion, but they can also be created for highlighting purposes. In case of completion, you can also use PsiFile#getOriginalFile, and then take the VirtualFile of the result, it should be physical then.

0
((LightVirtualFile) currentFile).getOriginalFile() can be null
0

Please sign in to leave a comment.