[kotlin] Properly retarget unary operators

#KTIJ-30032 Fixed

GitOrigin-RevId: 154fa9b03ed09f9e6fdbada465217a9764934be3
This commit is contained in:
Bart van Helvert
2024-06-25 18:33:41 +00:00
committed by intellij-monorepo-bot
parent 346ef0d213
commit dd875ac5ac
15 changed files with 81 additions and 1 deletions
@@ -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)
@@ -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 <caret>!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
@@ -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 <caret>!x
}
@@ -0,0 +1,3 @@
package foo
fun doFoo(): Boolean { return false }
@@ -0,0 +1,7 @@
package otherBar
import foo.doFoo
fun fooBar() {
if (!doFoo()) return
}
@@ -0,0 +1,7 @@
package bar
import foo.doFoo
fun foo<caret>Bar() {
if (!doFoo()) return
}
@@ -0,0 +1,3 @@
package foo
fun doFoo(): Boolean { return false }
@@ -0,0 +1,7 @@
{
"mainFile": "bar/UseSite.kt",
"type": "MOVE_KOTLIN_TOP_LEVEL_DECLARATIONS",
"targetPackage": "otherBar",
"enabledInK1": "true",
"enabledInK2": "true"
}
@@ -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 {
@@ -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");
}
}
@@ -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");