diff --git a/platform/lang-impl/src/com/intellij/openapi/module/impl/scopes/LibraryRuntimeClasspathScope.java b/platform/lang-impl/src/com/intellij/openapi/module/impl/scopes/LibraryRuntimeClasspathScope.java index 89a0585a2cad..00c16f26dfcb 100644 --- a/platform/lang-impl/src/com/intellij/openapi/module/impl/scopes/LibraryRuntimeClasspathScope.java +++ b/platform/lang-impl/src/com/intellij/openapi/module/impl/scopes/LibraryRuntimeClasspathScope.java @@ -21,15 +21,17 @@ import com.intellij.openapi.project.Project; import com.intellij.openapi.projectRoots.Sdk; import com.intellij.openapi.roots.*; import com.intellij.openapi.roots.libraries.Library; +import com.intellij.openapi.util.Condition; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.search.GlobalSearchScope; +import com.intellij.util.containers.ContainerUtil; import gnu.trove.THashSet; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.LinkedHashMap; +import java.util.Collections; +import java.util.LinkedHashSet; import java.util.List; -import java.util.Map; import java.util.Set; /** @@ -37,76 +39,33 @@ import java.util.Set; */ public class LibraryRuntimeClasspathScope extends GlobalSearchScope { private final ProjectFileIndex myIndex; - private final LinkedHashMap myEntries = new LinkedHashMap(); + private final LinkedHashSet myEntries = new LinkedHashSet(); public LibraryRuntimeClasspathScope(final Project project, final List modules) { super(project); myIndex = ProjectRootManager.getInstance(project).getFileIndex(); final Set processedSdk = new THashSet(); final Set processedLibraries = new THashSet(); - - ProjectRootManager.getInstance(project).orderEntries(modules).recursively() - .process(new RootPolicy>() { - @Override - public LinkedHashMap visitLibraryOrderEntry(final LibraryOrderEntry libraryOrderEntry, - final LinkedHashMap value) { - final Library library = libraryOrderEntry.getLibrary(); - if (library != null && processedLibraries.add(library)) { - addAll(value, libraryOrderEntry.getRootFiles(OrderRootType.CLASSES)); - } - return value; + final Set processedModules = new THashSet(); + final Condition condition = new Condition() { + @Override + public boolean value(OrderEntry orderEntry) { + if (orderEntry instanceof ModuleOrderEntry) { + final Module module = ((ModuleOrderEntry)orderEntry).getModule(); + return module != null && processedModules.add(module); } - - @Override - public LinkedHashMap visitModuleSourceOrderEntry(final ModuleSourceOrderEntry moduleSourceOrderEntry, - final LinkedHashMap value) { - addAll(value, moduleSourceOrderEntry.getFiles(OrderRootType.SOURCES)); - return value; - } - - @Override - public LinkedHashMap visitModuleOrderEntry(ModuleOrderEntry moduleOrderEntry, - LinkedHashMap value) { - final Module depModule = moduleOrderEntry.getModule(); - if (depModule != null) { - addAll(value, ModuleRootManager.getInstance(depModule).getSourceRoots()); - } - return value; - } - - @Override - public LinkedHashMap visitJdkOrderEntry(JdkOrderEntry jdkOrderEntry, - LinkedHashMap value) { - final Sdk jdk = jdkOrderEntry.getJdk(); - if (jdk != null && processedSdk.add(jdk)) { - addAll(value, jdkOrderEntry.getRootFiles(OrderRootType.CLASSES)); - } - return value; - } - }, myEntries); - - fillIndexes(); + return true; + } + }; + for (Module module : modules) { + buildEntries(module, processedModules, processedLibraries, processedSdk, condition); + } } public LibraryRuntimeClasspathScope(Project project, LibraryOrderEntry entry) { super(project); myIndex = ProjectRootManager.getInstance(project).getFileIndex(); - addAll(myEntries, entry.getRootFiles(OrderRootType.CLASSES)); - - fillIndexes(); - } - - private void fillIndexes() { - int i = 0; - for (Map.Entry entry : myEntries.entrySet()) { - entry.setValue(i++); - } - } - - private static void addAll(Map map, VirtualFile[] files) { - for (VirtualFile file : files) { - map.put(file, null); - } + Collections.addAll(myEntries, entry.getRootFiles(OrderRootType.CLASSES)); } public int hashCode() { @@ -121,8 +80,50 @@ public class LibraryRuntimeClasspathScope extends GlobalSearchScope { return that.myEntries.equals(myEntries); } + private void buildEntries(@NotNull final Module module, + @NotNull final Set processedModules, + @NotNull final Set processedLibraries, + @NotNull final Set processedSdk, + Condition condition) { + if (!processedModules.add(module)) return; + + ModuleRootManager.getInstance(module).orderEntries().recursively().satisfying(condition).process(new RootPolicy>() { + public LinkedHashSet visitLibraryOrderEntry(final LibraryOrderEntry libraryOrderEntry, + final LinkedHashSet value) { + final Library library = libraryOrderEntry.getLibrary(); + if (library != null && processedLibraries.add(library)) { + ContainerUtil.addAll(value, libraryOrderEntry.getRootFiles(OrderRootType.CLASSES)); + } + return value; + } + + public LinkedHashSet visitModuleSourceOrderEntry(final ModuleSourceOrderEntry moduleSourceOrderEntry, + final LinkedHashSet value) { + ContainerUtil.addAll(value, moduleSourceOrderEntry.getFiles(OrderRootType.SOURCES)); + return value; + } + + @Override + public LinkedHashSet visitModuleOrderEntry(ModuleOrderEntry moduleOrderEntry, LinkedHashSet value) { + final Module depModule = moduleOrderEntry.getModule(); + if (depModule != null) { + ContainerUtil.addAll(value, ModuleRootManager.getInstance(depModule).getSourceRoots()); + } + return value; + } + + public LinkedHashSet visitJdkOrderEntry(final JdkOrderEntry jdkOrderEntry, final LinkedHashSet value) { + final Sdk jdk = jdkOrderEntry.getJdk(); + if (jdk != null && processedSdk.add(jdk)) { + ContainerUtil.addAll(value, jdkOrderEntry.getRootFiles(OrderRootType.CLASSES)); + } + return value; + } + }, myEntries); + } + public boolean contains(VirtualFile file) { - return myEntries.containsKey(getFileRoot(file)); + return myEntries.contains(getFileRoot(file)); } @Nullable @@ -142,25 +143,11 @@ public class LibraryRuntimeClasspathScope extends GlobalSearchScope { public int compare(VirtualFile file1, VirtualFile file2) { final VirtualFile r1 = getFileRoot(file1); final VirtualFile r2 = getFileRoot(file2); - - //for (VirtualFile root : myEntries) { - // if (r1 == root) return 1; - // if (r2 == root) return -1; - //} - //return 0; - - Integer index1 = myEntries.get(r1); - Integer index2 = myEntries.get(r2); - - if (index1 == null) { - return index2 == null ? 0 : -1; + for (VirtualFile root : myEntries) { + if (r1 == root) return 1; + if (r2 == root) return -1; } - - if (index2 == null) { - return 1; - } - - return index2.compareTo(index1); + return 0; } public boolean isSearchInModuleContent(@NotNull Module aModule) {