List<VirtualFile> (@NotNull final List<String> targetParentFolders, final @NotNull List<? extends FileInfo> candidates, final int minProximityThreshold) { // let's find all files with similar relative path if (candidates.isEmpty()) { return Collections.emptyList(); } // let's iterate relative path components and determine which files are closer to our relative path for (String folderName : targetParentFolders) { for (FileInfo info : candidates) { info.processRelativePathComponent(folderName); } } // let's extract the closest files to relative path. For this we will find max proximity and and // we also assume that relative files and folders should have at least one common parent folder - just // to remove false positives on some cases int maxProximity = 0; for (FileInfo fileInfo : candidates) { final int proximity = fileInfo.getProximity(); if (proximity > maxProximity) { maxProximity = proximity; } } if (maxProximity >= minProximityThreshold) { final List<VirtualFile> files = new ArrayList<>(); for (FileInfo info : candidates) { if (info.getProximity() == maxProximity) { files.add(info.getFile()); } } return files; } return Collections.emptyList(); }