How to get the absolute path of a VirtualFile? Follow Stefan Endrullis Created May 01, 2018 20:54 How can I get the absolute path of a VirtualFile?
At the moment I use
But that feels kind of hacky.
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?
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.
Which problem are you solving? What do you use getPath for?
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.
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.