List<List<VirtualFile>> (@NotNull GlobalSearchScope scope, @NotNull StringSearcher searcher, short searchContext, boolean caseSensitively, @Nullable String containerName, @NotNull SearchSession session) { String text = searcher.getPattern(); Set<VirtualFile> allFiles = new HashSet<>(); getFilesWithText(scope, searchContext, caseSensitively, text, allFiles); List<List<VirtualFile>> priorities = new ArrayList<>(); List<VirtualFile> targets = ReadAction.compute(() -> ContainerUtil.filter(session.getTargetVirtualFiles(), scope::contains)); List<@NotNull VirtualFile> directories; if (targets.isEmpty()) { directories = Collections.emptyList(); } else { priorities.add(targets); targets.forEach(allFiles::remove); directories = ContainerUtil.mapNotNull(targets, v -> v.getParent()); GlobalSearchScope directoryNearTargetScope = new DelegatingGlobalSearchScope(scope) { @Override public boolean contains(@NotNull VirtualFile file) { return super.contains(file) && directories.contains(file.getParent()); } }; List<VirtualFile> directoryNearTargetFiles = ReadAction.compute(() -> ContainerUtil.filter(allFiles, f -> directoryNearTargetScope.contains(f) && !targets.contains(f)) ); if (!directoryNearTargetFiles.isEmpty()) { priorities.add(directoryNearTargetFiles); directoryNearTargetFiles.forEach(allFiles::remove); } } if (containerName != null) { Set<VirtualFile> intersectionWithContainerFiles = new HashSet<>(); // intersectionWithContainerFiles holds files containing words from both `text` and `containerName` getFilesWithText(scope, searchContext, caseSensitively, text+" "+containerName, intersectionWithContainerFiles); targets.forEach(intersectionWithContainerFiles::remove); directories.forEach(intersectionWithContainerFiles::remove); if (!intersectionWithContainerFiles.isEmpty()) { priorities.add(new ArrayList<>(intersectionWithContainerFiles)); allFiles.removeAll(intersectionWithContainerFiles); } } if (!allFiles.isEmpty()) { priorities.add(new ArrayList<>(allFiles)); } return priorities; }