find virtual file for relative path under content roots
Answered
Given a relative path, eg foo/bar.java, is it possible to search each content root in the project for that relative path and thus arrive at a VirtualFile? This will be a complete path under a content entry, so FileNameIndex etc I don't think is useful here, as that's for searching by name.
I can iterate the content roots of course, but I imagined there must be some utility method already written for this.
cheers, jamie
Please sign in to leave a comment.
I extracted the code I use in my plugin to find files.
It compiles but you will need to debug it for specific conditions like fileType not found or invalid parameters:
public class PsiUtils {
public static List<VirtualFile> findFileByRelativePath(@NotNull Project project, @NotNull String fileRelativePath) {
String relativePath = fileRelativePath.startsWith("/") ? fileRelativePath : "/" + fileRelativePath;
Set<FileType> fileTypes = Collections.singleton(FileTypeManager.getInstance().getFileTypeByFileName(relativePath));
final List<VirtualFile> fileList = new ArrayList<>();
FileBasedIndex.getInstance().processFilesContainingAllKeys(FileTypeIndex.NAME, fileTypes, GlobalSearchScope.projectScope(project), null, virtualFile -> {
if (virtualFile.getPath().endsWith(relativePath)) {
fileList.add(virtualFile);
}
return true;
});
return fileList;
}
}
Thanks Vladimir... I iterated the content entries for each module, which works ok.
Your way may well be better... I'll bear it in mind.
Thanks again.
I think u need use java.io.File.separator than "/" for support other platforms like Mac OS.
VirtualFile uses '/' as the separator:
from VirtualFile.java:131-141:
Just chiming in to say this was super useful. Thanks Vladimir!