From 83336553df1d99f2a5297f04c3dd5da82a5bd66f Mon Sep 17 00:00:00 2001 From: Dmitry Petrov Date: Thu, 6 Aug 2015 10:44:27 +0300 Subject: [PATCH] Use special index to record files corresponding to static facade classes. --- .../CliLightClassGenerationSupport.java | 5 ++- .../load/kotlin/PackagePartClassUtils.java | 12 ++++-- .../IDELightClassGenerationSupport.java | 9 ++--- .../stubindex/JetStaticFacadeClassIndex.kt | 38 +++++++++++++++++++ .../idea/stubindex/StaticFacadeIndexUtil.kt | 34 +++++++++++++++++ .../idea/stubindex/StubIndexServiceImpl.java | 8 ++++ idea/src/META-INF/plugin.xml | 1 + 7 files changed, 96 insertions(+), 11 deletions(-) create mode 100644 idea/idea-analysis/src/org/jetbrains/kotlin/idea/stubindex/JetStaticFacadeClassIndex.kt create mode 100644 idea/idea-analysis/src/org/jetbrains/kotlin/idea/stubindex/StaticFacadeIndexUtil.kt diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/CliLightClassGenerationSupport.java b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/CliLightClassGenerationSupport.java index 9b2853f697a..dc574055797 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/CliLightClassGenerationSupport.java +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/CliLightClassGenerationSupport.java @@ -214,8 +214,7 @@ public class CliLightClassGenerationSupport extends LightClassGenerationSupport @NotNull @Override public Collection getFacadeClasses(@NotNull FqName facadeFqName, @NotNull GlobalSearchScope scope) { - Collection filesInPackage = findFilesForPackage(facadeFqName.parent(), scope); - List filesForFacade = PackagePartClassUtils.getFilesForFacade(facadeFqName, filesInPackage); + Collection filesForFacade = findFilesForFacade(facadeFqName, scope); if (filesForFacade.isEmpty()) return Collections.emptyList(); //noinspection RedundantTypeArguments @@ -226,6 +225,8 @@ public class CliLightClassGenerationSupport extends LightClassGenerationSupport @NotNull @Override public Collection findFilesForFacade(@NotNull FqName facadeFqName, @NotNull GlobalSearchScope scope) { + // TODO We need a way to plug some platform-dependent stuff into LazyTopDownAnalyzer. + // It already performs some ad hoc stuff for packages->files mapping, anyway. Collection filesInPackage = findFilesForPackage(facadeFqName.parent(), scope); return PackagePartClassUtils.getFilesForFacade(facadeFqName, filesInPackage); } diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/load/kotlin/PackagePartClassUtils.java b/compiler/frontend.java/src/org/jetbrains/kotlin/load/kotlin/PackagePartClassUtils.java index 3a84d1ffce4..93c25e18c20 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/load/kotlin/PackagePartClassUtils.java +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/load/kotlin/PackagePartClassUtils.java @@ -77,8 +77,14 @@ public class PackagePartClassUtils { @NotNull public static FqName getPackagePartFqName(@NotNull FqName facadeFqName, @NotNull VirtualFile file, @Nullable JetFile jetFile) { - String fileName = FileUtil.getNameWithoutExtension(PathUtil.getFileName(file.getName())); - return facadeFqName.parent().child(Name.identifier(getSanitizedClassNameBase(fileName) + FACADE_CLASS_NAME_SUFFIX)); + String fileName = file.getName(); + return getStaticFacadeClassFqNameForFile(facadeFqName.parent(), fileName); + } + + @NotNull + private static FqName getStaticFacadeClassFqNameForFile(@NotNull FqName packageFqName, String fileName) { + String nameWithoutExtension = FileUtil.getNameWithoutExtension(PathUtil.getFileName(fileName)); + return packageFqName.child(Name.identifier(getSanitizedClassNameBase(nameWithoutExtension) + FACADE_CLASS_NAME_SUFFIX)); } public static boolean isFacadeClassFqName(@NotNull FqName classFqName) { @@ -98,7 +104,7 @@ public class PackagePartClassUtils { @NotNull public static FqName getPackagePartFqName(@NotNull JetFile file) { - return getPackagePartFqName(getPackageClassFqName(file.getPackageFqName()), file.getVirtualFile(), file); + return getStaticFacadeClassFqNameForFile(file.getPackageFqName(), file.getName()); } @NotNull diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/caches/resolve/IDELightClassGenerationSupport.java b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/caches/resolve/IDELightClassGenerationSupport.java index 6e5b274ade4..707a85efd62 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/caches/resolve/IDELightClassGenerationSupport.java +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/caches/resolve/IDELightClassGenerationSupport.java @@ -46,6 +46,7 @@ import org.jetbrains.kotlin.idea.resolve.ResolutionFacade; import org.jetbrains.kotlin.idea.stubindex.JetFullClassNameIndex; import org.jetbrains.kotlin.idea.stubindex.JetTopLevelClassByPackageIndex; import org.jetbrains.kotlin.idea.stubindex.PackageIndexUtil; +import org.jetbrains.kotlin.idea.stubindex.StaticFacadeIndexUtil; import org.jetbrains.kotlin.idea.util.ProjectRootsUtil; import org.jetbrains.kotlin.incremental.components.NoLookupLocation; import org.jetbrains.kotlin.load.kotlin.PackagePartClassUtils; @@ -350,16 +351,12 @@ public class IDELightClassGenerationSupport extends LightClassGenerationSupport @NotNull @Override public Collection findFilesForFacade(@NotNull FqName facadeFqName, @NotNull GlobalSearchScope scope) { - // TODO naive version. Production variant should use an equivalent of JetExactPackageIndex. - Collection packageFiles = findFilesForPackage(facadeFqName.parent(), scope); - return PackagePartClassUtils.getFilesForFacade(facadeFqName, packageFiles); + return StaticFacadeIndexUtil.findFilesForStaticFacade(facadeFqName, kotlinSourceAndClassFiles(scope, project), project); } @NotNull private List findFacadeClassesInfos(FqName facadeFqName, GlobalSearchScope scope) { - // TODO naive version. Production variant should use an equivalent of JetExactPackageIndex. - Collection packageFiles = findFilesForPackage(facadeFqName.parent(), scope); - List facadeFiles = PackagePartClassUtils.getFilesForFacade(facadeFqName, packageFiles); + Collection facadeFiles = findFilesForFacade(facadeFqName, scope); Map> filesByInfo = groupByModuleInfo(facadeFiles); List result = new ArrayList(); for (Map.Entry> entry : filesByInfo.entrySet()) { diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/stubindex/JetStaticFacadeClassIndex.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/stubindex/JetStaticFacadeClassIndex.kt new file mode 100644 index 00000000000..5dc7c932b09 --- /dev/null +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/stubindex/JetStaticFacadeClassIndex.kt @@ -0,0 +1,38 @@ +/* + * Copyright 2010-2015 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.kotlin.idea.stubindex + +import com.intellij.openapi.project.Project +import com.intellij.psi.search.GlobalSearchScope +import com.intellij.psi.stubs.StringStubIndexExtension +import com.intellij.psi.stubs.StubIndexKey +import org.jetbrains.kotlin.psi.JetFile + +public class JetStaticFacadeClassIndex private constructor() : StringStubIndexExtension() { + override fun getKey(): StubIndexKey = KEY + + override fun get(key: String, project: Project, scope: GlobalSearchScope) = + super.get(key, project, JetSourceFilterScope.kotlinSourcesAndLibraries(scope, project)) + + companion object { + private val KEY = KotlinIndexUtil.createIndexKey(javaClass()) + + public val INSTANCE: JetStaticFacadeClassIndex = JetStaticFacadeClassIndex() + + public fun getInstance(): JetStaticFacadeClassIndex = INSTANCE + } +} \ No newline at end of file diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/stubindex/StaticFacadeIndexUtil.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/stubindex/StaticFacadeIndexUtil.kt new file mode 100644 index 00000000000..2a3e886d429 --- /dev/null +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/stubindex/StaticFacadeIndexUtil.kt @@ -0,0 +1,34 @@ +/* + * Copyright 2010-2015 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.kotlin.idea.stubindex + +import com.intellij.openapi.project.Project +import com.intellij.psi.search.GlobalSearchScope +import org.jetbrains.kotlin.name.FqName +import org.jetbrains.kotlin.psi.JetFile +import kotlin.platform.platformStatic + +public object StaticFacadeIndexUtil { + + platformStatic public fun findFilesForStaticFacade( + facadeFqName: FqName, + searchScope: GlobalSearchScope, + project: Project + ) : Collection = + JetStaticFacadeClassIndex.INSTANCE.get(facadeFqName.asString(), project, searchScope) + +} \ No newline at end of file diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/stubindex/StubIndexServiceImpl.java b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/stubindex/StubIndexServiceImpl.java index 256808babd9..7f8f111958c 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/stubindex/StubIndexServiceImpl.java +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/stubindex/StubIndexServiceImpl.java @@ -17,9 +17,11 @@ package org.jetbrains.kotlin.idea.stubindex; import com.intellij.psi.stubs.IndexSink; +import org.jetbrains.kotlin.load.kotlin.PackagePartClassUtils; import com.intellij.psi.stubs.StubElement; import org.jetbrains.kotlin.name.FqName; import org.jetbrains.kotlin.psi.JetClassOrObject; +import org.jetbrains.kotlin.psi.JetFile; import org.jetbrains.kotlin.psi.stubs.*; import org.jetbrains.kotlin.psi.stubs.elements.StubIndexService; import org.jetbrains.kotlin.util.UtilPackage; @@ -33,6 +35,12 @@ public class StubIndexServiceImpl implements StubIndexService { FqName packageFqName = stub.getPackageFqName(); sink.occurrence(JetExactPackagesIndex.getInstance().getKey(), packageFqName.asString()); + + JetFile psi = stub.getPsi(); + if (psi != null) { + FqName staticFacadeFqName = PackagePartClassUtils.getPackagePartFqName(psi); + sink.occurrence(JetStaticFacadeClassIndex.INSTANCE.getKey(), staticFacadeFqName.asString()); + } } @Override diff --git a/idea/src/META-INF/plugin.xml b/idea/src/META-INF/plugin.xml index 4ddb8b09dfb..b39e0dd5034 100644 --- a/idea/src/META-INF/plugin.xml +++ b/idea/src/META-INF/plugin.xml @@ -497,6 +497,7 @@ +