mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[kotlin] optimize KotlinAwareJavaGetterRenameProcessor
* trigger only in case of possible getter * restrict setters scope to kotlin files * search only relevant setters ^KTIJ-21046 Fixed GitOrigin-RevId: 55bf3f0478c13e6774f0fb7a4805351cd366caea
This commit is contained in:
committed by
intellij-monorepo-bot
parent
1e848d3d7d
commit
2c3f4d8162
+29
-16
@@ -5,41 +5,54 @@ package org.jetbrains.kotlin.idea.refactoring.rename
|
||||
import com.intellij.psi.PsiElement
|
||||
import com.intellij.psi.PsiMethod
|
||||
import com.intellij.psi.PsiReference
|
||||
import com.intellij.psi.PsiType
|
||||
import com.intellij.psi.search.SearchScope
|
||||
import com.intellij.refactoring.rename.RenameJavaMethodProcessor
|
||||
import org.jetbrains.kotlin.asJava.elements.KtLightMethod
|
||||
import org.jetbrains.kotlin.idea.references.SyntheticPropertyAccessorReference
|
||||
import org.jetbrains.kotlin.idea.references.SyntheticPropertyAccessorReferenceDescriptorImpl
|
||||
import org.jetbrains.kotlin.idea.search.canHaveSyntheticGetter
|
||||
import org.jetbrains.kotlin.idea.search.canHaveSyntheticSetter
|
||||
import org.jetbrains.kotlin.idea.search.restrictToKotlinSources
|
||||
import org.jetbrains.kotlin.idea.search.syntheticGetter
|
||||
import org.jetbrains.kotlin.load.java.JvmAbi
|
||||
import org.jetbrains.kotlin.utils.addToStdlib.cast
|
||||
import org.jetbrains.kotlin.utils.addToStdlib.filterIsInstanceWithChecker
|
||||
|
||||
class KotlinAwareJavaGetterRenameProcessor : RenameJavaMethodProcessor() {
|
||||
override fun canProcessElement(element: PsiElement) =
|
||||
super.canProcessElement(element) && element !is KtLightMethod && element is PsiMethod && JvmAbi.isGetterName(element.name)
|
||||
super.canProcessElement(element) && element !is KtLightMethod && element.cast<PsiMethod>().canHaveSyntheticGetter
|
||||
|
||||
override fun findReferences(
|
||||
element: PsiElement,
|
||||
searchScope: SearchScope,
|
||||
searchInCommentsAndStrings: Boolean
|
||||
): Collection<PsiReference> {
|
||||
val getterReferences = super.findReferences(element, searchScope, searchInCommentsAndStrings)
|
||||
val getter = element as? PsiMethod ?: return getterReferences
|
||||
val propertyName = getter.syntheticGetter ?: return getterReferences
|
||||
val getters = super.findReferences(element, searchScope, searchInCommentsAndStrings)
|
||||
val setters = findSetterReferences(element, searchScope, searchInCommentsAndStrings).orEmpty()
|
||||
return getters + setters
|
||||
}
|
||||
|
||||
private fun findSetterReferences(
|
||||
element: PsiElement,
|
||||
searchScope: SearchScope,
|
||||
searchInCommentsAndStrings: Boolean
|
||||
): Collection<PsiReference>? {
|
||||
val getter = element as? PsiMethod ?: return null
|
||||
val propertyName = getter.syntheticGetter ?: return null
|
||||
val containingClass = getter.containingClass ?: return null
|
||||
val setterName = JvmAbi.setterName(propertyName.asString())
|
||||
val containingClass = getter.containingClass ?: return getterReferences
|
||||
val setterReferences = containingClass
|
||||
.findMethodsByName(setterName, true)
|
||||
.filter { it.parameters.size == 1 && it.returnType == PsiType.VOID }
|
||||
val restrictedToKotlinScope by lazy { searchScope.restrictToKotlinSources() }
|
||||
return containingClass
|
||||
.findMethodsByName(setterName, false)
|
||||
.filter { it.canHaveSyntheticSetter }
|
||||
.asSequence()
|
||||
.flatMap {
|
||||
super.findReferences(it, searchScope, searchInCommentsAndStrings)
|
||||
super.findReferences(it, restrictedToKotlinScope, searchInCommentsAndStrings)
|
||||
.filterIsInstanceWithChecker<SyntheticPropertyAccessorReference> { accessor -> !accessor.getter }
|
||||
}
|
||||
.ifEmpty { return getterReferences }
|
||||
return ArrayList<PsiReference>(getterReferences.size + setterReferences.size).apply {
|
||||
addAll(getterReferences)
|
||||
setterReferences.mapTo(this) { SyntheticPropertyAccessorReferenceDescriptorImpl(it.expression, getter = true) }
|
||||
}
|
||||
.map {
|
||||
SyntheticPropertyAccessorReferenceDescriptorImpl(it.expression, getter = true)
|
||||
}
|
||||
.toList()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user