Index properties defined in primary constructor

#KT-15029 Fixed
This commit is contained in:
Dmitry Jemerov
2017-05-09 19:03:32 +02:00
parent ec53a6dbe7
commit 9c4f897d31
13 changed files with 51 additions and 28 deletions
@@ -23,12 +23,12 @@ object KotlinStubVersions {
// Though only kotlin declarations (no code in the bodies) are stubbed, please do increase this version // Though only kotlin declarations (no code in the bodies) are stubbed, please do increase this version
// if you are not 100% sure it can be avoided. // if you are not 100% sure it can be avoided.
// Increasing this version will lead to reindexing of all kotlin source files on the first IDE startup with the new version. // Increasing this version will lead to reindexing of all kotlin source files on the first IDE startup with the new version.
const val SOURCE_STUB_VERSION = 121 const val SOURCE_STUB_VERSION = 122
// Binary stub version should be increased if stub format (org.jetbrains.kotlin.psi.stubs.impl) is changed // Binary stub version should be increased if stub format (org.jetbrains.kotlin.psi.stubs.impl) is changed
// or changes are made to the core stub building code (org.jetbrains.kotlin.idea.decompiler.stubBuilder). // or changes are made to the core stub building code (org.jetbrains.kotlin.idea.decompiler.stubBuilder).
// Increasing this version will lead to reindexing of all binary files that are potentially kotlin binaries (including all class files). // Increasing this version will lead to reindexing of all binary files that are potentially kotlin binaries (including all class files).
private const val BINARY_STUB_VERSION = 58 private const val BINARY_STUB_VERSION = 59
// Classfile stub version should be increased if changes are made to classfile stub building subsystem (org.jetbrains.kotlin.idea.decompiler.classFile) // Classfile stub version should be increased if changes are made to classfile stub building subsystem (org.jetbrains.kotlin.idea.decompiler.classFile)
// Increasing this version will lead to reindexing of all classfiles. // Increasing this version will lead to reindexing of all classfiles.
@@ -16,6 +16,7 @@
package org.jetbrains.kotlin.psi.stubs.elements; package org.jetbrains.kotlin.psi.stubs.elements;
import com.intellij.psi.stubs.IndexSink;
import com.intellij.psi.stubs.StubElement; import com.intellij.psi.stubs.StubElement;
import com.intellij.psi.stubs.StubInputStream; import com.intellij.psi.stubs.StubInputStream;
import com.intellij.psi.stubs.StubOutputStream; import com.intellij.psi.stubs.StubOutputStream;
@@ -63,4 +64,9 @@ public class KtParameterElementType extends KtStubElementType<KotlinParameterStu
return new KotlinParameterStubImpl(parentStub, fqName, name, isMutable, hasValOrValNode, hasDefaultValue); return new KotlinParameterStubImpl(parentStub, fqName, name, isMutable, hasValOrValNode, hasDefaultValue);
} }
@Override
public void indexStub(@NotNull KotlinParameterStub stub, @NotNull IndexSink sink) {
StubIndexService.getInstance().indexParameter(stub, sink);
}
} }
@@ -44,6 +44,9 @@ open class StubIndexService protected constructor() {
open fun indexProperty(stub: KotlinPropertyStub, sink: IndexSink) { open fun indexProperty(stub: KotlinPropertyStub, sink: IndexSink) {
} }
open fun indexParameter(stub: KotlinParameterStub, sink: IndexSink) {
}
open fun indexAnnotation(stub: KotlinAnnotationEntryStub, sink: IndexSink) { open fun indexAnnotation(stub: KotlinAnnotationEntryStub, sink: IndexSink) {
} }
@@ -247,3 +247,11 @@ object LightClassUtil {
override fun iterator(): Iterator<PsiMethod> = allMethods.iterator() override fun iterator(): Iterator<PsiMethod> = allMethods.iterator()
} }
} }
fun KtNamedDeclaration.getAccessorLightMethods(): LightClassUtil.PropertyAccessorsPsiMethods {
return when (this) {
is KtProperty -> LightClassUtil.getLightClassPropertyMethods(this)
is KtParameter -> LightClassUtil.getLightClassPropertyMethods(this)
else -> throw IllegalStateException("Unexpected property type: ${this}")
}
}
@@ -28,6 +28,7 @@ import com.intellij.util.containers.HashSet
import org.jetbrains.kotlin.asJava.LightClassUtil import org.jetbrains.kotlin.asJava.LightClassUtil
import org.jetbrains.kotlin.asJava.defaultImplsChild import org.jetbrains.kotlin.asJava.defaultImplsChild
import org.jetbrains.kotlin.asJava.finder.JavaElementFinder import org.jetbrains.kotlin.asJava.finder.JavaElementFinder
import org.jetbrains.kotlin.asJava.getAccessorLightMethods
import org.jetbrains.kotlin.asJava.getAccessorNamesCandidatesByPropertyName import org.jetbrains.kotlin.asJava.getAccessorNamesCandidatesByPropertyName
import org.jetbrains.kotlin.fileClasses.javaFileFacadeFqName import org.jetbrains.kotlin.fileClasses.javaFileFacadeFqName
import org.jetbrains.kotlin.idea.stubindex.* import org.jetbrains.kotlin.idea.stubindex.*
@@ -103,7 +104,7 @@ class KotlinShortNamesCache(private val project: Project) : PsiShortNamesCache()
val propertyAccessorsPsi = sequenceOfLazyValues({ getPropertyNamesCandidatesByAccessorName(Name.identifier(name)) }) val propertyAccessorsPsi = sequenceOfLazyValues({ getPropertyNamesCandidatesByAccessorName(Name.identifier(name)) })
.flatMap { it.asSequence() } .flatMap { it.asSequence() }
.flatMap { propertiesIndex.get(it.asString(), project, scope).asSequence() } .flatMap { propertiesIndex.get(it.asString(), project, scope).asSequence() }
.flatMap { LightClassUtil.getLightClassPropertyMethods(it).allDeclarations.asSequence() } .flatMap { it.getAccessorLightMethods().allDeclarations.asSequence() }
.filter { it.name == name } .filter { it.name == name }
.map { it as? PsiMethod } .map { it as? PsiMethod }
@@ -214,6 +214,14 @@ public class IdeStubIndexService extends StubIndexService {
IndexUtilsKt.indexInternals(stub, sink); IndexUtilsKt.indexInternals(stub, sink);
} }
@Override
public void indexParameter(@NotNull KotlinParameterStub stub, @NotNull IndexSink sink) {
String name = stub.getName();
if (name != null && stub.hasValOrVar()) {
sink.occurrence(KotlinPropertyShortNameIndex.getInstance().getKey(), name);
}
}
@Override @Override
public void indexAnnotation(KotlinAnnotationEntryStub stub, IndexSink sink) { public void indexAnnotation(KotlinAnnotationEntryStub stub, IndexSink sink) {
sink.occurrence(KotlinAnnotationsIndex.getInstance().getKey(), stub.getShortName()); sink.occurrence(KotlinAnnotationsIndex.getInstance().getKey(), stub.getShortName());
@@ -22,12 +22,12 @@ import com.intellij.psi.stubs.StringStubIndexExtension;
import com.intellij.psi.stubs.StubIndex; import com.intellij.psi.stubs.StubIndex;
import com.intellij.psi.stubs.StubIndexKey; import com.intellij.psi.stubs.StubIndexKey;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.psi.KtProperty; import org.jetbrains.kotlin.psi.KtNamedDeclaration;
import java.util.Collection; import java.util.Collection;
public class KotlinPropertyShortNameIndex extends StringStubIndexExtension<KtProperty> { public class KotlinPropertyShortNameIndex extends StringStubIndexExtension<KtNamedDeclaration> {
private static final StubIndexKey<String, KtProperty> KEY = KotlinIndexUtil.createIndexKey(KotlinPropertyShortNameIndex.class); private static final StubIndexKey<String, KtNamedDeclaration> KEY = KotlinIndexUtil.createIndexKey(KotlinPropertyShortNameIndex.class);
private static final KotlinPropertyShortNameIndex ourInstance = new KotlinPropertyShortNameIndex(); private static final KotlinPropertyShortNameIndex ourInstance = new KotlinPropertyShortNameIndex();
@@ -39,13 +39,13 @@ public class KotlinPropertyShortNameIndex extends StringStubIndexExtension<KtPro
@NotNull @NotNull
@Override @Override
public StubIndexKey<String, KtProperty> getKey() { public StubIndexKey<String, KtNamedDeclaration> getKey() {
return KEY; return KEY;
} }
@NotNull @NotNull
@Override @Override
public Collection<KtProperty> get(@NotNull String s, @NotNull Project project, @NotNull GlobalSearchScope scope) { public Collection<KtNamedDeclaration> get(@NotNull String s, @NotNull Project project, @NotNull GlobalSearchScope scope) {
return StubIndex.getElements(KEY, s, project, scope, KtProperty.class); return StubIndex.getElements(KEY, s, project, scope, KtNamedDeclaration.class);
} }
} }
@@ -27,9 +27,9 @@ import org.jetbrains.kotlin.idea.core.KotlinIndicesHelper
import org.jetbrains.kotlin.idea.core.targetDescriptors import org.jetbrains.kotlin.idea.core.targetDescriptors
import org.jetbrains.kotlin.idea.resolve.ResolutionFacade import org.jetbrains.kotlin.idea.resolve.ResolutionFacade
import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.KtCallableDeclaration
import org.jetbrains.kotlin.psi.KtClass import org.jetbrains.kotlin.psi.KtClass
import org.jetbrains.kotlin.psi.KtFile import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtNamedDeclaration
import org.jetbrains.kotlin.psi.KtObjectDeclaration import org.jetbrains.kotlin.psi.KtObjectDeclaration
import org.jetbrains.kotlin.resolve.ImportedFromObjectCallableDescriptor import org.jetbrains.kotlin.resolve.ImportedFromObjectCallableDescriptor
import org.jetbrains.kotlin.resolve.scopes.DescriptorKindExclude import org.jetbrains.kotlin.resolve.scopes.DescriptorKindExclude
@@ -99,7 +99,7 @@ class StaticMembersCompletion(
val descriptorKindFilter = DescriptorKindFilter.CALLABLES exclude DescriptorKindExclude.Extensions val descriptorKindFilter = DescriptorKindFilter.CALLABLES exclude DescriptorKindExclude.Extensions
val nameFilter: (String) -> Boolean = { prefixMatcher.prefixMatches(it) } val nameFilter: (String) -> Boolean = { prefixMatcher.prefixMatches(it) }
val filter = { declaration: KtCallableDeclaration, objectDeclaration: KtObjectDeclaration -> val filter = { declaration: KtNamedDeclaration, objectDeclaration: KtObjectDeclaration ->
!declaration.hasModifier(KtTokens.OVERRIDE_KEYWORD) && objectDeclaration.isTopLevelOrCompanion() !declaration.hasModifier(KtTokens.OVERRIDE_KEYWORD) && objectDeclaration.isTopLevelOrCompanion()
} }
indicesHelper.processObjectMembers(descriptorKindFilter, nameFilter, filter) { indicesHelper.processObjectMembers(descriptorKindFilter, nameFilter, filter) {
@@ -75,7 +75,7 @@ class KotlinIndicesHelper(
} }
fun getTopLevelCallablesByName(name: String): Collection<CallableDescriptor> { fun getTopLevelCallablesByName(name: String): Collection<CallableDescriptor> {
val declarations = LinkedHashSet<KtCallableDeclaration>() val declarations = LinkedHashSet<KtNamedDeclaration>()
declarations.addTopLevelNonExtensionCallablesByName(KotlinFunctionShortNameIndex.getInstance(), name) declarations.addTopLevelNonExtensionCallablesByName(KotlinFunctionShortNameIndex.getInstance(), name)
declarations.addTopLevelNonExtensionCallablesByName(KotlinPropertyShortNameIndex.getInstance(), name) declarations.addTopLevelNonExtensionCallablesByName(KotlinPropertyShortNameIndex.getInstance(), name)
return declarations return declarations
@@ -83,11 +83,11 @@ class KotlinIndicesHelper(
.filter { descriptorFilter(it) } .filter { descriptorFilter(it) }
} }
private fun MutableSet<KtCallableDeclaration>.addTopLevelNonExtensionCallablesByName( private fun MutableSet<KtNamedDeclaration>.addTopLevelNonExtensionCallablesByName(
index: StringStubIndexExtension<out KtCallableDeclaration>, index: StringStubIndexExtension<out KtNamedDeclaration>,
name: String name: String
) { ) {
index.get(name, project, scope).filterTo(this) { it.parent is KtFile && it.receiverTypeReference == null } index.get(name, project, scope).filterTo(this) { it.parent is KtFile && it is KtCallableDeclaration && it.receiverTypeReference == null }
} }
fun getTopLevelExtensionOperatorsByName(name: String): Collection<FunctionDescriptor> { fun getTopLevelExtensionOperatorsByName(name: String): Collection<FunctionDescriptor> {
@@ -336,11 +336,11 @@ class KotlinIndicesHelper(
fun processKotlinCallablesByName( fun processKotlinCallablesByName(
name: String, name: String,
filter: (KtCallableDeclaration) -> Boolean, filter: (KtNamedDeclaration) -> Boolean,
processor: (CallableDescriptor) -> Unit processor: (CallableDescriptor) -> Unit
) { ) {
val functions: Sequence<KtCallableDeclaration> = KotlinFunctionShortNameIndex.getInstance().get(name, project, scope).asSequence() val functions: Sequence<KtCallableDeclaration> = KotlinFunctionShortNameIndex.getInstance().get(name, project, scope).asSequence()
val properties: Sequence<KtCallableDeclaration> = KotlinPropertyShortNameIndex.getInstance().get(name, project, scope).asSequence() val properties: Sequence<KtNamedDeclaration> = KotlinPropertyShortNameIndex.getInstance().get(name, project, scope).asSequence()
val processed = HashSet<CallableDescriptor>() val processed = HashSet<CallableDescriptor>()
for (declaration in functions + properties) { for (declaration in functions + properties) {
ProgressManager.checkCanceled() ProgressManager.checkCanceled()
@@ -390,10 +390,10 @@ class KotlinIndicesHelper(
fun processObjectMembers( fun processObjectMembers(
descriptorKindFilter: DescriptorKindFilter, descriptorKindFilter: DescriptorKindFilter,
nameFilter: (String) -> Boolean, nameFilter: (String) -> Boolean,
filter: (KtCallableDeclaration, KtObjectDeclaration) -> Boolean, filter: (KtNamedDeclaration, KtObjectDeclaration) -> Boolean,
processor: (DeclarationDescriptor) -> Unit processor: (DeclarationDescriptor) -> Unit
) { ) {
fun processIndex(index: StringStubIndexExtension<out KtCallableDeclaration>) { fun processIndex(index: StringStubIndexExtension<out KtNamedDeclaration>) {
for (name in index.getAllKeys(project)) { for (name in index.getAllKeys(project)) {
ProgressManager.checkCanceled() ProgressManager.checkCanceled()
if (!nameFilter(name)) continue if (!nameFilter(name)) continue
@@ -34,6 +34,7 @@ import com.intellij.psi.PsiMethod
import com.intellij.psi.PsiNameIdentifierOwner import com.intellij.psi.PsiNameIdentifierOwner
import com.intellij.psi.search.searches.ClassInheritorsSearch import com.intellij.psi.search.searches.ClassInheritorsSearch
import org.jetbrains.kotlin.asJava.LightClassUtil import org.jetbrains.kotlin.asJava.LightClassUtil
import org.jetbrains.kotlin.asJava.getAccessorLightMethods
import org.jetbrains.kotlin.asJava.toLightClass import org.jetbrains.kotlin.asJava.toLightClass
import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor
import org.jetbrains.kotlin.descriptors.MemberDescriptor import org.jetbrains.kotlin.descriptors.MemberDescriptor
@@ -323,14 +324,6 @@ private fun collectHeaderMarkers(declaration: KtNamedDeclaration,
)) ))
} }
fun KtNamedDeclaration.getAccessorLightMethods(): LightClassUtil.PropertyAccessorsPsiMethods {
return when (this) {
is KtProperty -> LightClassUtil.getLightClassPropertyMethods(this)
is KtParameter -> LightClassUtil.getLightClassPropertyMethods(this)
else -> throw IllegalStateException("Unexpected property type: ${this}")
}
}
private fun collectOverriddenFunctions(functions: Collection<KtNamedFunction>, result: MutableCollection<LineMarkerInfo<*>>) { private fun collectOverriddenFunctions(functions: Collection<KtNamedFunction>, result: MutableCollection<LineMarkerInfo<*>>) {
val mappingToJava = HashMap<PsiMethod, KtNamedFunction>() val mappingToJava = HashMap<PsiMethod, KtNamedFunction>()
for (function in functions) { for (function in functions) {
@@ -34,6 +34,7 @@ import com.intellij.util.AdapterProcessor
import com.intellij.util.CommonProcessors import com.intellij.util.CommonProcessors
import com.intellij.util.Function import com.intellij.util.Function
import org.jetbrains.kotlin.asJava.LightClassUtil import org.jetbrains.kotlin.asJava.LightClassUtil
import org.jetbrains.kotlin.asJava.getAccessorLightMethods
import org.jetbrains.kotlin.idea.KotlinBundle import org.jetbrains.kotlin.idea.KotlinBundle
import org.jetbrains.kotlin.idea.search.ideaExtensions.KotlinDefinitionsSearcher import org.jetbrains.kotlin.idea.search.ideaExtensions.KotlinDefinitionsSearcher
import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.lexer.KtTokens
@@ -60,6 +60,7 @@ import com.intellij.util.VisibilityUtil
import com.intellij.util.containers.MultiMap import com.intellij.util.containers.MultiMap
import org.jetbrains.kotlin.asJava.LightClassUtil import org.jetbrains.kotlin.asJava.LightClassUtil
import org.jetbrains.kotlin.asJava.elements.KtLightMethod import org.jetbrains.kotlin.asJava.elements.KtLightMethod
import org.jetbrains.kotlin.asJava.getAccessorLightMethods
import org.jetbrains.kotlin.asJava.namedUnwrappedElement import org.jetbrains.kotlin.asJava.namedUnwrappedElement
import org.jetbrains.kotlin.asJava.toLightClass import org.jetbrains.kotlin.asJava.toLightClass
import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.descriptors.*
@@ -75,7 +76,6 @@ import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptor
import org.jetbrains.kotlin.idea.codeInsight.DescriptorToSourceUtilsIde import org.jetbrains.kotlin.idea.codeInsight.DescriptorToSourceUtilsIde
import org.jetbrains.kotlin.idea.core.* import org.jetbrains.kotlin.idea.core.*
import org.jetbrains.kotlin.idea.core.util.showYesNoCancelDialog import org.jetbrains.kotlin.idea.core.util.showYesNoCancelDialog
import org.jetbrains.kotlin.idea.highlighter.markers.getAccessorLightMethods
import org.jetbrains.kotlin.idea.intentions.RemoveCurlyBracesFromTemplateIntention import org.jetbrains.kotlin.idea.intentions.RemoveCurlyBracesFromTemplateIntention
import org.jetbrains.kotlin.idea.j2k.IdeaJavaToKotlinServices import org.jetbrains.kotlin.idea.j2k.IdeaJavaToKotlinServices
import org.jetbrains.kotlin.idea.refactoring.changeSignature.KotlinValVar import org.jetbrains.kotlin.idea.refactoring.changeSignature.KotlinValVar
+3
View File
@@ -16,8 +16,11 @@ class Some() {
} }
} }
class SomePrimary(val testInPrimary: Int)
// SEARCH_TEXT: test // SEARCH_TEXT: test
// REF: (<root>).testGlobal // REF: (<root>).testGlobal
// REF: (in Some).testInClass // REF: (in Some).testInClass
// REF: (in Some.Companion).testInClassObject // REF: (in Some.Companion).testInClassObject
// REF: (in SomePrimary).testInPrimary
// REF: (in SomeTrait).testInTrait // REF: (in SomeTrait).testInTrait