More usable caret position
This commit is contained in:
+9
-1
@@ -22,6 +22,7 @@ import org.jetbrains.kotlin.idea.core.moveCaret
|
||||
import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection
|
||||
import org.jetbrains.kotlin.idea.intentions.SelfTargetingRangeIntention
|
||||
import org.jetbrains.kotlin.psi.KtForExpression
|
||||
import org.jetbrains.kotlin.psi.KtProperty
|
||||
import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
||||
|
||||
class LoopToCallChainInspection : IntentionBasedInspection<KtForExpression>(
|
||||
@@ -55,6 +56,13 @@ class LoopToCallChainIntention : SelfTargetingRangeIntention<KtForExpression>(
|
||||
override fun applyTo(element: KtForExpression, editor: Editor?) {
|
||||
val match = match(element)!!
|
||||
val result = convertLoop(element, match)
|
||||
editor?.moveCaret(result.startOffset)
|
||||
|
||||
val offset = when (result) {
|
||||
// if result is variable declaration, put the caret onto its name to allow quick inline
|
||||
is KtProperty -> result.nameIdentifier?.startOffset ?: result.startOffset
|
||||
else -> result.startOffset
|
||||
}
|
||||
|
||||
editor?.moveCaret(offset)
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
// WITH_RUNTIME
|
||||
// INTENTION_TEXT: "Replace with 'any{}'"
|
||||
fun foo(list: List<String>) {
|
||||
<caret>val found = list.any { it.length > 0 }
|
||||
val <caret>found = list.any { it.length > 0 }
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
// WITH_RUNTIME
|
||||
// INTENTION_TEXT: "Replace with 'any{}'"
|
||||
fun foo(list: List<String>) {
|
||||
<caret>val found = list.any { it.length > 0 }
|
||||
val <caret>found = list.any { it.length > 0 }
|
||||
}
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
// WITH_RUNTIME
|
||||
// INTENTION_TEXT: "Replace with 'any{}'"
|
||||
fun foo(list: List<String>) {
|
||||
<caret>val result = if (list.any { it.length > 0 }) 1 else 0
|
||||
val <caret>result = if (list.any { it.length > 0 }) 1 else 0
|
||||
}
|
||||
@@ -3,6 +3,6 @@
|
||||
import java.util.ArrayList
|
||||
|
||||
fun foo(list: List<String>): List<String> {
|
||||
<caret>val result = list.filter { it.length > 0 }
|
||||
val <caret>result = list.filter { it.length > 0 }
|
||||
return result
|
||||
}
|
||||
@@ -4,7 +4,7 @@ import java.util.ArrayList
|
||||
|
||||
fun foo(list: List<String>, p: Int): List<String> {
|
||||
return if (p > 0) {
|
||||
<caret>val result = list.filter { it.length > 0 }
|
||||
val <caret>result = list.filter { it.length > 0 }
|
||||
result
|
||||
}
|
||||
else {
|
||||
|
||||
+1
-1
@@ -3,6 +3,6 @@
|
||||
import java.util.ArrayList
|
||||
|
||||
fun foo(list: List<String>): ArrayList<String> {
|
||||
<caret>val result = list.filterTo(ArrayList<String>()) { it.length > 0 }
|
||||
val <caret>result = list.filterTo(ArrayList<String>()) { it.length > 0 }
|
||||
return result
|
||||
}
|
||||
+1
-1
@@ -4,7 +4,7 @@ import java.util.ArrayList
|
||||
|
||||
fun foo(list: List<String>, p: Int): ArrayList<String> {
|
||||
return if (p > 0) {
|
||||
<caret>val result = list.filterTo(ArrayList<String>()) { it.length > 0 }
|
||||
val <caret>result = list.filterTo(ArrayList<String>()) { it.length > 0 }
|
||||
result
|
||||
}
|
||||
else {
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@ import java.util.*
|
||||
|
||||
fun foo(list: List<String>): ArrayList<String> {
|
||||
return run {
|
||||
<caret>val result = list.filterTo(ArrayList<String>()) { it.length > 0 }
|
||||
val <caret>result = list.filterTo(ArrayList<String>()) { it.length > 0 }
|
||||
result
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -3,7 +3,7 @@
|
||||
import java.util.ArrayList
|
||||
|
||||
fun foo(list: List<String>): MutableList<String> {
|
||||
<caret>val result = list
|
||||
val <caret>result = list
|
||||
.filter { it.length > 0 }
|
||||
.toMutableList()
|
||||
return result
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@ import java.util.ArrayList
|
||||
|
||||
fun foo(): List<String> {
|
||||
while (true) {
|
||||
<caret>val result = list().filter { it.length > 0 }
|
||||
val <caret>result = list().filter { it.length > 0 }
|
||||
|
||||
if (bar1()) continue
|
||||
if (bar2()) break
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// WITH_RUNTIME
|
||||
// INTENTION_TEXT: "Replace with 'count{}'"
|
||||
fun foo(list: List<String>): Int {
|
||||
<caret>val count = list.count { it.isNotBlank() }
|
||||
val <caret>count = list.count { it.isNotBlank() }
|
||||
return count
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
// WITH_RUNTIME
|
||||
// INTENTION_TEXT: "Replace with 'count()'"
|
||||
fun foo(list: Iterable<String>): Int {
|
||||
<caret>val count = list.count()
|
||||
val <caret>count = list.count()
|
||||
return count
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
// WITH_RUNTIME
|
||||
// INTENTION_TEXT: "Replace with 'count{}'"
|
||||
fun foo(list: List<String>): Int {
|
||||
<caret>val count = bar() + list.count { it.isNotBlank() }
|
||||
val <caret>count = bar() + list.count { it.isNotBlank() }
|
||||
return count
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// WITH_RUNTIME
|
||||
// INTENTION_TEXT: "Replace with 'count{}'"
|
||||
fun foo(list: List<String>): Int {
|
||||
<caret>val count = 1 + list.count { it.isNotBlank() }
|
||||
val <caret>count = 1 + list.count { it.isNotBlank() }
|
||||
return count
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
// WITH_RUNTIME
|
||||
// INTENTION_TEXT: "Replace with 'count{}'"
|
||||
fun foo(list: List<String>): Int {
|
||||
<caret>val count = list.count { it.isNotBlank() }
|
||||
val <caret>count = list.count { it.isNotBlank() }
|
||||
return count
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
// WITH_RUNTIME
|
||||
// INTENTION_TEXT: "Replace with 'firstOrNull{}'"
|
||||
fun foo(list: List<String>) {
|
||||
<caret>val result: String? = list.firstOrNull { it.length > 0 }
|
||||
val <caret>result: String? = list.firstOrNull { it.length > 0 }
|
||||
}
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
// WITH_RUNTIME
|
||||
// INTENTION_TEXT: "Replace with 'firstOrNull{}'"
|
||||
fun foo(list: List<String>) {
|
||||
<caret>var result: String? = list.firstOrNull { it.length > 0 }
|
||||
var <caret>result: String? = list.firstOrNull { it.length > 0 }
|
||||
|
||||
result += "1"
|
||||
}
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
// WITH_RUNTIME
|
||||
// INTENTION_TEXT: "Replace with 'firstOrNull{}'"
|
||||
fun foo(list: List<String>) {
|
||||
<caret>val result: String? = list.firstOrNull { // search for first non-empty string in the list
|
||||
val <caret>result: String? = list.firstOrNull { // search for first non-empty string in the list
|
||||
it.length > 0
|
||||
}// string should be non-empty
|
||||
// save it into result
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// WITH_RUNTIME
|
||||
// INTENTION_TEXT: "Replace with 'firstOrNull{}'"
|
||||
fun foo(list: List<String>) {
|
||||
<caret>val result: String? = list
|
||||
val <caret>result: String? = list
|
||||
.firstOrNull { it.length > 0 }
|
||||
?.let { bar(it) }
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// WITH_RUNTIME
|
||||
// INTENTION_TEXT: "Replace with 'firstOrNull{}'"
|
||||
fun foo(list: List<String>) {
|
||||
<caret>val result: String? = list
|
||||
val <caret>result: String? = list
|
||||
.firstOrNull { it.length > 0 }
|
||||
?.let { it.substring(0, it.length - 1) }
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// WITH_RUNTIME
|
||||
// INTENTION_TEXT: "Replace with 'firstOrNull{}'"
|
||||
fun foo(list: List<String>) {
|
||||
<caret>val result = list
|
||||
val <caret>result = list
|
||||
.firstOrNull { it.length > 0 }
|
||||
?.let { bar(it) } ?: ""
|
||||
}
|
||||
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
// WITH_RUNTIME
|
||||
// INTENTION_TEXT: "Replace with 'firstOrNull{}'"
|
||||
fun foo(list: List<String?>) {
|
||||
<caret>val result: String? = list
|
||||
val <caret>result: String? = list
|
||||
.firstOrNull { it != "" }
|
||||
?.substring(1)
|
||||
}
|
||||
|
||||
@@ -3,6 +3,6 @@
|
||||
import java.util.ArrayList
|
||||
|
||||
fun foo(list: List<String>): List<String> {
|
||||
<caret>val target = list.flatMapTo(ArrayList<String>(100)) { it.lines() }
|
||||
val <caret>target = list.flatMapTo(ArrayList<String>(100)) { it.lines() }
|
||||
return target
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
// WITH_RUNTIME
|
||||
// INTENTION_TEXT: "Replace with 'flatMap{}.firstOrNull{}'"
|
||||
fun foo(list: List<String>): String? {
|
||||
<caret>val result: String? = list
|
||||
val <caret>result: String? = list
|
||||
.flatMap { it.lines() }
|
||||
.firstOrNull { it.isNotBlank() }
|
||||
return result
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
// WITH_RUNTIME
|
||||
// INTENTION_TEXT: "Replace with 'any{}'"
|
||||
fun foo(list: List<String>, it: Int) {
|
||||
<caret>val found = list.any { s -> s.length > it }
|
||||
val <caret>found = list.any { s -> s.length > it }
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
// WITH_RUNTIME
|
||||
// INTENTION_TEXT: "Replace with 'lastOrNull{}'"
|
||||
fun foo(list: List<String>) {
|
||||
<caret>val result: String? = list.lastOrNull { it.length > 0 }
|
||||
val <caret>result: String? = list.lastOrNull { it.length > 0 }
|
||||
}
|
||||
@@ -3,7 +3,7 @@
|
||||
import java.util.ArrayList
|
||||
|
||||
fun foo(list: List<String>): List<Int> {
|
||||
<caret>val target = list
|
||||
val <caret>target = list
|
||||
.filter { it.length > 0 }
|
||||
.mapTo(ArrayList<Int>(100)) { it.hashCode() }
|
||||
return target
|
||||
|
||||
@@ -3,6 +3,6 @@
|
||||
import java.util.ArrayList
|
||||
|
||||
fun foo(map: Map<Int, String>): List<String> {
|
||||
<caret>val result = map.values.toList()
|
||||
val <caret>result = map.values.toList()
|
||||
return result
|
||||
}
|
||||
@@ -3,6 +3,6 @@
|
||||
import java.util.HashSet
|
||||
|
||||
fun foo(map: Map<Int, String>): MutableCollection<String> {
|
||||
<caret>val result = map.values.toMutableSet()
|
||||
val <caret>result = map.values.toMutableSet()
|
||||
return result
|
||||
}
|
||||
+1
-1
@@ -3,6 +3,6 @@
|
||||
import java.util.HashSet
|
||||
|
||||
fun foo(map: Map<Int, String>): Collection<String> {
|
||||
<caret>val result = map.values.toSet()
|
||||
val <caret>result = map.values.toSet()
|
||||
return result
|
||||
}
|
||||
@@ -3,7 +3,7 @@
|
||||
import java.util.HashSet
|
||||
|
||||
fun foo(map: Map<Int, String>): Collection<Int> {
|
||||
<caret>val result = map.values
|
||||
val <caret>result = map.values
|
||||
.map { it.length }
|
||||
.toSet()
|
||||
return result
|
||||
|
||||
Reference in New Issue
Block a user