Skip to content

Commit 6b18ff1

Browse files
committed
Copy/Move: Fix processing of calls used as callees
#KT-18241 Fixed
1 parent 87a4129 commit 6b18ff1

File tree

7 files changed

+63
-2
lines changed

7 files changed

+63
-2
lines changed

idea/idea-analysis/src/org/jetbrains/kotlin/idea/references/KtSimpleNameReference.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import org.jetbrains.kotlin.idea.codeInsight.shorten.addToShorteningWaitSet
2929
import org.jetbrains.kotlin.idea.core.ShortenReferences
3030
import org.jetbrains.kotlin.idea.core.copied
3131
import org.jetbrains.kotlin.idea.core.quoteIfNeeded
32+
import org.jetbrains.kotlin.idea.core.replaced
3233
import org.jetbrains.kotlin.idea.intentions.OperatorToFunctionIntention
3334
import org.jetbrains.kotlin.idea.refactoring.fqName.getKotlinFqName
3435
import org.jetbrains.kotlin.lexer.KtToken
@@ -206,7 +207,7 @@ class KtSimpleNameReference(expression: KtSimpleNameExpression) : KtSimpleRefere
206207
val typeText = "$text${elementToReplace.typeArgumentList?.text ?: ""}"
207208
elementToReplace.replace(psiFactory.createType(typeText).typeElement!!)
208209
}
209-
else -> elementToReplace.replace(psiFactory.createExpression(text))
210+
else -> KtPsiUtil.safeDeparenthesize(elementToReplace.replaced(psiFactory.createExpression(text)))
210211
} as KtElement
211212

212213
val selector = (newElement as? KtCallableReferenceExpression)?.callableReference

idea/idea-core/src/org/jetbrains/kotlin/idea/core/ShortenReferences.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,11 @@ class ShortenReferences(val options: (KtElement) -> Options = { Options.DEFAULT
562562
}
563563

564564
override fun shortenElement(element: KtDotQualifiedExpression): KtElement {
565-
return element.replace(element.selectorExpression!!) as KtElement
565+
val parens = element.parent as? KtParenthesizedExpression
566+
val requiredParens = parens != null && !KtPsiUtil.areParenthesesUseless(parens)
567+
val shortenedElement = element.replace(element.selectorExpression!!) as KtElement
568+
if (requiredParens) return shortenedElement.parent.replaced(shortenedElement)
569+
return shortenedElement
566570
}
567571
}
568572

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package bar
2+
3+
import foo.parenB
4+
import foo.parenBP
5+
import foo.parenP
6+
import foo.parenPB
7+
import foo.parenPP
8+
9+
fun some2(p1: () -> Unit, p2: (() -> Unit) -> Unit) {
10+
parenB { p1() }
11+
parenP()
12+
parenBP { p1 }()
13+
parenPP()()
14+
parenPB(p2) {}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package foo
2+
3+
fun parenB(p: () -> Unit) = p()
4+
fun parenP() {}
5+
fun parenBP(p: () -> () -> Unit): () -> Unit = p()
6+
fun parenPP(): () -> Unit = {}
7+
fun parenPB(p: (() -> Unit) -> Unit): (() -> Unit) -> Unit = p
8+
9+
fun some(p1: () -> Unit, p2: (() -> Unit) -> Unit) {
10+
parenB { p1() }
11+
parenP()
12+
parenBP { p1 }()
13+
parenPP()()
14+
(parenPB(p2)) {}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package foo
2+
3+
fun parenB(p: () -> Unit) = p()
4+
fun parenP() {}
5+
fun parenBP(p: () -> () -> Unit): () -> Unit = p()
6+
fun parenPP(): () -> Unit = {}
7+
fun parenPB(p: (() -> Unit) -> Unit): (() -> Unit) -> Unit = p
8+
9+
fun <caret>some(p1: () -> Unit, p2: (() -> Unit) -> Unit) {
10+
parenB { p1() }
11+
parenP()
12+
parenBP { p1 }()
13+
parenPP()()
14+
(parenPB(p2)) {}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"mainFile": "foo/test.kt",
3+
"targetPackage": "bar",
4+
"newName": "some2"
5+
}

idea/tests/org/jetbrains/kotlin/idea/refactoring/copy/CopyTestGenerated.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ public void testCopyClassWithRename_CopyClassWithRename() throws Exception {
7272
doTest(fileName);
7373
}
7474

75+
@TestMetadata("copyFunCallQualificationWithParentheses/copyFunCallQualificationWithParentheses.test")
76+
public void testCopyFunCallQualificationWithParentheses_CopyFunCallQualificationWithParentheses() throws Exception {
77+
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/copy/copyFunCallQualificationWithParentheses/copyFunCallQualificationWithParentheses.test");
78+
doTest(fileName);
79+
}
80+
7581
@TestMetadata("copyLocalClass/copyLocalClass.test")
7682
public void testCopyLocalClass_CopyLocalClass() throws Exception {
7783
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/copy/copyLocalClass/copyLocalClass.test");

0 commit comments

Comments
 (0)