Convert to scope function: enable on single function call

#KT-29099 Fixed
This commit is contained in:
Toshiaki Kameyama
2019-02-16 12:02:57 +09:00
committed by Mikhail Glukhikh
parent e1fdf0aa43
commit 5f7dee4194
18 changed files with 177 additions and 5 deletions
@@ -111,7 +111,7 @@ sealed class ConvertToScopeIntention(
(prev + listOf(this) + next) to referenceName
}
}
if (targets.size < 2) return null
if (targets.isEmpty()) return null
return targets to referenceName
}
@@ -1,5 +1,4 @@
// WITH_RUNTIME
// IS_APPLICABLE: false
class Foo {
fun foo(i: Int) {}
@@ -11,6 +10,7 @@ fun test() {
listOf(1).forEach {
val f = Foo()<caret>
f.foo(1)
f.foo(2)
bar(it, f)
}
}
@@ -0,0 +1,17 @@
// WITH_RUNTIME
class Foo {
fun foo(i: Int) {}
}
fun bar(i: Int, f: Foo) {}
fun test() {
listOf(1).forEach {
val f = Foo().also {
it.foo(1)
it.foo(2)
}
bar(it, f)
}
}
@@ -0,0 +1,10 @@
// WITH_RUNTIME
class C {
fun foo() {}
}
fun test() {
val c = C()
c.foo()<caret>
}
@@ -0,0 +1,11 @@
// WITH_RUNTIME
class C {
fun foo() {}
}
fun test() {
val c = C().also {
it.foo()
}
}
@@ -1,5 +1,4 @@
// WITH_RUNTIME
// IS_APPLICABLE: false
class Foo {
fun foo(i: Int) {}
@@ -7,6 +6,7 @@ class Foo {
fun test(f: Foo) {
val f = Foo()<caret>
f.foo(1)
f.foo(2)
bar(2, this)
}
}
@@ -0,0 +1,15 @@
// WITH_RUNTIME
class Foo {
fun foo(i: Int) {}
fun test(f: Foo) {
val f = Foo().apply {
foo(1)
foo(2)
}
bar(2, this)
}
}
fun bar(i: Int, f: Foo) {}
@@ -0,0 +1,10 @@
// WITH_RUNTIME
class C {
fun foo() {}
}
fun test() {
val c = C()
c.foo()<caret>
}
@@ -0,0 +1,11 @@
// WITH_RUNTIME
class C {
fun foo() {}
}
fun test() {
val c = C().apply {
foo()
}
}
@@ -1,5 +1,4 @@
// WITH_RUNTIME
// IS_APPLICABLE: false
class MyClass {
fun foo1(a: MyClass): MyClass = this
@@ -9,5 +8,7 @@ class MyClass {
fun foo4(a: MyClass) {
a.foo1(this).foo2().foo3()
a.foo2()<caret>
a.foo3()
a.foo1(this).foo2().foo3()
}
}
@@ -0,0 +1,16 @@
// WITH_RUNTIME
class MyClass {
fun foo1(a: MyClass): MyClass = this
fun foo2(): MyClass = this
fun foo3(): MyClass = this
fun foo4(a: MyClass) {
a.foo1(this).foo2().foo3()
a.run {
foo2()
foo3()
}
a.foo1(this).foo2().foo3()
}
}
@@ -0,0 +1,10 @@
// WITH_RUNTIME
class C {
fun foo() {}
}
fun test() {
val c = C()
c.foo()<caret>
}
@@ -0,0 +1,12 @@
// WITH_RUNTIME
class C {
fun foo() {}
}
fun test() {
val c = C()
c.run {
foo()
}
}
@@ -1,5 +1,4 @@
// WITH_RUNTIME
// IS_APPLICABLE: false
class MyClass {
fun foo1(a: MyClass): MyClass = this
@@ -9,5 +8,7 @@ class MyClass {
fun foo4(a: MyClass) {
a.foo1(this).foo2().foo3()
a.foo2()<caret>
a.foo3()
a.foo1(this).foo2().foo3()
}
}
@@ -0,0 +1,16 @@
// WITH_RUNTIME
class MyClass {
fun foo1(a: MyClass): MyClass = this
fun foo2(): MyClass = this
fun foo3(): MyClass = this
fun foo4(a: MyClass) {
a.foo1(this).foo2().foo3()
with(a) {
foo2()
foo3()
}
a.foo1(this).foo2().foo3()
}
}
@@ -0,0 +1,10 @@
// WITH_RUNTIME
class C {
fun foo() {}
}
fun test() {
val c = C()
c.foo()<caret>
}
@@ -0,0 +1,12 @@
// WITH_RUNTIME
class C {
fun foo() {}
}
fun test() {
val c = C()
with(c) {
foo()
}
}
@@ -7553,6 +7553,11 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
runTest("idea/testData/intentions/convertToScope/convertToAlso/onProperty.kt");
}
@TestMetadata("singleCall.kt")
public void testSingleCall() throws Exception {
runTest("idea/testData/intentions/convertToScope/convertToAlso/singleCall.kt");
}
@TestMetadata("untilItParameter.kt")
public void testUntilItParameter() throws Exception {
runTest("idea/testData/intentions/convertToScope/convertToAlso/untilItParameter.kt");
@@ -7641,6 +7646,11 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
runTest("idea/testData/intentions/convertToScope/convertToApply/onProperty.kt");
}
@TestMetadata("singleCall.kt")
public void testSingleCall() throws Exception {
runTest("idea/testData/intentions/convertToScope/convertToApply/singleCall.kt");
}
@TestMetadata("thisParameter.kt")
public void testThisParameter() throws Exception {
runTest("idea/testData/intentions/convertToScope/convertToApply/thisParameter.kt");
@@ -7744,6 +7754,11 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
runTest("idea/testData/intentions/convertToScope/convertToRun/onProperty.kt");
}
@TestMetadata("singleCall.kt")
public void testSingleCall() throws Exception {
runTest("idea/testData/intentions/convertToScope/convertToRun/singleCall.kt");
}
@TestMetadata("thisParameter.kt")
public void testThisParameter() throws Exception {
runTest("idea/testData/intentions/convertToScope/convertToRun/thisParameter.kt");
@@ -7842,6 +7857,11 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
runTest("idea/testData/intentions/convertToScope/convertToWith/onProperty.kt");
}
@TestMetadata("singleCall.kt")
public void testSingleCall() throws Exception {
runTest("idea/testData/intentions/convertToScope/convertToWith/singleCall.kt");
}
@TestMetadata("thisParameter.kt")
public void testThisParameter() throws Exception {
runTest("idea/testData/intentions/convertToScope/convertToWith/thisParameter.kt");