Introduce index for the set of top-level names by package

It should help with actual loading of all the stubs for all declarations
in the package on every access when it's implemented through
`getDeclarations()`
This commit is contained in:
Denis Zharkov
2017-11-17 16:04:30 +03:00
parent ce41b5745a
commit d655c4075c
4 changed files with 81 additions and 3 deletions
@@ -19,8 +19,10 @@ package org.jetbrains.kotlin.idea.stubindex.resolve
import com.intellij.openapi.project.Project
import com.intellij.psi.search.GlobalSearchScope
import com.intellij.psi.stubs.StringStubIndexExtension
import com.intellij.util.indexing.FileBasedIndex
import org.jetbrains.kotlin.idea.stubindex.*
import org.jetbrains.kotlin.idea.util.application.runReadAction
import org.jetbrains.kotlin.idea.vfilefinder.KotlinPackageSourcesMemberNamesIndex
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi.*
@@ -61,9 +63,12 @@ class StubBasedPackageMemberDeclarationProvider(
return result
}
private val declarationNames_ by lazy(LazyThreadSafetyMode.PUBLICATION) {
getDeclarations(DescriptorKindFilter.ALL, { true })
.mapTo(mutableSetOf()) { ResolveSessionUtils.safeNameForLazyResolve(it as KtNamedDeclaration) }
private val declarationNames_: Set<Name> by lazy(LazyThreadSafetyMode.PUBLICATION) {
FileBasedIndex.getInstance()
.getValues(KotlinPackageSourcesMemberNamesIndex.KEY, fqName.asString(), searchScope)
.flatMapTo(hashSetOf()) {
it.map { stringName -> ResolveSessionUtils.safeNameForLazyResolve(Name.identifier(stringName)) }
}
}
override fun getDeclarationNames() = declarationNames_
@@ -0,0 +1,71 @@
/*
* Copyright 2010-2017 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.vfilefinder
import com.intellij.util.indexing.*
import com.intellij.util.io.DataExternalizer
import com.intellij.util.io.EnumeratorStringDescriptor
import org.jetbrains.kotlin.idea.KotlinFileType
import org.jetbrains.kotlin.psi.KtDeclaration
import org.jetbrains.kotlin.psi.KtFile
import java.io.DataInput
import java.io.DataOutput
object KotlinPackageSourcesMemberNamesIndex : FileBasedIndexExtension<String, Collection<String>>() {
val KEY: ID<String, Collection<String>> = ID.create(KotlinPackageSourcesMemberNamesIndex::class.java.canonicalName)
private val KEY_DESCRIPTOR = EnumeratorStringDescriptor()
override fun getName() = KEY
override fun dependsOnFileContent() = true
override fun getKeyDescriptor() = KEY_DESCRIPTOR
override fun getValueExternalizer() = StringSetExternalizer
override fun getInputFilter(): FileBasedIndex.InputFilter =
FileBasedIndex.InputFilter { file -> file.extension == KotlinFileType.EXTENSION }
override fun getVersion(): Int = 1
override fun getIndexer(): DataIndexer<String, Collection<String>, FileContent> =
DataIndexer { inputData ->
val ktFile = inputData.psiFile as KtFile
val packageName = ktFile.packageDirective?.fqName?.asString() ?: ""
mapOf(packageName to ktFile.declarations.mapNotNullTo(hashSetOf(), KtDeclaration::getName))
}
}
object StringSetExternalizer : DataExternalizer<Collection<String>> {
private val ELEMENTS_SERIALIZER = EnumeratorStringDescriptor()
override fun read(input: DataInput): Set<String> {
val size = input.readInt()
return hashSetOf<String>().apply {
repeat(size) {
add(ELEMENTS_SERIALIZER.read(input))
}
}
}
override fun save(output: DataOutput, value: Collection<String>) {
output.writeInt(value.size)
value.toSet().forEach { ELEMENTS_SERIALIZER.save(output, it) }
}
}
+1
View File
@@ -748,6 +748,7 @@
<fileBasedIndex implementation="org.jetbrains.kotlin.idea.vfilefinder.KotlinMetadataFileIndex"/>
<fileBasedIndex implementation="org.jetbrains.kotlin.idea.vfilefinder.KotlinMetadataFilePackageIndex"/>
<fileBasedIndex implementation="org.jetbrains.kotlin.idea.vfilefinder.KotlinModuleMappingIndex"/>
<fileBasedIndex implementation="org.jetbrains.kotlin.idea.vfilefinder.KotlinPackageSourcesMemberNamesIndex"/>
<idIndexer filetype="Kotlin" implementationClass="org.jetbrains.kotlin.idea.search.KotlinIdIndexer"/>
<todoIndexer filetype="Kotlin" implementationClass="org.jetbrains.kotlin.idea.search.KotlinTodoIndexer"/>
@@ -58,6 +58,7 @@ class KotlinUpdatePluginComponent : ApplicationComponent {
fileBasedIndex.requestRebuild(KotlinMetadataFileIndex.KEY)
fileBasedIndex.requestRebuild(KotlinMetadataFilePackageIndex.KEY)
fileBasedIndex.requestRebuild(KotlinModuleMappingIndex.KEY)
fileBasedIndex.requestRebuild(KotlinPackageSourcesMemberNamesIndex.KEY)
PropertiesComponent.getInstance()?.setValue(INSTALLED_KOTLIN_VERSION, KotlinPluginUtil.getPluginVersion())
}