diff --git a/idea/src/META-INF/plugin.xml b/idea/src/META-INF/plugin.xml
index fa8065d6150..27eeb9b18f4 100644
--- a/idea/src/META-INF/plugin.xml
+++ b/idea/src/META-INF/plugin.xml
@@ -803,11 +803,6 @@
Kotlin
S
-
- org.jetbrains.jet.plugin.intentions.SwapBinaryExpression
- Kotlin
-
-
org.jetbrains.kotlin.idea.intentions.SwapBinaryExpressionIntention
Kotlin
diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/AddForLoopIndicesIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/AddForLoopIndicesIntention.kt
index 5030329a8d2..05fd49970d3 100644
--- a/idea/src/org/jetbrains/kotlin/idea/intentions/AddForLoopIndicesIntention.kt
+++ b/idea/src/org/jetbrains/kotlin/idea/intentions/AddForLoopIndicesIntention.kt
@@ -16,34 +16,33 @@
package org.jetbrains.kotlin.idea.intentions
-import com.intellij.openapi.editor.Editor
-import org.jetbrains.jet.lang.psi.JetForExpression
-import org.jetbrains.jet.lang.psi.JetPsiFactory
-import org.jetbrains.jet.lang.psi.JetDotQualifiedExpression
-import com.intellij.psi.util.PsiTreeUtil
-import com.intellij.psi.PsiDocumentManager
-import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache
-import org.jetbrains.jet.lang.resolve.BindingContext
-import org.jetbrains.jet.lang.psi.JetParenthesizedExpression
import com.intellij.codeInsight.template.TemplateBuilderImpl
+import com.intellij.codeInsight.template.TemplateManager
import com.intellij.codeInsight.template.impl.TemplateManagerImpl
-import org.jetbrains.jet.lang.resolve.DescriptorUtils
-import org.jetbrains.jet.analyzer.analyzeInContext
-import org.jetbrains.jet.lang.psi.JetCallExpression
+import com.intellij.openapi.editor.Editor
+import com.intellij.psi.PsiDocumentManager
+import com.intellij.psi.util.PsiTreeUtil
+import org.jetbrains.kotlin.analyzer.analyzeInContext
+import org.jetbrains.kotlin.idea.caches.resolve.analyze
+import org.jetbrains.kotlin.psi.*
+import org.jetbrains.kotlin.resolve.BindingContext
+import org.jetbrains.kotlin.resolve.DescriptorUtils
public class AddForLoopIndicesIntention : JetSelfTargetingIntention(
- "add.for.loop.indices", javaClass()) {
+ javaClass(), "Add indices to 'for' loop") {
override fun applyTo(element: JetForExpression, editor: Editor) {
val loopRange = element.getLoopRange()!!
- val newRangeText = "${loopRange.getText()}.withIndices()"
- val newRange = JetPsiFactory.createExpression(editor.getProject(), newRangeText)
+ val newRangeText = "${loopRange.getText()}.withIndex()"
+ val project = editor.getProject()!!
+ val psiFactory = JetPsiFactory(project)
+ val newRange = psiFactory.createExpression(newRangeText)
//Roundabout way to create new multiparameter element so as not to incorrectly trigger syntax error highlighting
val loopParameter = element.getLoopParameter()!!
- val parenthesizedParam = JetPsiFactory.createExpression(editor.getProject(), "(index)") as JetParenthesizedExpression
+ val parenthesizedParam = psiFactory.createExpression("(index)") as JetParenthesizedExpression
val indexElement = parenthesizedParam.getExpression()!!
- val comma = JetPsiFactory.createComma(editor.getProject())
- val newParamElement = JetPsiFactory.createExpression(editor.getProject(), " ${loopParameter.getText()}")
+ val comma = psiFactory.createComma()
+ val newParamElement = psiFactory.createExpression(loopParameter.getText())
parenthesizedParam.addAfter(newParamElement, indexElement)
parenthesizedParam.addAfter(comma, indexElement)
@@ -55,29 +54,31 @@ public class AddForLoopIndicesIntention : JetSelfTargetingIntention= body.getTextRange().getStartOffset()) return false
+
val range = element.getLoopRange() ?: return false
if (range is JetDotQualifiedExpression) {
val selector = range.getSelectorExpression() ?: return true
- if (selector.getText() == "withIndices()") return false
+ if (selector.getText() == "withIndex()") return false
}
- val potentialExpression = JetPsiFactory.createExpression(element.getProject(),
- "${range.getText()}.withIndices()") as JetDotQualifiedExpression
+ val psiFactory = JetPsiFactory(element.getProject())
+ val potentialExpression = psiFactory.createExpression("${range.getText()}.withIndex()") as JetDotQualifiedExpression
- val bindingContext = AnalyzerFacadeWithCache.getContextForElement(element)
+ val bindingContext = element.analyze()
val scope = bindingContext[BindingContext.RESOLUTION_SCOPE, element] ?: return false
val functionSelector = potentialExpression.getSelectorExpression() as JetCallExpression
val updatedContext = potentialExpression.analyzeInContext(scope)
- val callScope = updatedContext[BindingContext.RESOLVED_CALL, functionSelector.getCalleeExpression()!!] ?: return false
+ val call = updatedContext[BindingContext.CALL, functionSelector.getCalleeExpression()] ?: return false
+ val callScope = updatedContext[BindingContext.RESOLVED_CALL, call] ?: return false
val callFqName = DescriptorUtils.getFqNameSafe(callScope.getCandidateDescriptor())
- return callFqName.toString() == "kotlin.withIndices"
+ return callFqName.toString() == "kotlin.withIndex"
}
-
-}
\ No newline at end of file
+}
diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveForLoopIndicesIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveForLoopIndicesIntention.kt
index 638781790ef..649db97d58f 100644
--- a/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveForLoopIndicesIntention.kt
+++ b/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveForLoopIndicesIntention.kt
@@ -16,55 +16,48 @@
package org.jetbrains.kotlin.idea.intentions
-import org.jetbrains.jet.lang.psi.JetForExpression
-import com.intellij.openapi.editor.Editor
-import org.jetbrains.jet.lang.psi.JetDotQualifiedExpression
-import org.jetbrains.jet.lang.psi.JetPsiUtil
-import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache
import com.intellij.find.FindManager
import com.intellij.find.impl.FindManagerImpl
+import com.intellij.openapi.editor.Editor
+import com.intellij.psi.search.searches.ReferencesSearch
import com.intellij.usageView.UsageInfo
-import org.jetbrains.jet.plugin.findUsages.KotlinPropertyFindUsagesOptions
-import org.jetbrains.jet.lang.resolve.BindingContext
-import org.jetbrains.jet.lang.psi.JetCallExpression
-import org.jetbrains.jet.lang.resolve.DescriptorUtils
+import org.jetbrains.kotlin.idea.caches.resolve.analyze
+import org.jetbrains.kotlin.idea.findUsages.KotlinPropertyFindUsagesOptions
+import org.jetbrains.kotlin.psi.*
+import org.jetbrains.kotlin.resolve.BindingContext
+import org.jetbrains.kotlin.resolve.DescriptorUtils
public class RemoveForLoopIndicesIntention : JetSelfTargetingIntention(
- "remove.for.loop.indices", javaClass()) {
+ javaClass(), "Remove indices in 'for' loop") {
override fun applyTo(element: JetForExpression, editor: Editor) {
val parameter = element.getMultiParameter()!!
val range = element.getLoopRange() as JetDotQualifiedExpression
val parameters = parameter.getEntries()
- if (parameters.size() == 2) {
- parameter.replace(parameters[1])
- }
- else {
- JetPsiUtil.deleteElementWithDelimiters(parameters[0])
- }
+
+ val loop = JetPsiFactory(element).createExpression("for (${parameters[1].getText()} in _) {}") as JetForExpression
+ parameter.replace(loop.getLoopParameter()!!)
range.replace(range.getReceiverExpression())
}
- override fun isApplicableTo(element: JetForExpression): Boolean {
+ override fun isApplicableTo(element: JetForExpression, caretOffset: Int): Boolean {
val multiParameter = element.getMultiParameter() ?: return false
+ if (multiParameter.getEntries().size() != 2) return false
val range = element.getLoopRange() as? JetDotQualifiedExpression ?: return false
val selector = range.getSelectorExpression() as? JetCallExpression ?: return false
- if (!selector.textMatches("withIndices()")) return false
+ if (!selector.textMatches("withIndex()")) return false
- val bindingContext = AnalyzerFacadeWithCache.getContextForElement(element)
- val callResolution = bindingContext[BindingContext.RESOLVED_CALL, selector.getCalleeExpression()!!] ?: return false
+ val body = element.getBody()
+ if (body != null && caretOffset >= body.getTextRange().getStartOffset()) return false
+
+ val bindingContext = element.analyze()
+ val call = bindingContext[BindingContext.CALL, selector.getCalleeExpression()] ?: return false
+ val callResolution = bindingContext[BindingContext.RESOLVED_CALL, call] ?: return false
val fqName = DescriptorUtils.getFqNameSafe(callResolution.getCandidateDescriptor())
- if (fqName.toString() != "kotlin.withIndices") return false
+ if (fqName.toString() != "kotlin.withIndex") return false
val indexVar = multiParameter.getEntries()[0]
- val findManager = FindManager.getInstance(element.getProject()) as FindManagerImpl
- val findHandler = findManager.getFindUsagesManager().getFindUsagesHandler(indexVar, false) ?: return false
- val options = KotlinPropertyFindUsagesOptions(element.getProject())
- var usageCount = 0
- val processorLambda: (UsageInfo?) -> Boolean = { t -> usageCount++; false }
- findHandler.processElementUsages(indexVar, processorLambda, options)
- return usageCount == 0
+ return ReferencesSearch.search(indexVar).findFirst() == null
}
-
-}
\ No newline at end of file
+}
diff --git a/idea/testData/intentions/addForLoopIndicesIntention/.intention b/idea/testData/intentions/addForLoopIndicesIntention/.intention
index e69de29bb2d..8e1d6530547 100644
--- a/idea/testData/intentions/addForLoopIndicesIntention/.intention
+++ b/idea/testData/intentions/addForLoopIndicesIntention/.intention
@@ -0,0 +1 @@
+org.jetbrains.kotlin.idea.intentions.AddForLoopIndicesIntention
\ No newline at end of file
diff --git a/idea/testData/intentions/addForLoopIndicesIntention/inapplicableExistingIndices.kt b/idea/testData/intentions/addForLoopIndicesIntention/inapplicableExistingIndices.kt
index b9b2b899071..08b3a0ec6c9 100644
--- a/idea/testData/intentions/addForLoopIndicesIntention/inapplicableExistingIndices.kt
+++ b/idea/testData/intentions/addForLoopIndicesIntention/inapplicableExistingIndices.kt
@@ -1,7 +1,7 @@
//IS_APPLICABLE: FALSE
// WITH_RUNTIME
fun b(c: List) {
- for ((indexVariable, d) in c.withIndices()) {
+ for ((indexVariable, d) in c.withIndex()) {
}
}
\ No newline at end of file
diff --git a/idea/testData/intentions/addForLoopIndicesIntention/inapplicableOverridenFunction.kt b/idea/testData/intentions/addForLoopIndicesIntention/inapplicableOverridenFunction.kt
index c16ceeb8a97..5ce28ace4b3 100644
--- a/idea/testData/intentions/addForLoopIndicesIntention/inapplicableOverridenFunction.kt
+++ b/idea/testData/intentions/addForLoopIndicesIntention/inapplicableOverridenFunction.kt
@@ -1,6 +1,6 @@
// WITH_RUNTIME
// IS_APPLICABLE: FALSE
-fun String.withIndices(): Int = 42
+fun String.withIndex(): Int = 42
fun foo(s: String) {
for (a in s) {
diff --git a/idea/testData/intentions/addForLoopIndicesIntention/intArray.kt.after b/idea/testData/intentions/addForLoopIndicesIntention/intArray.kt.after
index 317eb113f87..b4ab124628b 100644
--- a/idea/testData/intentions/addForLoopIndicesIntention/intArray.kt.after
+++ b/idea/testData/intentions/addForLoopIndicesIntention/intArray.kt.after
@@ -1,6 +1,6 @@
//WITH_RUNTIME
fun foo(bar: IntArray) {
- for ((index, a) in bar.withIndices()) {
+ for ((index, a) in bar.withIndex()) {
}
}
\ No newline at end of file
diff --git a/idea/testData/intentions/addForLoopIndicesIntention/iterable.kt.after b/idea/testData/intentions/addForLoopIndicesIntention/iterable.kt.after
index 4351c3856a9..902f7c2661f 100644
--- a/idea/testData/intentions/addForLoopIndicesIntention/iterable.kt.after
+++ b/idea/testData/intentions/addForLoopIndicesIntention/iterable.kt.after
@@ -1,6 +1,6 @@
//WITH_RUNTIME
fun foo(bar: Iterable) {
- for ((index, a) in bar.withIndices()) {
+ for ((index, a) in bar.withIndex()) {
}
}
\ No newline at end of file
diff --git a/idea/testData/intentions/addForLoopIndicesIntention/listWithType.kt.after b/idea/testData/intentions/addForLoopIndicesIntention/listWithType.kt.after
index 633543f3ffe..09d634c6844 100644
--- a/idea/testData/intentions/addForLoopIndicesIntention/listWithType.kt.after
+++ b/idea/testData/intentions/addForLoopIndicesIntention/listWithType.kt.after
@@ -1,7 +1,7 @@
// WITH_RUNTIME
fun a() {
val b = listOf(1,2,3,4,5)
- for ((index, c : Int) in b.withIndices()) {
+ for ((index, c : Int) in b.withIndex()) {
}
}
\ No newline at end of file
diff --git a/idea/testData/intentions/addForLoopIndicesIntention/objectArray.kt.after b/idea/testData/intentions/addForLoopIndicesIntention/objectArray.kt.after
index 07f226b0945..3a439a7c204 100644
--- a/idea/testData/intentions/addForLoopIndicesIntention/objectArray.kt.after
+++ b/idea/testData/intentions/addForLoopIndicesIntention/objectArray.kt.after
@@ -1,6 +1,6 @@
//WITH_RUNTIME
fun foo(bar: Array