Add intention for replacing explicit function literal parameter with 'it'

Kotlin's function literals have a shortcut for one-argument literals:
the single argument doesn't need to be explicitly named, but can be
referred via the 'it' contextual keyword.

For example, 'array(1, 2, 3).filter { x -> x % 2 == 0 }'
          -> 'array(1, 2, 3).filter { it % 2 == 0 }'

Add an intention action for this transformation.
This commit is contained in:
Tuomas Tynkkynen
2014-02-08 15:57:54 -08:00
committed by Nikolay Krasko
parent 1dd57b5f6e
commit c6d6a32314
21 changed files with 226 additions and 1 deletions
@@ -0,0 +1,2 @@
fun foo(a: (Int) -> Int): Int = a(1)
val x = foo { p -> foo { y -> <caret>p } }
@@ -0,0 +1,2 @@
fun foo(a: (Int) -> Int): Int = a(1)
val x = foo { foo { y -> <caret>it } }
@@ -0,0 +1,2 @@
fun applyTwice<A>(f: (A) -> A, x: A) = f(f(x))
val x = applyTwice({ <caret>x -> 1 + x }, 40)
@@ -0,0 +1,2 @@
fun applyTwice<A>(f: (A) -> A, x: A) = f(f(x))
val x = applyTwice({ <caret>1 + it }, 40)
@@ -0,0 +1,2 @@
fun applyTwice<A>(f: (A) -> A, x: A) = f(f(x))
val x = applyTwice({ x -> <caret>x + 1 }, 40)
@@ -0,0 +1,2 @@
fun applyTwice<A>(f: (A) -> A, x: A) = f(f(x))
val x = applyTwice({ <caret>it + 1 }, 40)
@@ -0,0 +1,6 @@
fun foo(i: (Int) -> Unit) {}
fun test() {
foo { <caret>x ->
array(1, 2, 3).filter { x -> x % 2 == 0 }
}
}
@@ -0,0 +1,6 @@
fun foo(i: (Int) -> Unit) {}
fun test() {
foo {
<caret>array(1, 2, 3).filter { x -> x % 2 == 0 }
}
}
@@ -0,0 +1,4 @@
// IS_APPLICABLE: false
fun applyTwice<A>(f: (A) -> A, x: A) = f(f(x))
val x = applyTwice({ i<caret>t + 1 }, 40)
@@ -0,0 +1,4 @@
// IS_APPLICABLE: false
fun call2(f: (Int, Int) -> Int, x: Int, y: Int) = f(x, y)
val foo = call2({ <caret>x, y -> x + y }, 40, 2)
@@ -0,0 +1,8 @@
// IS_APPLICABLE: false
val foo = { (x: Int) ->
class Inner() {
fun temp(<caret>y: Int) : Int { return x + y }
}
Inner()
}
@@ -0,0 +1,2 @@
// IS_APPLICABLE: false
val incr = { (<caret>x: Int) -> x + 1}