Minor code changes after code review

This commit is contained in:
Valentin Kipyatkov
2015-04-01 19:42:11 +03:00
parent b45b462bdf
commit 784b4a8aeb
7 changed files with 36 additions and 38 deletions
@@ -114,10 +114,3 @@ public trait KotlinTypeProjectionStub : StubElement<JetTypeProjection> {
public trait KotlinUserTypeStub : StubElement<JetUserType> { public trait KotlinUserTypeStub : StubElement<JetUserType> {
public fun isAbsoluteInRootPackage(): Boolean public fun isAbsoluteInRootPackage(): Boolean
} }
public fun StubElement<*>.getContainingFileStub(): PsiFileStub<*> {
return if (this is PsiFileStub)
this
else
getParentStub().getContainingFileStub()
}
@@ -18,12 +18,12 @@ package org.jetbrains.kotlin.util
import com.google.common.collect.HashMultimap import com.google.common.collect.HashMultimap
import com.google.common.collect.Multimap import com.google.common.collect.Multimap
import com.google.common.collect.Multimaps
import com.intellij.openapi.util.Key import com.intellij.openapi.util.Key
import com.intellij.psi.stubs.PsiFileStub
import com.intellij.psi.stubs.StubElement
import org.jetbrains.kotlin.psi.JetFile import org.jetbrains.kotlin.psi.JetFile
import org.jetbrains.kotlin.psi.JetTypeReference import org.jetbrains.kotlin.psi.JetTypeReference
import org.jetbrains.kotlin.psi.JetUserType import org.jetbrains.kotlin.psi.JetUserType
import org.jetbrains.kotlin.psi.stubs.getContainingFileStub
public fun JetUserType.aliasImportMap(): Multimap<String, String> { public fun JetUserType.aliasImportMap(): Multimap<String, String> {
// we need to access containing file via stub because getPsi() may return null when indexing and getContainingFile() will crash // we need to access containing file via stub because getPsi() may return null when indexing and getContainingFile() will crash
@@ -69,3 +69,9 @@ public fun JetUserType?.isProbablyNothing(): Boolean {
return referencedName == "Nothing" || aliasImportMap()[referencedName].contains("Nothing") return referencedName == "Nothing" || aliasImportMap()[referencedName].contains("Nothing")
} }
private fun StubElement<*>.getContainingFileStub(): PsiFileStub<*> {
return if (this is PsiFileStub)
this
else
getParentStub().getContainingFileStub()
}
@@ -22,7 +22,6 @@ import com.intellij.openapi.util.Key
import com.intellij.psi.stubs.IndexSink import com.intellij.psi.stubs.IndexSink
import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.stubs.KotlinCallableStubBase import org.jetbrains.kotlin.psi.stubs.KotlinCallableStubBase
import org.jetbrains.kotlin.psi.stubs.getContainingFileStub
import org.jetbrains.kotlin.util.aliasImportMap import org.jetbrains.kotlin.util.aliasImportMap
fun indexTopLevelExtension<TDeclaration : JetCallableDeclaration>(stub: KotlinCallableStubBase<TDeclaration>, sink: IndexSink) { fun indexTopLevelExtension<TDeclaration : JetCallableDeclaration>(stub: KotlinCallableStubBase<TDeclaration>, sink: IndexSink) {
@@ -32,7 +31,7 @@ fun indexTopLevelExtension<TDeclaration : JetCallableDeclaration>(stub: KotlinCa
} }
} }
private fun <TDeclaration : JetCallableDeclaration> JetTypeElement.index(declaration: TDeclaration, sink: IndexSink) { private fun JetTypeElement.index<TDeclaration : JetCallableDeclaration>(declaration: TDeclaration, sink: IndexSink) {
fun occurrence(typeName: String) { fun occurrence(typeName: String) {
val name = declaration.getName() ?: return val name = declaration.getName() ?: return
sink.occurrence(JetTopLevelExtensionsByReceiverTypeIndex.INSTANCE.getKey(), sink.occurrence(JetTopLevelExtensionsByReceiverTypeIndex.INSTANCE.getKey(),
@@ -43,17 +42,16 @@ private fun <TDeclaration : JetCallableDeclaration> JetTypeElement.index(declara
is JetUserType -> { is JetUserType -> {
var referenceName = getReferencedName() ?: return var referenceName = getReferencedName() ?: return
if (declaration is JetNamedFunction) { val typeParameter = declaration.getTypeParameters().firstOrNull { it.getName() == referenceName }
val typeParameter = declaration.getTypeParameters().firstOrNull { it.getName() == referenceName } if (typeParameter != null) {
if (typeParameter != null) { val bound = typeParameter.getExtendsBound()
val bound = typeParameter.getExtendsBound() if (bound != null) {
if (bound != null) { bound.getTypeElement()?.index(declaration, sink)
bound.getTypeElement()?.index(declaration, sink)
return
}
occurrence("Any")
return
} }
else {
occurrence("Any")
}
return
} }
occurrence(referenceName) occurrence(referenceName)
@@ -68,7 +66,9 @@ private fun <TDeclaration : JetCallableDeclaration> JetTypeElement.index(declara
occurrence(typeName) occurrence(typeName)
} }
else -> occurrence("Any") is JetDynamicType -> occurrence("Any")
else -> error("Unsupported type: $this")
} }
} }
@@ -31,13 +31,14 @@ public class JetTopLevelExtensionsByReceiverTypeIndex private() : StringStubInde
companion object { companion object {
private val KEY = KotlinIndexUtil.createIndexKey<String, JetCallableDeclaration>(javaClass<JetTopLevelExtensionsByReceiverTypeIndex>()) private val KEY = KotlinIndexUtil.createIndexKey<String, JetCallableDeclaration>(javaClass<JetTopLevelExtensionsByReceiverTypeIndex>())
private val SEPARATOR = '\n'
public val INSTANCE: JetTopLevelExtensionsByReceiverTypeIndex = JetTopLevelExtensionsByReceiverTypeIndex() public val INSTANCE: JetTopLevelExtensionsByReceiverTypeIndex = JetTopLevelExtensionsByReceiverTypeIndex()
public fun buildKey(receiverTypeName: String, callableName: String): String = receiverTypeName + "\n" + callableName public fun buildKey(receiverTypeName: String, callableName: String): String = receiverTypeName + SEPARATOR + callableName
public fun receiverTypeNameFromKey(key: String): String = key.substringBefore('\n', "") public fun receiverTypeNameFromKey(key: String): String = key.substringBefore(SEPARATOR, "")
public fun callableNameFromKey(key: String): String = key.substringAfter('\n', "") public fun callableNameFromKey(key: String): String = key.substringAfter(SEPARATOR, "")
} }
} }
@@ -102,7 +102,7 @@ public class KotlinIndicesHelper(
} }
private fun possibleReceiverTypeNames(receiverValues: Collection<ReceiverValue>, dataFlowInfo: DataFlowInfo): Set<String> { private fun possibleReceiverTypeNames(receiverValues: Collection<ReceiverValue>, dataFlowInfo: DataFlowInfo): Set<String> {
val result = LinkedHashSet<String>() val result = HashSet<String>()
for (receiverValue in receiverValues) { for (receiverValue in receiverValues) {
for (type in SmartCastUtils.getSmartCastVariants(receiverValue, bindingContext, dataFlowInfo)) { for (type in SmartCastUtils.getSmartCastVariants(receiverValue, bindingContext, dataFlowInfo)) {
result.addTypeNames(type) result.addTypeNames(type)
@@ -111,6 +111,12 @@ public class KotlinIndicesHelper(
return result return result
} }
private fun MutableCollection<String>.addTypeNames(type: JetType) {
val constructor = type.getConstructor()
addIfNotNull(constructor.getDeclarationDescriptor()?.getName()?.asString())
constructor.getSupertypes().forEach { addTypeNames(it) }
}
private fun receiverValues(expression: JetSimpleNameExpression): Collection<Pair<ReceiverValue, CallType>> { private fun receiverValues(expression: JetSimpleNameExpression): Collection<Pair<ReceiverValue, CallType>> {
val receiverPair = ReferenceVariantsHelper.getExplicitReceiverData(expression) val receiverPair = ReferenceVariantsHelper.getExplicitReceiverData(expression)
if (receiverPair != null) { if (receiverPair != null) {
@@ -125,17 +131,10 @@ public class KotlinIndicesHelper(
} }
else { else {
val resolutionScope = bindingContext[BindingContext.RESOLUTION_SCOPE, expression] ?: return emptyList() val resolutionScope = bindingContext[BindingContext.RESOLUTION_SCOPE, expression] ?: return emptyList()
return resolutionScope.getImplicitReceiversWithInstance().map { it.getValue() to CallType.NORMAL } return resolutionScope.getImplicitReceiversWithInstance().map { it.getValue() to CallType.NORMAL }
} }
} }
private fun MutableCollection<String>.addTypeNames(type: JetType) {
val constructor = type.getConstructor()
addIfNotNull(constructor.getDeclarationDescriptor()?.getName()?.asString())
constructor.getSupertypes().forEach { addTypeNames(it) }
}
/** /**
* Check that function or property with the given qualified name can be resolved in given scope and called on given receiver * Check that function or property with the given qualified name can be resolved in given scope and called on given receiver
*/ */
@@ -7,11 +7,11 @@ fun String.helloWithParams(i : Int) : String {
return "" return ""
} }
fun String.helloFunPreventAutoInsert() {
}
fun <T: CharSequence> T.helloFunGeneric() { fun <T: CharSequence> T.helloFunGeneric() {
} }
fun Int.helloFake() { fun Int.helloFake() {
} }
fun dynamic.helloDynamic() {
}
@@ -6,8 +6,7 @@ fun firstFun() {
} }
// EXIST: helloFun // EXIST: helloFun
// EXIST: helloFunPreventAutoInsert
// EXIST: helloWithParams // EXIST: helloWithParams
// EXIST: helloFunGeneric // EXIST: helloFunGeneric
// EXIST: helloDynamic
// ABSENT: helloFake // ABSENT: helloFake
// NUMBER: 4