Rename: Search text occurrences by declaration FQ name

#KT-17949 Fixed
 #KT-15932 Fixed
This commit is contained in:
Alexey Sedunov
2017-09-27 18:11:52 +03:00
parent e68f8d6d45
commit e2fd9a71df
14 changed files with 83 additions and 17 deletions
@@ -21,12 +21,12 @@ import com.intellij.psi.ElementDescriptionProvider
import com.intellij.psi.PsiElement
import com.intellij.refactoring.util.NonCodeSearchDescriptionLocation
import org.jetbrains.kotlin.asJava.namedUnwrappedElement
import org.jetbrains.kotlin.psi.KtClassOrObject
import org.jetbrains.kotlin.psi.KtNamedDeclaration
class KotlinNonCodeSearchElementDescriptionProvider : ElementDescriptionProvider {
override fun getElementDescription(element: PsiElement, location: ElementDescriptionLocation): String? {
if (location !is NonCodeSearchDescriptionLocation) return null
val declaration = element.namedUnwrappedElement as? KtClassOrObject ?: return null
val declaration = element.namedUnwrappedElement as? KtNamedDeclaration ?: return null
return if (location.isNonJava) (declaration.fqName?.asString() ?: declaration.name) else declaration.name
}
}
@@ -79,17 +79,6 @@ class RenameKotlinClassProcessor : RenameKotlinPsiProcessor() {
}
}
override fun getQualifiedNameAfterRename(element: PsiElement, newName: String?, nonJava: Boolean): String? {
if (!nonJava) return newName
val qualifiedName = when (element) {
is KtClassOrObject -> element.fqName?.asString() ?: element.name
is PsiClass -> element.qualifiedName ?: element.name
else -> return null
}
return PsiUtilCore.getQualifiedNameAfterRename(qualifiedName, newName)
}
override fun findReferences(element: PsiElement): Collection<PsiReference> {
if (element is KtObjectDeclaration && element.isCompanion()) {
return super.findReferences(element).filter { !it.isCompanionObjectClassReference() }
@@ -17,13 +17,11 @@
package org.jetbrains.kotlin.idea.refactoring.rename
import com.intellij.openapi.util.Key
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiImportStaticStatement
import com.intellij.psi.PsiPolyVariantReference
import com.intellij.psi.PsiReference
import com.intellij.psi.*
import com.intellij.psi.search.SearchScope
import com.intellij.psi.search.searches.MethodReferencesSearch
import com.intellij.psi.search.searches.ReferencesSearch
import com.intellij.psi.util.PsiUtilCore
import com.intellij.refactoring.listeners.RefactoringElementListener
import com.intellij.refactoring.rename.RenamePsiElementProcessor
import com.intellij.usageView.UsageInfo
@@ -60,6 +58,17 @@ abstract class RenameKotlinPsiProcessor : RenamePsiElementProcessor() {
return references
}
override fun getQualifiedNameAfterRename(element: PsiElement, newName: String?, nonJava: Boolean): String? {
if (!nonJava) return newName
val qualifiedName = when (element) {
is KtNamedDeclaration -> element.fqName?.asString() ?: element.name
is PsiClass -> element.qualifiedName ?: element.name
else -> return null
}
return PsiUtilCore.getQualifiedNameAfterRename(qualifiedName, newName)
}
override fun prepareRenaming(element: PsiElement, newName: String?, allRenames: MutableMap<PsiElement, String>, scope: SearchScope) {
if (newName == null) return
@@ -0,0 +1,9 @@
package test
class A {
fun baz() {}
fun bar() {
baz()
}
}
@@ -0,0 +1,2 @@
foo
test.A.baz
@@ -0,0 +1,9 @@
package test
class A {
fun /*rename*/foo() {}
fun bar() {
foo()
}
}
@@ -0,0 +1,2 @@
foo
test.A.foo
@@ -0,0 +1,6 @@
{
"type": "MARKED_ELEMENT",
"mainFile": "test.kt",
"newName": "baz",
"searchInTextOccurrences": "true"
}
@@ -0,0 +1,9 @@
package test
class A {
val baz = 1
fun bar() {
baz
}
}
@@ -0,0 +1,2 @@
foo
test.A.baz
@@ -0,0 +1,9 @@
package test
class A {
val /*rename*/foo = 1
fun bar() {
foo
}
}
@@ -0,0 +1,2 @@
foo
test.A.foo
@@ -0,0 +1,6 @@
{
"type": "MARKED_ELEMENT",
"mainFile": "test.kt",
"newName": "baz",
"searchInTextOccurrences": "true"
}
@@ -198,6 +198,12 @@ public class RenameTestGenerated extends AbstractRenameTest {
doTest(fileName);
}
@TestMetadata("funTextOccurrences/funTextOccurrences.test")
public void testFunTextOccurrences_FunTextOccurrences() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/rename/funTextOccurrences/funTextOccurrences.test");
doTest(fileName);
}
@TestMetadata("funWithLabeledReturns/funWithLabeledReturns.test")
public void testFunWithLabeledReturns_FunWithLabeledReturns() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/rename/funWithLabeledReturns/funWithLabeledReturns.test");
@@ -1302,6 +1308,12 @@ public class RenameTestGenerated extends AbstractRenameTest {
doTest(fileName);
}
@TestMetadata("valTextOccurrences/valTextOccurrences.test")
public void testValTextOccurrences_ValTextOccurrences() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/rename/valTextOccurrences/valTextOccurrences.test");
doTest(fileName);
}
@TestMetadata("varInDoWhile/varInDoWhile.test")
public void testVarInDoWhile_VarInDoWhile() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/rename/varInDoWhile/varInDoWhile.test");