From f1d4f3fef679dc11371ed8d3021bba6ddce766c0 Mon Sep 17 00:00:00 2001 From: Him188 Date: Thu, 8 Apr 2021 12:56:47 +0800 Subject: [PATCH] Improve `@ResolveContext` resolution performance --- .../ContextualParametersChecker.kt | 3 ++- .../src/resolve/resolveIdea.kt | 25 ++++++++++--------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/tools/intellij-plugin/src/diagnostics/ContextualParametersChecker.kt b/tools/intellij-plugin/src/diagnostics/ContextualParametersChecker.kt index 5226037b5..f63c90ca9 100644 --- a/tools/intellij-plugin/src/diagnostics/ContextualParametersChecker.kt +++ b/tools/intellij-plugin/src/diagnostics/ContextualParametersChecker.kt @@ -62,8 +62,9 @@ class ContextualParametersChecker : DeclarationChecker { context: DeclarationCheckerContext, ) { 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) { if (resolvedConstants.isEmpty()) { diff --git a/tools/intellij-plugin/src/resolve/resolveIdea.kt b/tools/intellij-plugin/src/resolve/resolveIdea.kt index 5a38ebf99..f16c33468 100644 --- a/tools/intellij-plugin/src/resolve/resolveIdea.kt +++ b/tools/intellij-plugin/src/resolve/resolveIdea.kt @@ -10,17 +10,19 @@ package net.mamoe.mirai.console.intellij.resolve 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 net.mamoe.mirai.console.compiler.common.castOrNull import net.mamoe.mirai.console.compiler.common.resolve.* import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.idea.caches.resolve.analyze 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.getKotlinFqName 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.name.FqName import org.jetbrains.kotlin.name.Name @@ -194,8 +196,8 @@ fun ResolvedCall<*>.valueParametersWithArguments(): List? { - return this.getArgumentExpression()?.resolveStringConstantValues() +fun ValueArgument.resolveStringConstantValues(bindingContext: BindingContext): Sequence? { + return this.getArgumentExpression()?.resolveStringConstantValues(bindingContext) } val PsiElement.allChildrenFlat: Sequence @@ -248,18 +250,17 @@ fun DeclarationDescriptor.companionObjectDescriptor(): ClassDescriptor? { return this.companionObjectDescriptor } -fun KtExpression.resolveStringConstantValues(): Sequence { +fun KtExpression.resolveStringConstantValues(bindingContext: BindingContext): Sequence { when (this) { is KtNameReferenceExpression -> { - when (val reference = references.firstIsInstance().resolve()) { - is KtDeclaration -> { - val descriptor = reference.resolveToDescriptorIfAny(BodyResolveMode.FULL).castOrNull() ?: return emptySequence() + when (val descriptor = references.firstIsInstance().resolveToDescriptors(bindingContext).singleOrNull()) { + is VariableDescriptor -> { val compileTimeConstant = descriptor.compileTimeInitializer ?: return emptySequence() return compileTimeConstant.selfOrChildrenConstantStrings() } - is PsiDeclarationStatement -> { - // TODO: 2020/9/18 compile-time constants from Java - } + //is PsiDeclarationStatement -> { + // // TODO: 2020/9/18 compile-time constants from Java + //} } } is KtStringTemplateExpression -> {