From 111fe14bd5611b583e2b15feeea6dfad08595eb3 Mon Sep 17 00:00:00 2001 From: vechro Date: Sat, 20 Aug 2022 19:27:13 +0300 Subject: [PATCH] Add caching --- .../intellij/editor/ColorRGBaColorProvider.kt | 35 ++++++++++++------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/src/main/kotlin/org/openrndr/plugin/intellij/editor/ColorRGBaColorProvider.kt b/src/main/kotlin/org/openrndr/plugin/intellij/editor/ColorRGBaColorProvider.kt index 1d3cd77..33e6f90 100644 --- a/src/main/kotlin/org/openrndr/plugin/intellij/editor/ColorRGBaColorProvider.kt +++ b/src/main/kotlin/org/openrndr/plugin/intellij/editor/ColorRGBaColorProvider.kt @@ -8,6 +8,8 @@ import com.intellij.patterns.PsiElementPattern import com.intellij.psi.PsiDocumentManager import com.intellij.psi.PsiElement import com.intellij.psi.impl.source.tree.LeafPsiElement +import com.intellij.psi.util.CachedValueProvider +import com.intellij.psi.util.CachedValuesManager import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.idea.caches.resolve.analyze import org.jetbrains.kotlin.idea.refactoring.fqName.fqName @@ -38,19 +40,28 @@ object ColorRGBaColorProvider : ElementColorProvider { override fun getColorFrom(element: PsiElement): Color? { if (element !is LeafPsiElement) return null if (!COLOR_PROVIDER_PATTERN.accepts(element)) return null - val outerExpression = element.getParentOfTypes2() as? KtExpression - val outerExpressionContext = outerExpression?.analyze() ?: return null - val resolvedCall = outerExpression.getResolvedCall(outerExpressionContext) as? NewAbstractResolvedCall - val descriptor = resolvedCall?.resultingDescriptor + return CachedValuesManager.getCachedValue(element) c@{ + val outerExpression = + element.getParentOfTypes2() as? KtExpression + val outerExpressionContext = outerExpression?.analyze() + val resolvedCall = + outerExpressionContext?.let { outerExpression.getResolvedCall(it) } as? NewAbstractResolvedCall + val descriptor = resolvedCall?.resultingDescriptor - if (descriptor?.isColorModelPackage() != true) return null - if (resolvedCall.kotlinCall?.callKind == KotlinCallKind.VARIABLE) { - return staticColorMap[descriptor.getImportableDescriptor().name.identifier] + if (descriptor?.isColorModelPackage() != true) { + CachedValueProvider.Result.create(null, element) + } else if (resolvedCall.kotlinCall?.callKind == KotlinCallKind.VARIABLE) { + CachedValueProvider.Result.create( + staticColorMap[descriptor.getImportableDescriptor().name.identifier], + element + ) + } else { + val argumentMap = resolvedCall.computeValueArguments(outerExpressionContext) + val colorRGBaDescriptor = ColorRGBaDescriptor.fromCallableDescriptor(descriptor) + val color = argumentMap?.let { colorRGBaDescriptor?.colorFromArguments(it) } + CachedValueProvider.Result.create(color, element) + } } - - val argumentMap = resolvedCall.computeValueArguments(outerExpressionContext) ?: return null - val colorRGBaDescriptor = ColorRGBaDescriptor.fromCallableDescriptor(descriptor) - return colorRGBaDescriptor?.colorFromArguments(argumentMap) } override fun setColorTo(element: PsiElement, color: Color) { @@ -157,7 +168,7 @@ object ColorRGBaColorProvider : ElementColorProvider { * @param replacementArguments replacement arguments which are * retrieved by parameter index and converted into [KtExpression]s */ - fun KtValueArgumentList.constructReplacement( + private fun KtValueArgumentList.constructReplacement( resolvedArgumentMap: Map, replacementArguments: Array ): KtValueArgumentList { val psiFactory = KtPsiFactory(this)