File tree Expand file tree Collapse file tree 7 files changed +63
-2
lines changed
idea-analysis/src/org/jetbrains/kotlin/idea/references
idea-core/src/org/jetbrains/kotlin/idea/core
testData/refactoring/copy/copyFunCallQualificationWithParentheses
tests/org/jetbrains/kotlin/idea/refactoring/copy Expand file tree Collapse file tree 7 files changed +63
-2
lines changed Original file line number Diff line number Diff line change @@ -29,6 +29,7 @@ import org.jetbrains.kotlin.idea.codeInsight.shorten.addToShorteningWaitSet
29
29
import org.jetbrains.kotlin.idea.core.ShortenReferences
30
30
import org.jetbrains.kotlin.idea.core.copied
31
31
import org.jetbrains.kotlin.idea.core.quoteIfNeeded
32
+ import org.jetbrains.kotlin.idea.core.replaced
32
33
import org.jetbrains.kotlin.idea.intentions.OperatorToFunctionIntention
33
34
import org.jetbrains.kotlin.idea.refactoring.fqName.getKotlinFqName
34
35
import org.jetbrains.kotlin.lexer.KtToken
@@ -206,7 +207,7 @@ class KtSimpleNameReference(expression: KtSimpleNameExpression) : KtSimpleRefere
206
207
val typeText = " $text${elementToReplace.typeArgumentList?.text ? : " " } "
207
208
elementToReplace.replace(psiFactory.createType(typeText).typeElement!! )
208
209
}
209
- else -> elementToReplace.replace (psiFactory.createExpression(text))
210
+ else -> KtPsiUtil .safeDeparenthesize( elementToReplace.replaced (psiFactory.createExpression(text) ))
210
211
} as KtElement
211
212
212
213
val selector = (newElement as ? KtCallableReferenceExpression )?.callableReference
Original file line number Diff line number Diff line change @@ -562,7 +562,11 @@ class ShortenReferences(val options: (KtElement) -> Options = { Options.DEFAULT
562
562
}
563
563
564
564
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
566
570
}
567
571
}
568
572
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
1
+ {
2
+ "mainFile": "foo/test.kt",
3
+ "targetPackage": "bar",
4
+ "newName": "some2"
5
+ }
Original file line number Diff line number Diff line change @@ -72,6 +72,12 @@ public void testCopyClassWithRename_CopyClassWithRename() throws Exception {
72
72
doTest (fileName );
73
73
}
74
74
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
+
75
81
@ TestMetadata ("copyLocalClass/copyLocalClass.test" )
76
82
public void testCopyLocalClass_CopyLocalClass () throws Exception {
77
83
String fileName = KotlinTestUtils .navigationMetadata ("idea/testData/refactoring/copy/copyLocalClass/copyLocalClass.test" );
You can’t perform that action at this time.
0 commit comments