Improve @ResolveContext resolution performance

This commit is contained in:
Him188 2021-04-08 12:56:47 +08:00
parent 662013e4ec
commit f1d4f3fef6
2 changed files with 15 additions and 13 deletions

View File

@ -62,8 +62,9 @@ class ContextualParametersChecker : DeclarationChecker {
context: DeclarationCheckerContext, context: DeclarationCheckerContext,
) { ) {
val elementCheckers = parameter.resolveContextKinds?.mapNotNull(checkersMap::get) ?: return val elementCheckers = parameter.resolveContextKinds?.mapNotNull(checkersMap::get) ?: return
if (elementCheckers.isEmpty()) return
val resolvedConstants = argument.resolveStringConstantValues()?.toList().orEmpty() val resolvedConstants = argument.resolveStringConstantValues(context.bindingContext)?.toList() ?: return
for (elementChecker in elementCheckers) { for (elementChecker in elementCheckers) {
if (resolvedConstants.isEmpty()) { if (resolvedConstants.isEmpty()) {

View File

@ -10,17 +10,19 @@
package net.mamoe.mirai.console.intellij.resolve package net.mamoe.mirai.console.intellij.resolve
import com.intellij.openapi.project.Project import com.intellij.openapi.project.Project
import com.intellij.psi.* import com.intellij.psi.PsiClass
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiElementFactory
import com.intellij.psi.PsiModifierListOwner
import com.intellij.psi.util.parents import com.intellij.psi.util.parents
import net.mamoe.mirai.console.compiler.common.castOrNull
import net.mamoe.mirai.console.compiler.common.resolve.* import net.mamoe.mirai.console.compiler.common.resolve.*
import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.idea.caches.resolve.analyze import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.caches.resolve.resolveToCall import org.jetbrains.kotlin.idea.caches.resolve.resolveToCall
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
import org.jetbrains.kotlin.idea.refactoring.fqName.fqName import org.jetbrains.kotlin.idea.refactoring.fqName.fqName
import org.jetbrains.kotlin.idea.refactoring.fqName.getKotlinFqName import org.jetbrains.kotlin.idea.refactoring.fqName.getKotlinFqName
import org.jetbrains.kotlin.idea.references.KtSimpleNameReference import org.jetbrains.kotlin.idea.references.KtSimpleNameReference
import org.jetbrains.kotlin.idea.references.resolveToDescriptors
import org.jetbrains.kotlin.incremental.components.NoLookupLocation import org.jetbrains.kotlin.incremental.components.NoLookupLocation
import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.name.Name
@ -194,8 +196,8 @@ fun ResolvedCall<*>.valueParametersWithArguments(): List<Pair<ValueParameterDesc
return this.valueParameters.zip(this.valueArgumentsByIndex?.mapNotNull { it.arguments.firstOrNull() }.orEmpty()) return this.valueParameters.zip(this.valueArgumentsByIndex?.mapNotNull { it.arguments.firstOrNull() }.orEmpty())
} }
fun ValueArgument.resolveStringConstantValues(): Sequence<String>? { fun ValueArgument.resolveStringConstantValues(bindingContext: BindingContext): Sequence<String>? {
return this.getArgumentExpression()?.resolveStringConstantValues() return this.getArgumentExpression()?.resolveStringConstantValues(bindingContext)
} }
val PsiElement.allChildrenFlat: Sequence<PsiElement> val PsiElement.allChildrenFlat: Sequence<PsiElement>
@ -248,18 +250,17 @@ fun DeclarationDescriptor.companionObjectDescriptor(): ClassDescriptor? {
return this.companionObjectDescriptor return this.companionObjectDescriptor
} }
fun KtExpression.resolveStringConstantValues(): Sequence<String> { fun KtExpression.resolveStringConstantValues(bindingContext: BindingContext): Sequence<String> {
when (this) { when (this) {
is KtNameReferenceExpression -> { is KtNameReferenceExpression -> {
when (val reference = references.firstIsInstance<KtSimpleNameReference>().resolve()) { when (val descriptor = references.firstIsInstance<KtSimpleNameReference>().resolveToDescriptors(bindingContext).singleOrNull()) {
is KtDeclaration -> { is VariableDescriptor -> {
val descriptor = reference.resolveToDescriptorIfAny(BodyResolveMode.FULL).castOrNull<VariableDescriptor>() ?: return emptySequence()
val compileTimeConstant = descriptor.compileTimeInitializer ?: return emptySequence() val compileTimeConstant = descriptor.compileTimeInitializer ?: return emptySequence()
return compileTimeConstant.selfOrChildrenConstantStrings() return compileTimeConstant.selfOrChildrenConstantStrings()
} }
is PsiDeclarationStatement -> { //is PsiDeclarationStatement -> {
// TODO: 2020/9/18 compile-time constants from Java // // TODO: 2020/9/18 compile-time constants from Java
} //}
} }
} }
is KtStringTemplateExpression -> { is KtStringTemplateExpression -> {