Spring Support: Replace accessor name with property name when processing usages in Kotlin files

#KT-11880 Fixed
This commit is contained in:
Alexey Sedunov
2016-04-26 16:01:55 +03:00
parent 6e08f06bfd
commit 12987de156
3 changed files with 33 additions and 18 deletions
@@ -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()
}