More usable caret position

This commit is contained in:
Valentin Kipyatkov
2016-04-20 16:44:33 +03:00
parent db53794663
commit 345f2b8a52
32 changed files with 40 additions and 32 deletions
@@ -22,6 +22,7 @@ import org.jetbrains.kotlin.idea.core.moveCaret
import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection
import org.jetbrains.kotlin.idea.intentions.SelfTargetingRangeIntention import org.jetbrains.kotlin.idea.intentions.SelfTargetingRangeIntention
import org.jetbrains.kotlin.psi.KtForExpression import org.jetbrains.kotlin.psi.KtForExpression
import org.jetbrains.kotlin.psi.KtProperty
import org.jetbrains.kotlin.psi.psiUtil.startOffset import org.jetbrains.kotlin.psi.psiUtil.startOffset
class LoopToCallChainInspection : IntentionBasedInspection<KtForExpression>( class LoopToCallChainInspection : IntentionBasedInspection<KtForExpression>(
@@ -55,6 +56,13 @@ class LoopToCallChainIntention : SelfTargetingRangeIntention<KtForExpression>(
override fun applyTo(element: KtForExpression, editor: Editor?) { override fun applyTo(element: KtForExpression, editor: Editor?) {
val match = match(element)!! val match = match(element)!!
val result = convertLoop(element, match) 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 // WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'any{}'" // INTENTION_TEXT: "Replace with 'any{}'"
fun foo(list: List<String>) { 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 // WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'any{}'" // INTENTION_TEXT: "Replace with 'any{}'"
fun foo(list: List<String>) { 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 // WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'any{}'" // INTENTION_TEXT: "Replace with 'any{}'"
fun foo(list: List<String>) { 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 import java.util.ArrayList
fun foo(list: List<String>): List<String> { 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 return result
} }
@@ -4,7 +4,7 @@ import java.util.ArrayList
fun foo(list: List<String>, p: Int): List<String> { fun foo(list: List<String>, p: Int): List<String> {
return if (p > 0) { return if (p > 0) {
<caret>val result = list.filter { it.length > 0 } val <caret>result = list.filter { it.length > 0 }
result result
} }
else { else {
@@ -3,6 +3,6 @@
import java.util.ArrayList import java.util.ArrayList
fun foo(list: List<String>): ArrayList<String> { 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 return result
} }
@@ -4,7 +4,7 @@ import java.util.ArrayList
fun foo(list: List<String>, p: Int): ArrayList<String> { fun foo(list: List<String>, p: Int): ArrayList<String> {
return if (p > 0) { 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 result
} }
else { else {
@@ -4,7 +4,7 @@ import java.util.*
fun foo(list: List<String>): ArrayList<String> { fun foo(list: List<String>): ArrayList<String> {
return run { return run {
<caret>val result = list.filterTo(ArrayList<String>()) { it.length > 0 } val <caret>result = list.filterTo(ArrayList<String>()) { it.length > 0 }
result result
} }
} }
@@ -3,7 +3,7 @@
import java.util.ArrayList import java.util.ArrayList
fun foo(list: List<String>): MutableList<String> { fun foo(list: List<String>): MutableList<String> {
<caret>val result = list val <caret>result = list
.filter { it.length > 0 } .filter { it.length > 0 }
.toMutableList() .toMutableList()
return result return result
@@ -4,7 +4,7 @@ import java.util.ArrayList
fun foo(): List<String> { fun foo(): List<String> {
while (true) { while (true) {
<caret>val result = list().filter { it.length > 0 } val <caret>result = list().filter { it.length > 0 }
if (bar1()) continue if (bar1()) continue
if (bar2()) break if (bar2()) break
+1 -1
View File
@@ -1,6 +1,6 @@
// WITH_RUNTIME // WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'count{}'" // INTENTION_TEXT: "Replace with 'count{}'"
fun foo(list: List<String>): Int { fun foo(list: List<String>): Int {
<caret>val count = list.count { it.isNotBlank() } val <caret>count = list.count { it.isNotBlank() }
return count return count
} }
+1 -1
View File
@@ -1,6 +1,6 @@
// WITH_RUNTIME // WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'count()'" // INTENTION_TEXT: "Replace with 'count()'"
fun foo(list: Iterable<String>): Int { fun foo(list: Iterable<String>): Int {
<caret>val count = list.count() val <caret>count = list.count()
return count return count
} }
@@ -1,7 +1,7 @@
// WITH_RUNTIME // WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'count{}'" // INTENTION_TEXT: "Replace with 'count{}'"
fun foo(list: List<String>): Int { fun foo(list: List<String>): Int {
<caret>val count = bar() + list.count { it.isNotBlank() } val <caret>count = bar() + list.count { it.isNotBlank() }
return count return count
} }
@@ -1,6 +1,6 @@
// WITH_RUNTIME // WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'count{}'" // INTENTION_TEXT: "Replace with 'count{}'"
fun foo(list: List<String>): Int { fun foo(list: List<String>): Int {
<caret>val count = 1 + list.count { it.isNotBlank() } val <caret>count = 1 + list.count { it.isNotBlank() }
return count return count
} }
@@ -1,6 +1,6 @@
// WITH_RUNTIME // WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'count{}'" // INTENTION_TEXT: "Replace with 'count{}'"
fun foo(list: List<String>): Int { fun foo(list: List<String>): Int {
<caret>val count = list.count { it.isNotBlank() } val <caret>count = list.count { it.isNotBlank() }
return count return count
} }
@@ -1,5 +1,5 @@
// WITH_RUNTIME // WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'firstOrNull{}'" // INTENTION_TEXT: "Replace with 'firstOrNull{}'"
fun foo(list: List<String>) { fun foo(list: List<String>) {
<caret>val result: String? = list.firstOrNull { it.length > 0 } val <caret>result: String? = list.firstOrNull { it.length > 0 }
} }
@@ -1,7 +1,7 @@
// WITH_RUNTIME // WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'firstOrNull{}'" // INTENTION_TEXT: "Replace with 'firstOrNull{}'"
fun foo(list: List<String>) { 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" result += "1"
} }
@@ -1,7 +1,7 @@
// WITH_RUNTIME // WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'firstOrNull{}'" // INTENTION_TEXT: "Replace with 'firstOrNull{}'"
fun foo(list: List<String>) { 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 it.length > 0
}// string should be non-empty }// string should be non-empty
// save it into result // save it into result
@@ -1,7 +1,7 @@
// WITH_RUNTIME // WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'firstOrNull{}'" // INTENTION_TEXT: "Replace with 'firstOrNull{}'"
fun foo(list: List<String>) { fun foo(list: List<String>) {
<caret>val result: String? = list val <caret>result: String? = list
.firstOrNull { it.length > 0 } .firstOrNull { it.length > 0 }
?.let { bar(it) } ?.let { bar(it) }
} }
@@ -1,7 +1,7 @@
// WITH_RUNTIME // WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'firstOrNull{}'" // INTENTION_TEXT: "Replace with 'firstOrNull{}'"
fun foo(list: List<String>) { fun foo(list: List<String>) {
<caret>val result: String? = list val <caret>result: String? = list
.firstOrNull { it.length > 0 } .firstOrNull { it.length > 0 }
?.let { it.substring(0, it.length - 1) } ?.let { it.substring(0, it.length - 1) }
} }
@@ -1,7 +1,7 @@
// WITH_RUNTIME // WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'firstOrNull{}'" // INTENTION_TEXT: "Replace with 'firstOrNull{}'"
fun foo(list: List<String>) { fun foo(list: List<String>) {
<caret>val result = list val <caret>result = list
.firstOrNull { it.length > 0 } .firstOrNull { it.length > 0 }
?.let { bar(it) } ?: "" ?.let { bar(it) } ?: ""
} }
@@ -1,7 +1,7 @@
// WITH_RUNTIME // WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'firstOrNull{}'" // INTENTION_TEXT: "Replace with 'firstOrNull{}'"
fun foo(list: List<String?>) { fun foo(list: List<String?>) {
<caret>val result: String? = list val <caret>result: String? = list
.firstOrNull { it != "" } .firstOrNull { it != "" }
?.substring(1) ?.substring(1)
} }
@@ -3,6 +3,6 @@
import java.util.ArrayList import java.util.ArrayList
fun foo(list: List<String>): List<String> { 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 return target
} }
@@ -1,7 +1,7 @@
// WITH_RUNTIME // WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'flatMap{}.firstOrNull{}'" // INTENTION_TEXT: "Replace with 'flatMap{}.firstOrNull{}'"
fun foo(list: List<String>): String? { fun foo(list: List<String>): String? {
<caret>val result: String? = list val <caret>result: String? = list
.flatMap { it.lines() } .flatMap { it.lines() }
.firstOrNull { it.isNotBlank() } .firstOrNull { it.isNotBlank() }
return result return result
@@ -1,5 +1,5 @@
// WITH_RUNTIME // WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'any{}'" // INTENTION_TEXT: "Replace with 'any{}'"
fun foo(list: List<String>, it: Int) { 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 // WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'lastOrNull{}'" // INTENTION_TEXT: "Replace with 'lastOrNull{}'"
fun foo(list: List<String>) { fun foo(list: List<String>) {
<caret>val result: String? = list.lastOrNull { it.length > 0 } val <caret>result: String? = list.lastOrNull { it.length > 0 }
} }
+1 -1
View File
@@ -3,7 +3,7 @@
import java.util.ArrayList import java.util.ArrayList
fun foo(list: List<String>): List<Int> { fun foo(list: List<String>): List<Int> {
<caret>val target = list val <caret>target = list
.filter { it.length > 0 } .filter { it.length > 0 }
.mapTo(ArrayList<Int>(100)) { it.hashCode() } .mapTo(ArrayList<Int>(100)) { it.hashCode() }
return target return target
+1 -1
View File
@@ -3,6 +3,6 @@
import java.util.ArrayList import java.util.ArrayList
fun foo(map: Map<Int, String>): List<String> { fun foo(map: Map<Int, String>): List<String> {
<caret>val result = map.values.toList() val <caret>result = map.values.toList()
return result return result
} }
@@ -3,6 +3,6 @@
import java.util.HashSet import java.util.HashSet
fun foo(map: Map<Int, String>): MutableCollection<String> { fun foo(map: Map<Int, String>): MutableCollection<String> {
<caret>val result = map.values.toMutableSet() val <caret>result = map.values.toMutableSet()
return result return result
} }
+1 -1
View File
@@ -3,6 +3,6 @@
import java.util.HashSet import java.util.HashSet
fun foo(map: Map<Int, String>): Collection<String> { fun foo(map: Map<Int, String>): Collection<String> {
<caret>val result = map.values.toSet() val <caret>result = map.values.toSet()
return result return result
} }
@@ -3,7 +3,7 @@
import java.util.HashSet import java.util.HashSet
fun foo(map: Map<Int, String>): Collection<Int> { fun foo(map: Map<Int, String>): Collection<Int> {
<caret>val result = map.values val <caret>result = map.values
.map { it.length } .map { it.length }
.toSet() .toSet()
return result return result