[Analysis API] Add KtReferenceMutateService...

and delegate renaming and rebinding to this new service for `KtReference`.

In addition, CliKtFe10XXXReference are all removed because it seems they are not necessary because the IDE flavor differs only by handling renaming, which is moved to a separate service.
This commit is contained in:
Tianyu Geng
2022-05-02 22:09:58 +00:00
committed by Ilya Kirillov
parent 213e7154c2
commit b5139c958b
20 changed files with 79 additions and 149 deletions
@@ -28,8 +28,6 @@ abstract class KtArrayAccessReference(
override fun canRename() = true
abstract override fun handleElementRename(newElementName: String): PsiElement?
companion object {
private val NAMES = listOf(OperatorNameConventions.GET, OperatorNameConventions.SET)
}
@@ -19,17 +19,10 @@ abstract class KtDestructuringDeclarationReference(
override fun getRangeInElement() = TextRange(0, element.textLength)
abstract override fun canRename(): Boolean
override fun resolve() = multiResolve(false).asSequence()
.map { it.element }
.first { it is KtDestructuringDeclarationEntry }
override fun handleElementRename(newElementName: String): PsiElement? {
if (canRename()) return expression
throw IncorrectOperationException()
}
override val resolvesByNames: Collection<Name>
get() {
val destructuringParent = element.parent as? KtDestructuringDeclaration ?: return emptyList()
@@ -63,28 +63,6 @@ abstract class KtInvokeFunctionReference(expression: KtCallExpression) : KtSimpl
override fun canRename(): Boolean = true
override fun handleElementRename(newElementName: String): PsiElement? {
val callExpression = expression
val fullCallExpression = callExpression.getQualifiedExpressionForSelectorOrThis()
if (newElementName == OperatorNameConventions.GET.asString() && callExpression.typeArguments.isEmpty()) {
val arrayAccessExpression = KtPsiFactory(callExpression).buildExpression {
if (fullCallExpression is KtQualifiedExpression) {
appendExpression(fullCallExpression.receiverExpression)
appendFixedText(fullCallExpression.operationSign.value)
}
appendExpression(callExpression.calleeExpression)
appendFixedText("[")
appendExpressions(callExpression.valueArguments.map { it.getArgumentExpression() })
appendFixedText("]")
}
return fullCallExpression.replace(arrayAccessExpression)
}
return doRenameImplicitConventionalCall(newElementName)
}
protected abstract fun doRenameImplicitConventionalCall(newName: String?): KtExpression
companion object {
private val NAMES = listOf(OperatorNameConventions.INVOKE)
}
@@ -5,6 +5,8 @@
package org.jetbrains.kotlin.idea.references
import com.intellij.openapi.application.Application
import com.intellij.openapi.components.ServiceManager
import com.intellij.psi.*
import com.intellij.psi.impl.source.resolve.ResolveCache
import com.intellij.util.IncorrectOperationException
@@ -34,9 +36,18 @@ abstract class AbstractKtReference<T : KtElement>(element: T) : PsiPolyVariantRe
override fun getCanonicalText(): String = "<TBD>"
open fun canRename(): Boolean = false
override fun handleElementRename(newElementName: String): PsiElement? = throw IncorrectOperationException()
override fun handleElementRename(newElementName: String): PsiElement? =
if (canRename())
getKtReferenceMutateService().handleElementRename(this, newElementName)
else
null
override fun bindToElement(element: PsiElement): PsiElement = throw IncorrectOperationException()
override fun bindToElement(element: PsiElement): PsiElement =
getKtReferenceMutateService().bindToElement(this, element)
protected fun getKtReferenceMutateService(): KtReferenceMutateService =
ServiceManager.getService(KtReferenceMutateService::class.java)
?: throw IllegalStateException("Cannot handle element rename because KtReferenceMutateService is missing")
@Suppress("UNCHECKED_CAST")
override fun getVariants(): Array<Any> = PsiReference.EMPTY_ARRAY as Array<Any>
@@ -0,0 +1,36 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.idea.references
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.name.FqName
/**
* Service that is responsible for mutating PSI through [KtReference].
*
* This service is only needed by IDE to handle element renaming or change elements bound to a [KtReference].
*/
interface KtReferenceMutateService {
/**
* See [com.intellij.psi.PsiReference.handleElementRename].
*/
fun handleElementRename(ktReference: KtReference, newElementName: String): PsiElement?
/**
* See [com.intellij.psi.PsiReference.bindToElement].
*/
fun bindToElement(ktReference: KtReference, element: PsiElement): PsiElement
fun bindToElement(simpleNameReference: KtSimpleNameReference, element: PsiElement, shorteningMode: KtSimpleNameReference.ShorteningMode): PsiElement
fun bindToFqName(
simpleNameReference: KtSimpleNameReference,
fqName: FqName,
shorteningMode: KtSimpleNameReference.ShorteningMode = KtSimpleNameReference.ShorteningMode.DELAYED_SHORTENING,
targetElement: PsiElement? = null
): PsiElement
}
@@ -40,25 +40,21 @@ abstract class KtSimpleNameReference(expression: KtSimpleNameExpression) : KtSim
return true
}
abstract override fun handleElementRename(newElementName: String): PsiElement?
enum class ShorteningMode {
NO_SHORTENING,
DELAYED_SHORTENING,
FORCED_SHORTENING
}
// By default reference binding is delayed
override fun bindToElement(element: PsiElement): PsiElement =
bindToElement(element, ShorteningMode.DELAYED_SHORTENING)
fun bindToElement(element: PsiElement, shorteningMode: ShorteningMode = ShorteningMode.DELAYED_SHORTENING): PsiElement =
getKtReferenceMutateService().bindToElement(this, element, shorteningMode)
abstract fun bindToElement(element: PsiElement, shorteningMode: ShorteningMode): PsiElement
abstract fun bindToFqName(
fun bindToFqName(
fqName: FqName,
shorteningMode: ShorteningMode = ShorteningMode.DELAYED_SHORTENING,
targetElement: PsiElement? = null
): PsiElement
): PsiElement =
getKtReferenceMutateService().bindToFqName(this, fqName, shorteningMode, targetElement)
override fun getCanonicalText(): String = expression.text