Restored live template but made it more useful

This commit is contained in:
Valentin Kipyatkov
2015-07-21 15:51:58 +03:00
parent 16ae9e3861
commit 82cd58a556
13 changed files with 56 additions and 18 deletions
@@ -17,15 +17,14 @@
package org.jetbrains.kotlin.idea.intentions
import com.intellij.codeInsight.intention.LowPriorityAction
import com.intellij.codeInsight.template.TemplateBuilderImpl
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiDocumentManager
import org.jetbrains.kotlin.analyzer.analyzeInContext
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.core.replaced
import org.jetbrains.kotlin.psi.JetExpression
import org.jetbrains.kotlin.psi.JetForExpression
import org.jetbrains.kotlin.psi.JetPsiFactory
import org.jetbrains.kotlin.psi.createExpressionByPattern
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.endOffset
import org.jetbrains.kotlin.psi.psiUtil.startOffset
import org.jetbrains.kotlin.resolve.BindingContext
@@ -68,7 +67,34 @@ public class AddForLoopIndicesIntention : JetSelfTargetingRangeIntention<JetForE
val indexVariable = multiParameter.entries[0]
editor.caretModel.moveToOffset(indexVariable.startOffset)
editor.selectionModel.setSelection(indexVariable.startOffset, indexVariable.endOffset)
runTemplate(editor, element, indexVariable)
}
private fun runTemplate(editor: Editor, forExpression: JetForExpression, indexVariable: JetMultiDeclarationEntry) {
PsiDocumentManager.getInstance(forExpression.project).doPostponedOperationsAndUnblockDocument(editor.document)
val templateBuilder = TemplateBuilderImpl(forExpression)
templateBuilder.replaceElement(indexVariable, ChooseStringExpression(listOf("index", "i")))
val body = forExpression.body
when (body) {
is JetBlockExpression -> {
val statement = body.statements.firstOrNull()
if (statement != null) {
templateBuilder.setEndVariableBefore(statement)
}
else {
templateBuilder.setEndVariableAfter(body.lBrace)
}
}
null -> forExpression.rightParenthesis.let { templateBuilder.setEndVariableAfter(it) }
else -> templateBuilder.setEndVariableBefore(body)
}
templateBuilder.run(editor, true)
}
private fun createWithIndexExpression(originalExpression: JetExpression): JetExpression {
@@ -1,7 +1,7 @@
// WITH_RUNTIME
fun a() {
val b = listOf(1,2,3,4,5)
for ((index, c : Int) in b.withIndex()) {
for ((index, c : Int) in b.withIndex()) {<caret>
}
}
@@ -1,6 +1,6 @@
// WITH_RUNTIME
fun foo(bar: IntArray) {
for ((index, a) in bar.withIndex()) {
for ((index, a) in bar.withIndex()) {<caret>
}
}
@@ -1,6 +1,6 @@
// WITH_RUNTIME
fun foo(bar: Iterable<Int>) {
for ((index, a) in bar.withIndex()) {
for ((index, a) in bar.withIndex()) {<caret>
}
}
+4
View File
@@ -0,0 +1,4 @@
// WITH_RUNTIME
fun foo(bar: IntArray) {
for (<caret>a in bar)
}
@@ -0,0 +1,4 @@
// WITH_RUNTIME
fun foo(bar: IntArray) {
for ((index, a) in bar.withIndex())<caret>
}
@@ -1,6 +1,6 @@
// WITH_RUNTIME
fun foo(bar: Array<Object>) {
for ((index, a) in bar.withIndex()) {
for ((index, a) in bar.withIndex()) {<caret>
}
}
+2 -3
View File
@@ -1,6 +1,5 @@
// WITH_RUNTIME
fun foo(bar: Sequence<String>) {
for (<caret>a in bar) {
}
for (<caret>a in bar)
print(a)
}
@@ -1,6 +1,5 @@
// WITH_RUNTIME
fun foo(bar: Sequence<String>) {
for ((index, a) in bar.withIndex()) {
}
for ((index, a) in bar.withIndex())
<caret>print(a)
}
@@ -1,7 +1,7 @@
// WITH_RUNTIME
fun a() {
val b = listOf(1,2,3,4,5)
for ((index, c) in b.withIndex()) {
for ((index, c) in b.withIndex()) {<caret>
}
}
+1 -1
View File
@@ -1,6 +1,6 @@
// WITH_RUNTIME
fun foo(bar: String) {
for (<caret>a in bar) {
print(a)
}
}
+1 -1
View File
@@ -1,6 +1,6 @@
// WITH_RUNTIME
fun foo(bar: String) {
for ((index, a) in bar.withIndex()) {
<caret>print(a)
}
}
@@ -148,6 +148,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
doTest(fileName);
}
@TestMetadata("noBody.kt")
public void testNoBody() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/addForLoopIndices/noBody.kt");
doTest(fileName);
}
@TestMetadata("objectArray.kt")
public void testObjectArray() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/addForLoopIndices/objectArray.kt");