Spring Support: Replace accessor name with property name when processing usages in Kotlin files
#KT-11880 Fixed
This commit is contained in:
@@ -24,9 +24,6 @@ import com.intellij.psi.scope.PsiScopeProcessor
|
||||
import com.intellij.psi.util.*
|
||||
import com.intellij.util.IncorrectOperationException
|
||||
import org.jetbrains.kotlin.idea.KotlinLanguage
|
||||
import org.jetbrains.kotlin.load.java.propertyNameByGetMethodName
|
||||
import org.jetbrains.kotlin.load.java.propertyNameBySetMethodName
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.resolve.jvm.diagnostics.JvmDeclarationOriginKind
|
||||
|
||||
@@ -98,19 +95,7 @@ sealed class KtLightMethodImpl(
|
||||
|
||||
override fun setName(name: String): PsiElement? {
|
||||
val toRename = kotlinOrigin as? PsiNamedElement ?: throwCanNotModify()
|
||||
|
||||
val newName = if (toRename is KtProperty || toRename is KtParameter) {
|
||||
val methodName = Name.guessByFirstCharacter(name)
|
||||
val propertyName = toRename.name ?: ""
|
||||
when {
|
||||
name.startsWith("get") -> propertyNameByGetMethodName(methodName)
|
||||
name.startsWith("set") -> propertyNameBySetMethodName(methodName, propertyName.startsWith("is"))
|
||||
else -> null
|
||||
}?.asString() ?: name
|
||||
}
|
||||
else name
|
||||
|
||||
toRename.setName(newName)
|
||||
toRename.setName(propertyNameByAccessor(name, this) ?: name)
|
||||
return this
|
||||
}
|
||||
|
||||
|
||||
@@ -19,6 +19,8 @@ package org.jetbrains.kotlin.asJava
|
||||
import com.intellij.psi.*
|
||||
import com.intellij.psi.search.GlobalSearchScope
|
||||
import org.jetbrains.kotlin.load.java.JvmAbi
|
||||
import org.jetbrains.kotlin.load.java.propertyNameByGetMethodName
|
||||
import org.jetbrains.kotlin.load.java.propertyNameBySetMethodName
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
@@ -135,3 +137,16 @@ fun KtAnnotationEntry.toLightAnnotation(): PsiAnnotation? {
|
||||
val lightElement = ktDeclaration.toLightElements().firstOrNull() as? PsiModifierListOwner ?: return null
|
||||
return lightElement.modifierList?.annotations?.firstOrNull { it is KtLightAnnotation && it.kotlinOrigin == this }
|
||||
}
|
||||
|
||||
fun propertyNameByAccessor(name: String, accessor: KtLightMethod): String? {
|
||||
val toRename = accessor.kotlinOrigin ?: return null
|
||||
if (toRename !is KtProperty && toRename !is KtParameter) return null
|
||||
|
||||
val methodName = Name.guessByFirstCharacter(name)
|
||||
val propertyName = toRename.name ?: ""
|
||||
return when {
|
||||
name.startsWith("get") -> propertyNameByGetMethodName(methodName)
|
||||
name.startsWith("set") -> propertyNameBySetMethodName(methodName, propertyName.startsWith("is"))
|
||||
else -> null
|
||||
}?.asString()
|
||||
}
|
||||
+17
-2
@@ -29,12 +29,15 @@ import com.intellij.refactoring.listeners.RefactoringElementListener
|
||||
import com.intellij.refactoring.rename.RenameProcessor
|
||||
import com.intellij.refactoring.util.RefactoringUtil
|
||||
import com.intellij.usageView.UsageInfo
|
||||
import org.jetbrains.kotlin.asJava.KtLightMethod
|
||||
import org.jetbrains.kotlin.asJava.LightClassUtil
|
||||
import org.jetbrains.kotlin.asJava.namedUnwrappedElement
|
||||
import org.jetbrains.kotlin.asJava.propertyNameByAccessor
|
||||
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.idea.refactoring.dropOverrideKeywordIfNecessary
|
||||
import org.jetbrains.kotlin.idea.references.KtSimpleNameReference
|
||||
import org.jetbrains.kotlin.idea.util.application.runReadAction
|
||||
import org.jetbrains.kotlin.lexer.KtTokens
|
||||
import org.jetbrains.kotlin.load.java.JvmAbi
|
||||
@@ -102,7 +105,19 @@ class RenameKotlinPropertyProcessor : RenameKotlinPsiProcessor() {
|
||||
SETTER_USAGE
|
||||
}
|
||||
|
||||
override fun renameElement(element: PsiElement?, newName: String?, usages: Array<out UsageInfo>, listener: RefactoringElementListener?) {
|
||||
override tailrec fun renameElement(element: PsiElement, newName: String, usages: Array<out UsageInfo>, listener: RefactoringElementListener?) {
|
||||
if (element is KtLightMethod) {
|
||||
val origin = element.kotlinOrigin
|
||||
val newPropertyName = propertyNameByAccessor(newName, element)
|
||||
// Kotlin references to Kotlin property should not use accessor name
|
||||
if (newPropertyName != null && (origin is KtProperty || origin is KtParameter)) {
|
||||
val (ktUsages, otherUsages) = usages.partition { it.reference is KtSimpleNameReference }
|
||||
super.renameElement(element, newName, otherUsages.toTypedArray(), listener)
|
||||
renameElement(origin, newPropertyName, ktUsages.toTypedArray(), listener)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if (element !is KtProperty && element !is KtParameter) {
|
||||
super.renameElement(element, newName, usages, listener)
|
||||
return
|
||||
@@ -126,7 +141,7 @@ class RenameKotlinPropertyProcessor : RenameKotlinPsiProcessor() {
|
||||
}
|
||||
}
|
||||
|
||||
super.renameElement(element, JvmAbi.setterName(newName!!),
|
||||
super.renameElement(element, JvmAbi.setterName(newName),
|
||||
refKindUsages[UsageKind.SETTER_USAGE]?.toTypedArray() ?: arrayOf<UsageInfo>(),
|
||||
null)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user