Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add caching #12

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<KtCallExpression, KtDotQualifiedExpression>() 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<KtCallExpression, KtDotQualifiedExpression>() 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) {
Expand Down Expand Up @@ -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<ValueParameterDescriptor, ResolvedValueArgument>, replacementArguments: Array<String>
): KtValueArgumentList {
val psiFactory = KtPsiFactory(this)
Expand Down