mirror of
https://github.com/jlengrand/kotlin.git
synced 2026-04-17 15:52:30 +00:00
Rename: Do not use Change Signature for parameter renaming
This commit is contained in:
@@ -17,54 +17,36 @@
|
||||
package org.jetbrains.kotlin.idea.refactoring.rename
|
||||
|
||||
import com.intellij.psi.PsiElement
|
||||
import com.intellij.psi.PsiMethod
|
||||
import com.intellij.psi.PsiNamedElement
|
||||
import com.intellij.psi.search.SearchScope
|
||||
import com.intellij.refactoring.listeners.RefactoringElementListener
|
||||
import com.intellij.usageView.UsageInfo
|
||||
import com.intellij.util.xml.impl.GenericDomValueReference
|
||||
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptor
|
||||
import org.jetbrains.kotlin.idea.refactoring.changeSignature.KotlinChangeSignatureConfiguration
|
||||
import org.jetbrains.kotlin.idea.refactoring.changeSignature.KotlinMethodDescriptor
|
||||
import org.jetbrains.kotlin.idea.refactoring.changeSignature.modify
|
||||
import org.jetbrains.kotlin.idea.refactoring.changeSignature.runChangeSignature
|
||||
import org.jetbrains.kotlin.idea.refactoring.getAffectedCallables
|
||||
import org.jetbrains.kotlin.psi.KtCallableDeclaration
|
||||
import org.jetbrains.kotlin.psi.KtNamedFunction
|
||||
import org.jetbrains.kotlin.psi.KtParameter
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getElementTextWithContext
|
||||
import org.jetbrains.kotlin.resolve.OverrideResolver
|
||||
|
||||
class RenameKotlinParameterProcessor : RenameKotlinPsiProcessor() {
|
||||
override fun canProcessElement(element: PsiElement): Boolean {
|
||||
return element is KtParameter && element.parent.parent is KtNamedFunction
|
||||
}
|
||||
override fun canProcessElement(element: PsiElement) = element is KtParameter && element.ownerFunction is KtNamedFunction
|
||||
|
||||
override fun renameElement(element: PsiElement, newName: String, usages: Array<out UsageInfo>, listener: RefactoringElementListener?) {
|
||||
// Workaround for usages in XML files
|
||||
// TODO: Do not use Change Signature for parameter rename as it's less efficient and loses some usages
|
||||
for (usage in usages) {
|
||||
(usage.reference as? GenericDomValueReference<*>)?.handleElementRename(newName)
|
||||
}
|
||||
override fun prepareRenaming(element: PsiElement, newName: String, allRenames: MutableMap<PsiElement, String>, scope: SearchScope) {
|
||||
if (element !is KtParameter) return
|
||||
|
||||
val function = (element as KtParameter).parent.parent as KtNamedFunction
|
||||
val paramIndex = function.valueParameters.indexOf(element)
|
||||
assert(paramIndex != -1, { "couldn't find parameter in parent ${element.getElementTextWithContext()}" })
|
||||
|
||||
val functionDescriptor = function.resolveToDescriptor() as? FunctionDescriptor ?: return
|
||||
val parameterDescriptor = functionDescriptor.valueParameters[paramIndex]
|
||||
|
||||
val parameterNameChangedOnOverride = parameterDescriptor.overriddenDescriptors.any {
|
||||
overriddenParameter -> OverrideResolver.shouldReportParameterNameOverrideWarning(parameterDescriptor, overriddenParameter)
|
||||
}
|
||||
|
||||
val changeSignatureConfiguration = object : KotlinChangeSignatureConfiguration {
|
||||
override fun configure(originalDescriptor: KotlinMethodDescriptor): KotlinMethodDescriptor {
|
||||
val paramInfoIndex = if (functionDescriptor.extensionReceiverParameter != null) paramIndex + 1 else paramIndex
|
||||
return originalDescriptor.modify { it.renameParameter(paramInfoIndex, newName) }
|
||||
val function = element.ownerFunction ?: return
|
||||
val originalDescriptor = function.resolveToDescriptor() as FunctionDescriptor
|
||||
val affectedCallables = getAffectedCallables(element.project, OverrideResolver.getDeepestSuperDeclarations(originalDescriptor))
|
||||
for (callable in affectedCallables) {
|
||||
val parameter: PsiNamedElement? = when (callable) {
|
||||
is KtCallableDeclaration -> callable.valueParameters.firstOrNull { it.name == element.name }
|
||||
is PsiMethod -> callable.parameterList.parameters.firstOrNull { it.name == element.name }
|
||||
else -> null
|
||||
}
|
||||
|
||||
override fun performSilently(affectedFunctions: Collection<PsiElement>) = true
|
||||
|
||||
override fun forcePerformForSelectedFunctionOnly() = parameterNameChangedOnOverride
|
||||
if (parameter == null) continue
|
||||
allRenames[parameter] = newName
|
||||
}
|
||||
|
||||
runChangeSignature(element.project, functionDescriptor, changeSignatureConfiguration, element, "Rename parameter")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user