GlobalSearchScope (@NotNull List<? extends OrderEntry> orderEntries) { List<Module> modulesLibraryUsedIn = new ArrayList<>(); LibraryOrderEntry lib = null; for (OrderEntry entry : orderEntries) { if (entry instanceof JdkOrderEntry) { return getScopeForSdk((JdkOrderEntry)entry); } if (entry instanceof LibraryOrderEntry) { lib = (LibraryOrderEntry)entry; modulesLibraryUsedIn.add(entry.getOwnerModule()); } else if (entry instanceof ModuleOrderEntry) { modulesLibraryUsedIn.add(entry.getOwnerModule()); } } Comparator<Module> comparator = Comparator.comparing(Module::getName); modulesLibraryUsedIn.sort(comparator); List<? extends Module> uniquesList = ContainerUtil.removeDuplicatesFromSorted(modulesLibraryUsedIn, comparator); GlobalSearchScope allCandidates = uniquesList.isEmpty() ? myLibrariesOnlyScope : getScopeForLibraryUsedIn(uniquesList); if (lib != null) { final LibraryRuntimeClasspathScope preferred = new LibraryRuntimeClasspathScope(myProject, lib); // prefer current library return new DelegatingGlobalSearchScope(allCandidates, preferred) { @Override public int compare(@NotNull VirtualFile file1, @NotNull VirtualFile file2) { boolean c1 = preferred.contains(file1); boolean c2 = preferred.contains(file2); if (c1 && !c2) return 1; if (c2 && !c1) return -1; return super.compare(file1, file2); } }; } return allCandidates; }