How to walk PsiDirectory recursively using provided api
Answered
In my custom language(named KCL), if a PsiDirectory contains KCL files recursively, then it can be a KCL package. So I check if a PsiDirectory is a KCL package by doing this:
public static boolean isKCLPackage(PsiDirectory dir) {
for (PsiFile file : dir.getFiles()) {
if (file instanceof KCLFile) {
return true;
}
}
for (PsiDirectory subDir : dir.getSubdirectories()) {
if (isKCLPackage(subDir)) {
return true;
}
}
return false;
}
Is there a better way or some API to walk the file tree recursively? for example, I see Java 8 provides a stream to process all files in a tree to traverse files:
Files.walk(Paths.get(path))
.filter(Files::isRegularFile)
.forEach(System.out::println);
Any help/pointers are gratefully received.
Please sign in to leave a comment.
You can use a combination of com.intellij.psi.search.FileTypeIndex#containsFileOfType with your FileType and pass in com.intellij.psi.search.PackageScope as search scope
Hi Yann Cebron,
Thank you very much for your kindly response, seems much better than what I've tried.