From 40dd229021d59f86edb560851f512d982a4924a9 Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Thu, 26 Sep 2019 12:51:34 +0300 Subject: [PATCH] Join with assignment: place the result at the assignment #KT-21172 Fixed --- .../JoinDeclarationAndAssignmentIntention.kt | 29 +++++++++++++------ .../atAssignment.kt | 9 ++++++ .../atAssignment.kt.after | 8 +++++ .../atAssignmentInConstructor.kt | 9 ++++++ .../atAssignmentInConstructor.kt.after | 8 +++++ .../atAssignmentInInitializer.kt | 9 ++++++ .../atAssignmentInInitializer.kt.after | 8 +++++ .../deleteConstructorBlock.kt | 7 +++++ .../deleteConstructorBlock.kt.after | 5 ++++ .../deleteConstructorBlock2.kt | 8 +++++ .../deleteConstructorBlock2.kt.after | 6 ++++ .../singleConstructor.kt | 3 ++ .../singleConstructor.kt.after | 3 ++ .../intentions/IntentionTestGenerated.java | 25 ++++++++++++++++ 14 files changed, 128 insertions(+), 9 deletions(-) create mode 100644 idea/testData/intentions/joinDeclarationAndAssignment/atAssignment.kt create mode 100644 idea/testData/intentions/joinDeclarationAndAssignment/atAssignment.kt.after create mode 100644 idea/testData/intentions/joinDeclarationAndAssignment/atAssignmentInConstructor.kt create mode 100644 idea/testData/intentions/joinDeclarationAndAssignment/atAssignmentInConstructor.kt.after create mode 100644 idea/testData/intentions/joinDeclarationAndAssignment/atAssignmentInInitializer.kt create mode 100644 idea/testData/intentions/joinDeclarationAndAssignment/atAssignmentInInitializer.kt.after create mode 100644 idea/testData/intentions/joinDeclarationAndAssignment/deleteConstructorBlock.kt create mode 100644 idea/testData/intentions/joinDeclarationAndAssignment/deleteConstructorBlock.kt.after create mode 100644 idea/testData/intentions/joinDeclarationAndAssignment/deleteConstructorBlock2.kt create mode 100644 idea/testData/intentions/joinDeclarationAndAssignment/deleteConstructorBlock2.kt.after diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/JoinDeclarationAndAssignmentIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/JoinDeclarationAndAssignmentIntention.kt index 89ee1752ae1..68a932d3bee 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/JoinDeclarationAndAssignmentIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/JoinDeclarationAndAssignmentIntention.kt @@ -25,6 +25,7 @@ import com.intellij.psi.util.PsiTreeUtil import org.jetbrains.kotlin.idea.caches.resolve.analyze import org.jetbrains.kotlin.idea.core.canOmitDeclaredType import org.jetbrains.kotlin.idea.core.moveCaret +import org.jetbrains.kotlin.idea.core.replaced import org.jetbrains.kotlin.idea.core.unblockDocument import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection import org.jetbrains.kotlin.lexer.KtTokens @@ -76,25 +77,35 @@ class JoinDeclarationAndAssignmentIntention : SelfTargetingRangeIntention + repo.git("add --verbose .") + hash = repo.git("commit -m $commitMessage") +} + +class Repository { + fun git(s: String) = "" +} \ No newline at end of file diff --git a/idea/testData/intentions/joinDeclarationAndAssignment/atAssignment.kt.after b/idea/testData/intentions/joinDeclarationAndAssignment/atAssignment.kt.after new file mode 100644 index 00000000000..fecfed1345d --- /dev/null +++ b/idea/testData/intentions/joinDeclarationAndAssignment/atAssignment.kt.after @@ -0,0 +1,8 @@ +fun test(repo: Repository, commitMessage: String) { + repo.git("add --verbose .") + val hash: String = repo.git("commit -m $commitMessage") +} + +class Repository { + fun git(s: String) = "" +} \ No newline at end of file diff --git a/idea/testData/intentions/joinDeclarationAndAssignment/atAssignmentInConstructor.kt b/idea/testData/intentions/joinDeclarationAndAssignment/atAssignmentInConstructor.kt new file mode 100644 index 00000000000..c13ad740514 --- /dev/null +++ b/idea/testData/intentions/joinDeclarationAndAssignment/atAssignmentInConstructor.kt @@ -0,0 +1,9 @@ +class A { + constructor() { + val foo: String + bar() + foo = "" + } + + fun bar() {} +} diff --git a/idea/testData/intentions/joinDeclarationAndAssignment/atAssignmentInConstructor.kt.after b/idea/testData/intentions/joinDeclarationAndAssignment/atAssignmentInConstructor.kt.after new file mode 100644 index 00000000000..031fde7a473 --- /dev/null +++ b/idea/testData/intentions/joinDeclarationAndAssignment/atAssignmentInConstructor.kt.after @@ -0,0 +1,8 @@ +class A { + constructor() { + bar() + val foo: String = "" + } + + fun bar() {} +} diff --git a/idea/testData/intentions/joinDeclarationAndAssignment/atAssignmentInInitializer.kt b/idea/testData/intentions/joinDeclarationAndAssignment/atAssignmentInInitializer.kt new file mode 100644 index 00000000000..84aea7bef1b --- /dev/null +++ b/idea/testData/intentions/joinDeclarationAndAssignment/atAssignmentInInitializer.kt @@ -0,0 +1,9 @@ +class A { + init { + val foo: String + bar() + foo = "" + } + + fun bar() {} +} \ No newline at end of file diff --git a/idea/testData/intentions/joinDeclarationAndAssignment/atAssignmentInInitializer.kt.after b/idea/testData/intentions/joinDeclarationAndAssignment/atAssignmentInInitializer.kt.after new file mode 100644 index 00000000000..cc38ae9b1ed --- /dev/null +++ b/idea/testData/intentions/joinDeclarationAndAssignment/atAssignmentInInitializer.kt.after @@ -0,0 +1,8 @@ +class A { + init { + bar() + val foo: String = "" + } + + fun bar() {} +} \ No newline at end of file diff --git a/idea/testData/intentions/joinDeclarationAndAssignment/deleteConstructorBlock.kt b/idea/testData/intentions/joinDeclarationAndAssignment/deleteConstructorBlock.kt new file mode 100644 index 00000000000..a4ae360a42e --- /dev/null +++ b/idea/testData/intentions/joinDeclarationAndAssignment/deleteConstructorBlock.kt @@ -0,0 +1,7 @@ +class A { + constructor() { + a = 1 + } + + val a: Int +} \ No newline at end of file diff --git a/idea/testData/intentions/joinDeclarationAndAssignment/deleteConstructorBlock.kt.after b/idea/testData/intentions/joinDeclarationAndAssignment/deleteConstructorBlock.kt.after new file mode 100644 index 00000000000..5f2e2a56c98 --- /dev/null +++ b/idea/testData/intentions/joinDeclarationAndAssignment/deleteConstructorBlock.kt.after @@ -0,0 +1,5 @@ +class A { + constructor() + + val a: Int = 1 +} \ No newline at end of file diff --git a/idea/testData/intentions/joinDeclarationAndAssignment/deleteConstructorBlock2.kt b/idea/testData/intentions/joinDeclarationAndAssignment/deleteConstructorBlock2.kt new file mode 100644 index 00000000000..b63ec8a2768 --- /dev/null +++ b/idea/testData/intentions/joinDeclarationAndAssignment/deleteConstructorBlock2.kt @@ -0,0 +1,8 @@ +// DISABLE-ERRORS +class A(i: Int, j: Int) { + constructor(i: Int) : this(i, 2) { + a = 1 + } + + val a: Int +} \ No newline at end of file diff --git a/idea/testData/intentions/joinDeclarationAndAssignment/deleteConstructorBlock2.kt.after b/idea/testData/intentions/joinDeclarationAndAssignment/deleteConstructorBlock2.kt.after new file mode 100644 index 00000000000..797b935cfae --- /dev/null +++ b/idea/testData/intentions/joinDeclarationAndAssignment/deleteConstructorBlock2.kt.after @@ -0,0 +1,6 @@ +// DISABLE-ERRORS +class A(i: Int, j: Int) { + constructor(i: Int) : this(i, 2) + + val a: Int = 1 +} \ No newline at end of file diff --git a/idea/testData/intentions/joinDeclarationAndAssignment/singleConstructor.kt b/idea/testData/intentions/joinDeclarationAndAssignment/singleConstructor.kt index eb0686a3ae3..b69c9c797d3 100644 --- a/idea/testData/intentions/joinDeclarationAndAssignment/singleConstructor.kt +++ b/idea/testData/intentions/joinDeclarationAndAssignment/singleConstructor.kt @@ -1,7 +1,10 @@ class A { constructor() { a = 1 + foo() } val a: Int + + fun foo() {} } diff --git a/idea/testData/intentions/joinDeclarationAndAssignment/singleConstructor.kt.after b/idea/testData/intentions/joinDeclarationAndAssignment/singleConstructor.kt.after index b7e322e5d8e..afe0890cdcf 100644 --- a/idea/testData/intentions/joinDeclarationAndAssignment/singleConstructor.kt.after +++ b/idea/testData/intentions/joinDeclarationAndAssignment/singleConstructor.kt.after @@ -1,6 +1,9 @@ class A { constructor() { + foo() } val a: Int = 1 + + fun foo() {} } diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java index 832c9d45e6d..c442369d150 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -10007,6 +10007,21 @@ public class IntentionTestGenerated extends AbstractIntentionTest { runTest("idea/testData/intentions/joinDeclarationAndAssignment/assignmentInIf.kt"); } + @TestMetadata("atAssignment.kt") + public void testAtAssignment() throws Exception { + runTest("idea/testData/intentions/joinDeclarationAndAssignment/atAssignment.kt"); + } + + @TestMetadata("atAssignmentInConstructor.kt") + public void testAtAssignmentInConstructor() throws Exception { + runTest("idea/testData/intentions/joinDeclarationAndAssignment/atAssignmentInConstructor.kt"); + } + + @TestMetadata("atAssignmentInInitializer.kt") + public void testAtAssignmentInInitializer() throws Exception { + runTest("idea/testData/intentions/joinDeclarationAndAssignment/atAssignmentInInitializer.kt"); + } + @TestMetadata("cannotRemoveType.kt") public void testCannotRemoveType() throws Exception { runTest("idea/testData/intentions/joinDeclarationAndAssignment/cannotRemoveType.kt"); @@ -10042,6 +10057,16 @@ public class IntentionTestGenerated extends AbstractIntentionTest { runTest("idea/testData/intentions/joinDeclarationAndAssignment/correctConditionalAssignment.kt"); } + @TestMetadata("deleteConstructorBlock.kt") + public void testDeleteConstructorBlock() throws Exception { + runTest("idea/testData/intentions/joinDeclarationAndAssignment/deleteConstructorBlock.kt"); + } + + @TestMetadata("deleteConstructorBlock2.kt") + public void testDeleteConstructorBlock2() throws Exception { + runTest("idea/testData/intentions/joinDeclarationAndAssignment/deleteConstructorBlock2.kt"); + } + @TestMetadata("deleteInitBlock.kt") public void testDeleteInitBlock() throws Exception { runTest("idea/testData/intentions/joinDeclarationAndAssignment/deleteInitBlock.kt");