diff --git a/idea/src/org/jetbrains/jet/plugin/caches/resolve/IDELightClassGenerationSupport.java b/idea/src/org/jetbrains/jet/plugin/caches/resolve/IDELightClassGenerationSupport.java index da5f283727b..eeac825a65d 100644 --- a/idea/src/org/jetbrains/jet/plugin/caches/resolve/IDELightClassGenerationSupport.java +++ b/idea/src/org/jetbrains/jet/plugin/caches/resolve/IDELightClassGenerationSupport.java @@ -16,7 +16,6 @@ package org.jetbrains.jet.plugin.caches.resolve; -import com.google.common.collect.Sets; import com.intellij.openapi.components.ServiceManager; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.project.Project; @@ -40,13 +39,13 @@ import org.jetbrains.jet.lang.resolve.lazy.ForceResolveUtil; import org.jetbrains.jet.lang.resolve.lazy.KotlinCodeAnalyzer; import org.jetbrains.jet.lang.resolve.name.FqName; import org.jetbrains.jet.lang.resolve.name.Name; -import org.jetbrains.jet.lang.resolve.name.NamePackage; import org.jetbrains.jet.plugin.libraries.JetSourceNavigationHelper; import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache; import org.jetbrains.jet.plugin.project.ResolveSessionForBodies; import org.jetbrains.jet.plugin.stubindex.JetAllPackagesIndex; import org.jetbrains.jet.plugin.stubindex.JetClassByPackageIndex; import org.jetbrains.jet.plugin.stubindex.JetFullClassNameIndex; +import org.jetbrains.jet.plugin.stubindex.PackageIndexUtil; import java.util.*; @@ -169,13 +168,7 @@ public class IDELightClassGenerationSupport extends LightClassGenerationSupport @NotNull @Override public Collection findFilesForPackage(@NotNull final FqName fqName, @NotNull GlobalSearchScope searchScope) { - Collection files = JetAllPackagesIndex.getInstance().get(fqName.asString(), project, kotlinSources(searchScope)); - return ContainerUtil.filter(files, new Condition() { - @Override - public boolean value(JetFile file) { - return fqName.equals(JetPsiUtil.getFQName(file)); - } - }); + return PackageIndexUtil.findFilesWithExactPackage(fqName, kotlinSources(searchScope), project); } @NotNull @@ -194,22 +187,7 @@ public class IDELightClassGenerationSupport extends LightClassGenerationSupport @NotNull @Override public Collection getSubPackages(@NotNull FqName fqn, @NotNull GlobalSearchScope scope) { - Collection files = JetAllPackagesIndex.getInstance().get(fqn.asString(), project, kotlinSources(scope)); - - Set result = Sets.newHashSet(); - for (JetFile file : files) { - FqName fqName = JetPsiUtil.getFQName(file); - - assert NamePackage.isSubpackageOf(fqName, fqn) : "Registered package is not a subpackage of actually declared package:\n" + - "in index: " + fqn + "\n" + - "declared: " + fqName; - FqName subpackage = NamePackage.plusOneSegment(fqn, fqName); - if (subpackage != null) { - result.add(subpackage); - } - } - - return result; + return PackageIndexUtil.getSubPackageFqNames(fqn, kotlinSources(scope), project); } @Nullable diff --git a/idea/src/org/jetbrains/jet/plugin/stubindex/PackageIndexUtil.java b/idea/src/org/jetbrains/jet/plugin/stubindex/PackageIndexUtil.java new file mode 100644 index 00000000000..52c9a961426 --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/stubindex/PackageIndexUtil.java @@ -0,0 +1,76 @@ +/* + * Copyright 2010-2014 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.jet.plugin.stubindex; + +import com.google.common.collect.Sets; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.util.Condition; +import com.intellij.psi.search.GlobalSearchScope; +import com.intellij.util.containers.ContainerUtil; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.jet.lang.psi.JetFile; +import org.jetbrains.jet.lang.psi.JetPsiUtil; +import org.jetbrains.jet.lang.resolve.name.FqName; +import org.jetbrains.jet.lang.resolve.name.NamePackage; + +import java.util.Collection; +import java.util.Set; + +public final class PackageIndexUtil { + @NotNull + public static Collection getSubPackageFqNames( + @NotNull FqName packageFqName, + @NotNull GlobalSearchScope scope, + @NotNull Project project + ) { + Collection files = JetAllPackagesIndex.getInstance().get(packageFqName.asString(), project, scope); + + Set result = Sets.newHashSet(); + for (JetFile file : files) { + FqName fqName = JetPsiUtil.getFQName(file); + + assert NamePackage.isSubpackageOf(fqName, packageFqName) : + "Registered package is not a subpackage of actually declared package:\n" + + "in index: " + packageFqName + "\n" + + "declared: " + fqName; + FqName subpackage = NamePackage.plusOneSegment(packageFqName, fqName); + if (subpackage != null) { + result.add(subpackage); + } + } + + return result; + } + + @NotNull + public static Collection findFilesWithExactPackage( + @NotNull final FqName packageFqName, + @NotNull GlobalSearchScope searchScope, + @NotNull Project project + ) { + Collection files = JetAllPackagesIndex.getInstance().get(packageFqName.asString(), project, searchScope); + return ContainerUtil.filter(files, new Condition() { + @Override + public boolean value(JetFile file) { + return packageFqName.equals(JetPsiUtil.getFQName(file)); + } + }); + } + + private PackageIndexUtil() { + } +} diff --git a/idea/src/org/jetbrains/jet/plugin/stubindex/resolve/StubBasedPackageMemberDeclarationProvider.kt b/idea/src/org/jetbrains/jet/plugin/stubindex/resolve/StubBasedPackageMemberDeclarationProvider.kt index 19f6ccb05c4..9d6604dadfe 100644 --- a/idea/src/org/jetbrains/jet/plugin/stubindex/resolve/StubBasedPackageMemberDeclarationProvider.kt +++ b/idea/src/org/jetbrains/jet/plugin/stubindex/resolve/StubBasedPackageMemberDeclarationProvider.kt @@ -31,6 +31,7 @@ import org.jetbrains.jet.plugin.stubindex.JetTopLevelPropertiesFqnNameIndex import java.util.* import org.jetbrains.jet.plugin.stubindex.JetSourceFilterScope.kotlinSources import org.jetbrains.jet.lang.resolve.name.numberOfSegments +import org.jetbrains.jet.plugin.stubindex.PackageIndexUtil public class StubBasedPackageMemberDeclarationProvider( private val fqName: FqName, @@ -59,12 +60,7 @@ public class StubBasedPackageMemberDeclarationProvider( } override fun getAllDeclaredSubPackages(): Collection { - //TODO: duplication with light class generation support - val allPackagesInProject = JetAllPackagesIndex.getInstance().getAllKeys(project) - return allPackagesInProject.filter { - val otherPackageFqName = FqName(it) - !otherPackageFqName.isRoot() && otherPackageFqName.parent() == fqName - }.map { FqName(it) } + return PackageIndexUtil.getSubPackageFqNames(fqName, searchScope, project) } override fun getPackageDeclarations(fqName: FqName): Collection { @@ -80,11 +76,7 @@ public class StubBasedPackageMemberDeclarationProvider( } override fun getPackageFiles(): Collection { - //TODO: duplicate with light class generation support - val files = JetAllPackagesIndex.getInstance().get(fqName.asString(), project, kotlinSources(searchScope)) - return files.filter { - fqName.equals(JetPsiUtil.getFQName(it)) - } + return PackageIndexUtil.findFilesWithExactPackage(fqName, searchScope, project) } private fun childName(name: Name): String {