From dd875ac5ac2eed7bc2314a24d7df5f236f8470a0 Mon Sep 17 00:00:00 2001 From: Bart van Helvert Date: Tue, 25 Jun 2024 15:11:49 +0200 Subject: [PATCH] [kotlin] Properly retarget unary operators #KTIJ-30032 Fixed GitOrigin-RevId: 154fa9b03ed09f9e6fdbada465217a9764934be3 --- .../refactoring/move/MoveTestGenerated.java | 5 +++++ .../operationReference/UnaryOperator.kt | 19 +++++++++++++++++++ .../operationReference/UnaryOperator.kt.after | 11 +++++++++++ .../unaryOperatorReference/after/bar/.keep | 0 .../after/foo/DeclSite.kt | 3 +++ .../after/otherBar/.keep | 0 .../after/otherBar/FooBar.kt | 7 +++++++ .../unaryOperatorReference/before/bar/.keep | 0 .../before/bar/UseSite.kt | 7 +++++++ .../before/foo/DeclSite.kt | 3 +++ .../before/otherBar/.keep | 0 .../unaryOperatorReference.test | 7 +++++++ .../refactoring/K2ReferenceMutateService.kt | 10 +++++++++- .../move/K2MoveTopLevelTestGenerated.java | 5 +++++ .../K2BindToElementTestGenerated.java | 5 +++++ 15 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/bindToElement/operationReference/UnaryOperator.kt create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/bindToElement/operationReference/UnaryOperator.kt.after create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/moveTopLevel/kotlin/unaryOperatorReference/after/bar/.keep create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/moveTopLevel/kotlin/unaryOperatorReference/after/foo/DeclSite.kt create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/moveTopLevel/kotlin/unaryOperatorReference/after/otherBar/.keep create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/moveTopLevel/kotlin/unaryOperatorReference/after/otherBar/FooBar.kt create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/moveTopLevel/kotlin/unaryOperatorReference/before/bar/.keep create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/moveTopLevel/kotlin/unaryOperatorReference/before/bar/UseSite.kt create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/moveTopLevel/kotlin/unaryOperatorReference/before/foo/DeclSite.kt create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/moveTopLevel/kotlin/unaryOperatorReference/before/otherBar/.keep create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/moveTopLevel/kotlin/unaryOperatorReference/unaryOperatorReference.test diff --git a/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/refactoring/move/MoveTestGenerated.java b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/refactoring/move/MoveTestGenerated.java index 2045cc9a884b..974ccb34dc36 100644 --- a/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/refactoring/move/MoveTestGenerated.java +++ b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/refactoring/move/MoveTestGenerated.java @@ -692,6 +692,11 @@ public abstract class MoveTestGenerated extends AbstractMoveTest { public void testKotlin_unaffectedQualifiedReferences_UnaffectedQualifiedReferences() throws Exception { runTest("testData/refactoring/moveTopLevel/kotlin/unaffectedQualifiedReferences/unaffectedQualifiedReferences.test"); } + + @TestMetadata("kotlin/unaryOperatorReference/unaryOperatorReference.test") + public void testKotlin_unaryOperatorReference_UnaryOperatorReference() throws Exception { + runTest("testData/refactoring/moveTopLevel/kotlin/unaryOperatorReference/unaryOperatorReference.test"); + } } @RunWith(JUnit3RunnerWithInners.class) diff --git a/plugins/kotlin/idea/tests/testData/refactoring/bindToElement/operationReference/UnaryOperator.kt b/plugins/kotlin/idea/tests/testData/refactoring/bindToElement/operationReference/UnaryOperator.kt new file mode 100644 index 000000000000..34e2966fb1b8 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/bindToElement/operationReference/UnaryOperator.kt @@ -0,0 +1,19 @@ +// FILE: test/UnaryOperator.kt +// BIND_TO test.bar.not +package test + +import test.foo.not + +fun foo(x: String): Boolean { + return !x +} + +// FILE: test/foo/Test.kt +package test.foo + +operator fun String.not() = false + +// FILE: test/bar/Test.kt +package test.bar + +operator fun String.not() = false \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/refactoring/bindToElement/operationReference/UnaryOperator.kt.after b/plugins/kotlin/idea/tests/testData/refactoring/bindToElement/operationReference/UnaryOperator.kt.after new file mode 100644 index 000000000000..7b27140e94c5 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/bindToElement/operationReference/UnaryOperator.kt.after @@ -0,0 +1,11 @@ +// FILE: test/UnaryOperator.kt +// BIND_TO test.bar.not +package test + +import test.bar.not +import test.foo.not + +fun foo(x: String): Boolean { + return !x +} + diff --git a/plugins/kotlin/idea/tests/testData/refactoring/moveTopLevel/kotlin/unaryOperatorReference/after/bar/.keep b/plugins/kotlin/idea/tests/testData/refactoring/moveTopLevel/kotlin/unaryOperatorReference/after/bar/.keep new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/plugins/kotlin/idea/tests/testData/refactoring/moveTopLevel/kotlin/unaryOperatorReference/after/foo/DeclSite.kt b/plugins/kotlin/idea/tests/testData/refactoring/moveTopLevel/kotlin/unaryOperatorReference/after/foo/DeclSite.kt new file mode 100644 index 000000000000..b6f0cc3b502e --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/moveTopLevel/kotlin/unaryOperatorReference/after/foo/DeclSite.kt @@ -0,0 +1,3 @@ +package foo + +fun doFoo(): Boolean { return false } \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/refactoring/moveTopLevel/kotlin/unaryOperatorReference/after/otherBar/.keep b/plugins/kotlin/idea/tests/testData/refactoring/moveTopLevel/kotlin/unaryOperatorReference/after/otherBar/.keep new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/plugins/kotlin/idea/tests/testData/refactoring/moveTopLevel/kotlin/unaryOperatorReference/after/otherBar/FooBar.kt b/plugins/kotlin/idea/tests/testData/refactoring/moveTopLevel/kotlin/unaryOperatorReference/after/otherBar/FooBar.kt new file mode 100644 index 000000000000..af5fe1ed0dc8 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/moveTopLevel/kotlin/unaryOperatorReference/after/otherBar/FooBar.kt @@ -0,0 +1,7 @@ +package otherBar + +import foo.doFoo + +fun fooBar() { + if (!doFoo()) return +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/refactoring/moveTopLevel/kotlin/unaryOperatorReference/before/bar/.keep b/plugins/kotlin/idea/tests/testData/refactoring/moveTopLevel/kotlin/unaryOperatorReference/before/bar/.keep new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/plugins/kotlin/idea/tests/testData/refactoring/moveTopLevel/kotlin/unaryOperatorReference/before/bar/UseSite.kt b/plugins/kotlin/idea/tests/testData/refactoring/moveTopLevel/kotlin/unaryOperatorReference/before/bar/UseSite.kt new file mode 100644 index 000000000000..b48e5fa1b2a7 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/moveTopLevel/kotlin/unaryOperatorReference/before/bar/UseSite.kt @@ -0,0 +1,7 @@ +package bar + +import foo.doFoo + +fun fooBar() { + if (!doFoo()) return +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/refactoring/moveTopLevel/kotlin/unaryOperatorReference/before/foo/DeclSite.kt b/plugins/kotlin/idea/tests/testData/refactoring/moveTopLevel/kotlin/unaryOperatorReference/before/foo/DeclSite.kt new file mode 100644 index 000000000000..b6f0cc3b502e --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/moveTopLevel/kotlin/unaryOperatorReference/before/foo/DeclSite.kt @@ -0,0 +1,3 @@ +package foo + +fun doFoo(): Boolean { return false } \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/refactoring/moveTopLevel/kotlin/unaryOperatorReference/before/otherBar/.keep b/plugins/kotlin/idea/tests/testData/refactoring/moveTopLevel/kotlin/unaryOperatorReference/before/otherBar/.keep new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/plugins/kotlin/idea/tests/testData/refactoring/moveTopLevel/kotlin/unaryOperatorReference/unaryOperatorReference.test b/plugins/kotlin/idea/tests/testData/refactoring/moveTopLevel/kotlin/unaryOperatorReference/unaryOperatorReference.test new file mode 100644 index 000000000000..6879825187ae --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/moveTopLevel/kotlin/unaryOperatorReference/unaryOperatorReference.test @@ -0,0 +1,7 @@ +{ + "mainFile": "bar/UseSite.kt", + "type": "MOVE_KOTLIN_TOP_LEVEL_DECLARATIONS", + "targetPackage": "otherBar", + "enabledInK1": "true", + "enabledInK2": "true" +} diff --git a/plugins/kotlin/refactorings/kotlin.refactorings.k2/src/org/jetbrains/kotlin/idea/k2/refactoring/K2ReferenceMutateService.kt b/plugins/kotlin/refactorings/kotlin.refactorings.k2/src/org/jetbrains/kotlin/idea/k2/refactoring/K2ReferenceMutateService.kt index 5d920579f66d..9867fe309f3a 100644 --- a/plugins/kotlin/refactorings/kotlin.refactorings.k2/src/org/jetbrains/kotlin/idea/k2/refactoring/K2ReferenceMutateService.kt +++ b/plugins/kotlin/refactorings/kotlin.refactorings.k2/src/org/jetbrains/kotlin/idea/k2/refactoring/K2ReferenceMutateService.kt @@ -228,7 +228,15 @@ internal class K2ReferenceMutateService : KtReferenceMutateServiceBase() { val isInfix = analyze(targetElement) { (targetElement.getFunctionLikeSymbol() as? KaNamedFunctionSymbol)?.isInfix == true } val isOperator = analyze(targetElement) { (targetElement.getFunctionLikeSymbol() as? KaNamedFunctionSymbol)?.isOperator == true } val replacedExpr = if (isOperator) { - replaced(psiFactory.createOperationName(OperatorNameConventions.TOKENS_BY_OPERATOR_NAME[Name.identifier(shortName)] ?: shortName)) + val identifier = Name.identifier(shortName) + val isUnary = OperatorNameConventions.UNARY_OPERATION_NAMES.contains(identifier) + val operator = OperatorNameConventions.TOKENS_BY_OPERATOR_NAME[identifier] ?: shortName + val newOperator = if (isUnary) { + (psiFactory.createExpression("${operator}0") as KtUnaryExpression).operationReference + } else { + psiFactory.createOperationName(operator) + } + replaced(newOperator) } else if (isInfix) { replaced(psiFactory.createOperationName(shortName)) } else { diff --git a/plugins/kotlin/refactorings/kotlin.refactorings.move.k2/test/org/jetbrains/kotlin/idea/k2/refactoring/move/K2MoveTopLevelTestGenerated.java b/plugins/kotlin/refactorings/kotlin.refactorings.move.k2/test/org/jetbrains/kotlin/idea/k2/refactoring/move/K2MoveTopLevelTestGenerated.java index 6ab0e3302664..45141290f401 100644 --- a/plugins/kotlin/refactorings/kotlin.refactorings.move.k2/test/org/jetbrains/kotlin/idea/k2/refactoring/move/K2MoveTopLevelTestGenerated.java +++ b/plugins/kotlin/refactorings/kotlin.refactorings.move.k2/test/org/jetbrains/kotlin/idea/k2/refactoring/move/K2MoveTopLevelTestGenerated.java @@ -449,4 +449,9 @@ public class K2MoveTopLevelTestGenerated extends AbstractK2MoveTopLevelTest { public void testKotlin_unaffectedQualifiedReferences_UnaffectedQualifiedReferences() throws Exception { runTest("../../idea/tests/testData/refactoring/moveTopLevel/kotlin/unaffectedQualifiedReferences/unaffectedQualifiedReferences.test"); } + + @TestMetadata("kotlin/unaryOperatorReference/unaryOperatorReference.test") + public void testKotlin_unaryOperatorReference_UnaryOperatorReference() throws Exception { + runTest("../../idea/tests/testData/refactoring/moveTopLevel/kotlin/unaryOperatorReference/unaryOperatorReference.test"); + } } diff --git a/plugins/kotlin/refactorings/kotlin.refactorings.tests.k2/test/org/jetbrains/kotlin/idea/k2/refactoring/bindToElement/K2BindToElementTestGenerated.java b/plugins/kotlin/refactorings/kotlin.refactorings.tests.k2/test/org/jetbrains/kotlin/idea/k2/refactoring/bindToElement/K2BindToElementTestGenerated.java index cc4660be284e..4d427df7fdfe 100644 --- a/plugins/kotlin/refactorings/kotlin.refactorings.tests.k2/test/org/jetbrains/kotlin/idea/k2/refactoring/bindToElement/K2BindToElementTestGenerated.java +++ b/plugins/kotlin/refactorings/kotlin.refactorings.tests.k2/test/org/jetbrains/kotlin/idea/k2/refactoring/bindToElement/K2BindToElementTestGenerated.java @@ -816,6 +816,11 @@ public abstract class K2BindToElementTestGenerated extends AbstractK2BindToEleme runTest("../../idea/tests/testData/refactoring/bindToElement/operationReference/UnQualified.kt"); } + @TestMetadata("UnaryOperator.kt") + public void testUnaryOperator() throws Exception { + runTest("../../idea/tests/testData/refactoring/bindToElement/operationReference/UnaryOperator.kt"); + } + @TestMetadata("WithRegularCall.kt") public void testWithRegularCall() throws Exception { runTest("../../idea/tests/testData/refactoring/bindToElement/operationReference/WithRegularCall.kt");