Automatic renaming overloads in class or object.
#KT-4642 fixed
This commit is contained in:
+24
-12
@@ -28,22 +28,14 @@ import com.intellij.refactoring.rename.naming.AutomaticRenamerFactory
|
||||
import com.intellij.usageView.UsageInfo
|
||||
import org.jetbrains.kotlin.idea.stubindex.JetTopLevelFunctionByPackageIndex
|
||||
import org.jetbrains.kotlin.idea.stubindex.JetTopLevelFunctionFqnNameIndex
|
||||
import org.jetbrains.kotlin.psi.JetClassBody
|
||||
import org.jetbrains.kotlin.psi.JetClassOrObject
|
||||
import org.jetbrains.kotlin.psi.JetFile
|
||||
import org.jetbrains.kotlin.psi.JetNamedFunction
|
||||
|
||||
public class AutomaticOverloadsRenamer(function: JetNamedFunction, newName: String) : AutomaticRenamer() {
|
||||
init {
|
||||
val project = function.getProject()
|
||||
val module = ProjectFileIndex.SERVICE.getInstance(project).getModuleForFile(function.getContainingFile().getVirtualFile())
|
||||
if (module != null) {
|
||||
val searchScope = GlobalSearchScope.moduleScope(module)
|
||||
val overloads = JetTopLevelFunctionFqnNameIndex.getInstance().get(function.getFqName()!!.asString(), project, searchScope)
|
||||
for (overload in overloads) {
|
||||
if (overload != function) {
|
||||
myElements.add(overload)
|
||||
}
|
||||
}
|
||||
}
|
||||
myElements.addAll(function.getOverloads().filter { it != function })
|
||||
suggestAllNames(function.getName(), newName)
|
||||
}
|
||||
|
||||
@@ -53,10 +45,30 @@ public class AutomaticOverloadsRenamer(function: JetNamedFunction, newName: Stri
|
||||
override fun isSelectedByDefault(): Boolean = true
|
||||
}
|
||||
|
||||
private fun JetNamedFunction.getOverloads(): Collection<JetNamedFunction> {
|
||||
val parent = getParent()
|
||||
when (parent) {
|
||||
is JetFile -> {
|
||||
val module = ModuleUtilCore.findModuleForPsiElement(this)
|
||||
if (module != null) {
|
||||
val searchScope = GlobalSearchScope.moduleScope(module)
|
||||
val fqName = getFqName()
|
||||
if (fqName != null) {
|
||||
return JetTopLevelFunctionFqnNameIndex.getInstance().get(fqName.asString(), getProject(), searchScope)
|
||||
}
|
||||
}
|
||||
}
|
||||
is JetClassBody -> {
|
||||
return parent.getDeclarations().filterIsInstance<JetNamedFunction>().filter { it.getName() == this.getName() }
|
||||
}
|
||||
}
|
||||
return emptyList()
|
||||
}
|
||||
|
||||
public class AutomaticOverloadsRenamerFactory : AutomaticRenamerFactory {
|
||||
override fun isApplicable(element: PsiElement): Boolean {
|
||||
return element is JetNamedFunction && element.getName() != null && element.getParent() is JetFile
|
||||
return element is JetNamedFunction && element.getName() != null
|
||||
&& (element.getParent() is JetFile || element.getParent() is JetClassBody)
|
||||
}
|
||||
|
||||
override fun getOptionName() = RefactoringBundle.message("rename.overloads")
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
package testing;
|
||||
|
||||
class JavaSub extends Klass {
|
||||
void bar(int a) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package testing
|
||||
|
||||
open class Klass {
|
||||
fun bar() {
|
||||
"".bar()
|
||||
}
|
||||
|
||||
open fun bar(a: Int) {
|
||||
}
|
||||
|
||||
fun String.bar() {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class Sub : Klass() {
|
||||
override fun bar(a: Int) {
|
||||
}
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
Klass().bar()
|
||||
Klass().bar(1)
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package testing;
|
||||
|
||||
class JavaSub extends Klass {
|
||||
void foo(int a) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package testing
|
||||
|
||||
open class Klass {
|
||||
fun foo() {
|
||||
"".foo()
|
||||
}
|
||||
|
||||
open fun foo(a: Int) {
|
||||
}
|
||||
|
||||
fun String.foo() {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class Sub : Klass() {
|
||||
override fun foo(a: Int) {
|
||||
}
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
Klass().foo()
|
||||
Klass().foo(1)
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"type": "KOTLIN_FUNCTION",
|
||||
"classId": "testing/Klass",
|
||||
"oldName": "foo",
|
||||
"newName": "bar",
|
||||
"mainFile": "Klass.kt"
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
class JavaSuper {
|
||||
void bar(int a) {
|
||||
}
|
||||
|
||||
void bar() {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
class Sub : JavaSuper() {
|
||||
override fun bar(a: Int) {
|
||||
}
|
||||
|
||||
override fun bar() {
|
||||
}
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
JavaSuper().bar()
|
||||
JavaSuper().bar(1)
|
||||
Sub().bar()
|
||||
Sub().bar(1)
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
class JavaSuper {
|
||||
void foo(int a) {
|
||||
}
|
||||
|
||||
void foo() {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
class Sub : JavaSuper() {
|
||||
override fun foo(a: Int) {
|
||||
}
|
||||
|
||||
override fun foo() {
|
||||
}
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
JavaSuper().foo()
|
||||
JavaSuper().foo(1)
|
||||
Sub().foo()
|
||||
Sub().foo(1)
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"type": "JAVA_METHOD",
|
||||
"classId": "/JavaSuper",
|
||||
"methodSignature": "void foo()",
|
||||
"newName": "bar",
|
||||
"mainFile": "JavaSuper.java"
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package testing
|
||||
|
||||
object Object {
|
||||
fun bar() {
|
||||
"".bar()
|
||||
}
|
||||
|
||||
fun bar(a: Int) {
|
||||
}
|
||||
|
||||
fun String.bar() {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
Object.bar()
|
||||
Object.bar(1)
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package testing
|
||||
|
||||
object Object {
|
||||
fun foo() {
|
||||
"".foo()
|
||||
}
|
||||
|
||||
fun foo(a: Int) {
|
||||
}
|
||||
|
||||
fun String.foo() {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
Object.foo()
|
||||
Object.foo(1)
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"type": "KOTLIN_FUNCTION",
|
||||
"classId": "testing/Object",
|
||||
"oldName": "foo",
|
||||
"newName": "bar",
|
||||
"mainFile": "Object.kt"
|
||||
}
|
||||
@@ -53,6 +53,24 @@ public class RenameTestGenerated extends AbstractRenameTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("automaticRenamerOverloadsClass/class.test")
|
||||
public void testAutomaticRenamerOverloadsClass_Class() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/rename/automaticRenamerOverloadsClass/class.test");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("automaticRenamerOverloadsJavaClass/overloads.test")
|
||||
public void testAutomaticRenamerOverloadsJavaClass_Overloads() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/rename/automaticRenamerOverloadsJavaClass/overloads.test");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("automaticRenamerOverloadsObject/object.test")
|
||||
public void testAutomaticRenamerOverloadsObject_Object() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/rename/automaticRenamerOverloadsObject/object.test");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("defaultObject/defaultObject.test")
|
||||
public void testDefaultObject_DefaultObject() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/rename/defaultObject/defaultObject.test");
|
||||
|
||||
Reference in New Issue
Block a user