KT-4820: Added new tests, introduced functionality to detect overridden functions, other minor fixes

This commit is contained in:
Ross Hanson
2014-05-09 05:36:46 -04:00
parent 35c5a62b11
commit bbbdfd25d9
30 changed files with 170 additions and 75 deletions
@@ -1,3 +1,3 @@
for ((i,x) in foo.withIndices()) { for ((i, x) in foo.withIndices()) {
} }
@@ -23,18 +23,13 @@ import org.jetbrains.jet.lang.psi.JetDotQualifiedExpression
import com.intellij.psi.util.PsiTreeUtil import com.intellij.psi.util.PsiTreeUtil
import com.intellij.psi.PsiDocumentManager import com.intellij.psi.PsiDocumentManager
import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache
import org.jetbrains.jet.di.InjectorForMacros
import org.jetbrains.jet.lang.resolve.BindingContext import org.jetbrains.jet.lang.resolve.BindingContext
import org.jetbrains.jet.lang.types.TypeUtils
import org.jetbrains.jet.lang.resolve.ObservableBindingTrace
import org.jetbrains.jet.lang.resolve.BindingTraceContext
import org.jetbrains.jet.lang.resolve.calls.autocasts.DataFlowInfo
import org.jetbrains.jet.lang.psi.JetParenthesizedExpression import org.jetbrains.jet.lang.psi.JetParenthesizedExpression
import com.intellij.codeInsight.template.TemplateBuilderImpl import com.intellij.codeInsight.template.TemplateBuilderImpl
import com.intellij.codeInsight.template.impl.TemplateManagerImpl import com.intellij.codeInsight.template.impl.TemplateManagerImpl
import org.jetbrains.jet.lang.resolve.DescriptorUtils import org.jetbrains.jet.lang.resolve.DescriptorUtils
import org.jetbrains.jet.lang.descriptors.ModuleDescriptor import org.jetbrains.jet.analyzer.analyzeInContext
import org.jetbrains.jet.lang.psi.JetCallExpression
public class AddForLoopIndicesIntention : JetSelfTargetingIntention<JetForExpression>( public class AddForLoopIndicesIntention : JetSelfTargetingIntention<JetForExpression>(
"add.for.loop.indices", javaClass()) { "add.for.loop.indices", javaClass()) {
@@ -69,19 +64,20 @@ public class AddForLoopIndicesIntention : JetSelfTargetingIntention<JetForExpres
if (element.getLoopParameter() == null) return false if (element.getLoopParameter() == null) return false
val range = element.getLoopRange() ?: return false val range = element.getLoopRange() ?: return false
if (range is JetDotQualifiedExpression) { if (range is JetDotQualifiedExpression) {
val selector = (range as JetDotQualifiedExpression).getSelectorExpression() ?: return true val selector = range.getSelectorExpression() ?: return true
if (selector.getText().equals("withIndices()")) return false if (selector.getText() == "withIndices()") return false
} }
val potentialExpression = JetPsiFactory.createExpression(element.getProject(), "${range.getText()}.withIndices()") val potentialExpression = JetPsiFactory.createExpression(element.getProject(),
"${range.getText()}.withIndices()") as JetDotQualifiedExpression
val bindingContext = AnalyzerFacadeWithCache.getContextForElement(element) val bindingContext = AnalyzerFacadeWithCache.getContextForElement(element)
val scope = bindingContext[BindingContext.RESOLUTION_SCOPE, element] ?: return false val scope = bindingContext[BindingContext.RESOLUTION_SCOPE, element] ?: return false
val module = DescriptorUtils.getParentOfType(scope.getContainingDeclaration(), javaClass<ModuleDescriptor>())!! val functionSelector = potentialExpression.getSelectorExpression() as JetCallExpression
val components = InjectorForMacros(element.getProject(), module) val updatedContext = potentialExpression.analyzeInContext(scope)
val dataFlowInfo = bindingContext[BindingContext.NON_DEFAULT_EXPRESSION_DATA_FLOW, element] ?: DataFlowInfo.EMPTY val callScope = updatedContext[BindingContext.RESOLVED_CALL, functionSelector.getCalleeExpression()!!] ?: return false
val bindingTrace = ObservableBindingTrace(BindingTraceContext()) val callFqName = DescriptorUtils.getFqNameSafe(callScope.getCandidateDescriptor())
val expressionType = components.getExpressionTypingServices()!!.getTypeInfo(scope, potentialExpression, TypeUtils.NO_EXPECTED_TYPE, dataFlowInfo, bindingTrace) return callFqName.toString() == "kotlin.withIndices"
return expressionType.getType() != null
} }
} }
@@ -18,25 +18,16 @@ package org.jetbrains.jet.plugin.intentions
import org.jetbrains.jet.lang.psi.JetForExpression import org.jetbrains.jet.lang.psi.JetForExpression
import com.intellij.openapi.editor.Editor import com.intellij.openapi.editor.Editor
import org.jetbrains.jet.lang.psi.JetPsiFactory
import org.jetbrains.jet.lang.psi.JetDotQualifiedExpression import org.jetbrains.jet.lang.psi.JetDotQualifiedExpression
import org.jetbrains.jet.lang.psi.JetPsiUtil import org.jetbrains.jet.lang.psi.JetPsiUtil
import org.jetbrains.jet.lang.cfg.PseudocodeVariablesData.VariableUseState
import org.jetbrains.jet.lang.cfg.pseudocode.PseudocodeUtil
import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache
import org.jetbrains.jet.lang.psi.JetFile
import org.jetbrains.jet.lang.resolve.ObservableBindingTrace
import org.jetbrains.jet.lang.resolve.BindingTraceContext
import org.jetbrains.jet.lang.cfg.JetFlowInformationProvider
import org.jetbrains.jet.lang.cfg.JetControlFlowProcessor
import com.intellij.debugger.jdi.LocalVariablesUtil
import com.siyeh.ig.psiutils.VariableAccessUtils
import com.google.dart.compiler.util.AstUtil
import com.intellij.find.FindManager import com.intellij.find.FindManager
import com.intellij.find.impl.FindManagerImpl import com.intellij.find.impl.FindManagerImpl
import com.intellij.usageView.UsageInfo import com.intellij.usageView.UsageInfo
import com.intellij.util.Processor
import org.jetbrains.jet.plugin.findUsages.KotlinPropertyFindUsagesOptions 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
public class RemoveForLoopIndicesIntention : JetSelfTargetingIntention<JetForExpression>( public class RemoveForLoopIndicesIntention : JetSelfTargetingIntention<JetForExpression>(
"remove.for.loop.indices", javaClass()) { "remove.for.loop.indices", javaClass()) {
@@ -46,7 +37,8 @@ public class RemoveForLoopIndicesIntention : JetSelfTargetingIntention<JetForExp
val parameters = parameter.getEntries() val parameters = parameter.getEntries()
if (parameters.size() == 2) { if (parameters.size() == 2) {
parameter.replace(parameters[1]) parameter.replace(parameters[1])
} else { }
else {
JetPsiUtil.deleteElementWithDelimiters(parameters[0]) JetPsiUtil.deleteElementWithDelimiters(parameters[0])
} }
@@ -54,30 +46,25 @@ public class RemoveForLoopIndicesIntention : JetSelfTargetingIntention<JetForExp
} }
override fun isApplicableTo(element: JetForExpression): Boolean { override fun isApplicableTo(element: JetForExpression): Boolean {
if (element.getMultiParameter() == null) return false val multiParameter = element.getMultiParameter() ?: return false
val range = element.getLoopRange() as? JetDotQualifiedExpression ?: return false val range = element.getLoopRange() as? JetDotQualifiedExpression ?: return false
val selector = range.getSelectorExpression() ?: return false val selector = range.getSelectorExpression() as? JetCallExpression ?: return false
if (!selector.textMatches("withIndices()")) return false if (!selector.textMatches("withIndices()")) return false
val indexVar = element.getMultiParameter()!!.getEntries()[0] val bindingContext = AnalyzerFacadeWithCache.getContextForElement(element)
val callResolution = bindingContext[BindingContext.RESOLVED_CALL, selector.getCalleeExpression()!!] ?: return false
val fqName = DescriptorUtils.getFqNameSafe(callResolution.getCandidateDescriptor())
if (fqName.toString() != "kotlin.withIndices") return false
val indexVar = multiParameter.getEntries()[0]
val findManager = FindManager.getInstance(element.getProject()) as FindManagerImpl val findManager = FindManager.getInstance(element.getProject()) as FindManagerImpl
val findHandler = findManager.getFindUsagesManager().getFindUsagesHandler(indexVar,false) ?: return false val findHandler = findManager.getFindUsagesManager().getFindUsagesHandler(indexVar, false) ?: return false
val options = KotlinPropertyFindUsagesOptions(element.getProject()) val options = KotlinPropertyFindUsagesOptions(element.getProject())
val usageCount = array(0) var usageCount = 0
val usageFinderRunnable = object : Runnable { val processorLambda: (UsageInfo?) -> Boolean = { t -> usageCount++; false }
override fun run() { findHandler.processElementUsages(indexVar, processorLambda, options)
val processor = object : Processor<UsageInfo> { return usageCount == 0
override fun process(t: UsageInfo?): Boolean {
usageCount[0] = usageCount[0] + 1
return true
}
}
findHandler.processElementUsages(indexVar,processor,options)
}
}
usageFinderRunnable.run()
return usageCount[0] == 0
} }
} }
@@ -1,5 +1,5 @@
//IS_APPLICABLE: FALSE //IS_APPLICABLE: FALSE
//ERROR: Unresolved reference: withIndices // WITH_RUNTIME
fun b(c: List<String>) { fun b(c: List<String>) {
for ((<caret>indexVariable, d) in c.withIndices()) { for ((<caret>indexVariable, d) in c.withIndices()) {
@@ -0,0 +1,9 @@
// WITH_RUNTIME
// IS_APPLICABLE: FALSE
fun String.withIndices(): Int = 42
fun foo(s: String) {
for (<caret>a in s) {
}
}
@@ -0,0 +1,7 @@
//IS_APPLICABLE: FALSE
//ERROR: Unresolved reference: b
fun foo() {
for (a <caret>in b) {
}
}
@@ -1,5 +1,5 @@
// IS_APPLICABLE: FALSE // IS_APPLICABLE: FALSE
// ERROR: For-loop range must have an iterator() method // WITH_RUNTIME
fun foo(bar: Map<String, String>) { fun foo(bar: Map<String, String>) {
for (a <caret>in bar) { for (a <caret>in bar) {
@@ -0,0 +1,6 @@
//WITH_RUNTIME
fun foo(bar: IntArray) {
for (<caret>a in bar) {
}
}
@@ -0,0 +1,6 @@
//WITH_RUNTIME
fun foo(bar: IntArray) {
for ((index, a) in bar.withIndices()) {
}
}
@@ -0,0 +1,6 @@
//WITH_RUNTIME
fun foo(bar: Iterable<Int>) {
for (<caret>a in bar) {
}
}
@@ -0,0 +1,6 @@
//WITH_RUNTIME
fun foo(bar: Iterable<Int>) {
for ((index, a) in bar.withIndices()) {
}
}
@@ -1,4 +1,4 @@
// ERROR: Unresolved reference: listOf // WITH_RUNTIME
fun a() { fun a() {
val b = listOf(1,2,3,4,5) val b = listOf(1,2,3,4,5)
for (<caret>c : Int in b) { for (<caret>c : Int in b) {
@@ -1,4 +1,4 @@
// ERROR: Unresolved reference: listOf // WITH_RUNTIME
fun a() { fun a() {
val b = listOf(1,2,3,4,5) val b = listOf(1,2,3,4,5)
for ((index, c : Int) in b.withIndices()) { for ((index, c : Int) in b.withIndices()) {
@@ -0,0 +1,6 @@
//WITH_RUNTIME
fun foo(bar: Array<Object>) {
for (<caret>a in bar) {
}
}
@@ -0,0 +1,6 @@
//WITH_RUNTIME
fun foo(bar: Array<Object>) {
for ((index, a) in bar.withIndices()) {
}
}
@@ -1,4 +1,4 @@
// ERROR: Unresolved reference: listOf // WITH_RUNTIME
fun a() { fun a() {
val b = listOf(1,2,3,4,5) val b = listOf(1,2,3,4,5)
for (<caret>c in b) { for (<caret>c in b) {
@@ -1,4 +1,4 @@
// ERROR: Unresolved reference: listOf // WITH_RUNTIME
fun a() { fun a() {
val b = listOf(1,2,3,4,5) val b = listOf(1,2,3,4,5)
for ((index, c) in b.withIndices()) { for ((index, c) in b.withIndices()) {
@@ -1,6 +0,0 @@
//ERROR: Unresolved reference: SortedMap
fun a(b: SortedMap<Int, String>) {
for (<caret>c in b) {
}
}
@@ -1,6 +0,0 @@
//ERROR: Unresolved reference: SortedMap
fun a(b: SortedMap<Int, String>) {
for ((index, c) in b.withIndices()) {
}
}
@@ -0,0 +1,6 @@
//WITH_RUNTIME
fun foo(bar: Stream<String>) {
for (<caret>a in bar) {
}
}
@@ -0,0 +1,6 @@
//WITH_RUNTIME
fun foo(bar: Stream<String>) {
for ((index, a) in bar.withIndices()) {
}
}
@@ -0,0 +1,6 @@
//WITH_RUNTIME
fun foo(bar: String) {
for (<caret>a in bar) {
}
}
@@ -0,0 +1,6 @@
//WITH_RUNTIME
fun foo(bar: String) {
for ((index, a) in bar.withIndices()) {
}
}
@@ -1,7 +1,9 @@
// IS_APPLICABLE: FALSE // IS_APPLICABLE: FALSE
//ERROR: Unresolved reference: withIndices //WITH_RUNTIME
fun foo(b: List<Int>) {
fun foo(b: List<Int>) : Int {
for ((i, <caret>c) in b.withIndices()) { for ((i, <caret>c) in b.withIndices()) {
return i return i
} }
return 0
} }
@@ -0,0 +1,11 @@
// WITH_RUNTIME
// IS_APPLICABLE: FALSE
import java.util.LinkedList
fun Int.withIndices(): List<Pair<Int, Int>> = LinkedList<Pair<Int, Int>>()
fun foo(s: Int) {
for ((index<caret>, a) in s.withIndices()) {
}
}
@@ -1,4 +1,4 @@
//ERROR: Unresolved reference: withIndices //WITH_RUNTIME
fun foo(bar: List<Int>) { fun foo(bar: List<Int>) {
for ((i : <caret>Int, b: Int) in bar.withIndices()) { for ((i : <caret>Int, b: Int) in bar.withIndices()) {
@@ -1,4 +1,4 @@
//ERROR: Unresolved reference: withIndices //WITH_RUNTIME
fun foo(bar: List<Int>) { fun foo(bar: List<Int>) {
for (b: Int in bar) { for (b: Int in bar) {
@@ -1,4 +1,4 @@
//ERROR: Unresolved reference: withIndices //WITH_RUNTIME
fun foo(bar: List<String>) { fun foo(bar: List<String>) {
for ((i,<caret>a) in bar.withIndices()) { for ((i,<caret>a) in bar.withIndices()) {
@@ -1,4 +1,4 @@
//ERROR: Unresolved reference: withIndices //WITH_RUNTIME
fun foo(bar: List<String>) { fun foo(bar: List<String>) {
for (a in bar) { for (a in bar) {
@@ -4252,24 +4252,54 @@ public class CodeTransformationTestGenerated extends AbstractCodeTransformationT
doTestAddForLoopIndicesIntention("idea/testData/intentions/addForLoopIndicesIntention/inapplicableExistingIndices.kt"); doTestAddForLoopIndicesIntention("idea/testData/intentions/addForLoopIndicesIntention/inapplicableExistingIndices.kt");
} }
@TestMetadata("inapplicableOverridenFunction.kt")
public void testInapplicableOverridenFunction() throws Exception {
doTestAddForLoopIndicesIntention("idea/testData/intentions/addForLoopIndicesIntention/inapplicableOverridenFunction.kt");
}
@TestMetadata("inapplicableSyntaxError.kt")
public void testInapplicableSyntaxError() throws Exception {
doTestAddForLoopIndicesIntention("idea/testData/intentions/addForLoopIndicesIntention/inapplicableSyntaxError.kt");
}
@TestMetadata("inapplicableUnorderedCollection.kt") @TestMetadata("inapplicableUnorderedCollection.kt")
public void testInapplicableUnorderedCollection() throws Exception { public void testInapplicableUnorderedCollection() throws Exception {
doTestAddForLoopIndicesIntention("idea/testData/intentions/addForLoopIndicesIntention/inapplicableUnorderedCollection.kt"); doTestAddForLoopIndicesIntention("idea/testData/intentions/addForLoopIndicesIntention/inapplicableUnorderedCollection.kt");
} }
@TestMetadata("intArray.kt")
public void testIntArray() throws Exception {
doTestAddForLoopIndicesIntention("idea/testData/intentions/addForLoopIndicesIntention/intArray.kt");
}
@TestMetadata("iterable.kt")
public void testIterable() throws Exception {
doTestAddForLoopIndicesIntention("idea/testData/intentions/addForLoopIndicesIntention/iterable.kt");
}
@TestMetadata("listWithType.kt") @TestMetadata("listWithType.kt")
public void testListWithType() throws Exception { public void testListWithType() throws Exception {
doTestAddForLoopIndicesIntention("idea/testData/intentions/addForLoopIndicesIntention/listWithType.kt"); doTestAddForLoopIndicesIntention("idea/testData/intentions/addForLoopIndicesIntention/listWithType.kt");
} }
@TestMetadata("objectArray.kt")
public void testObjectArray() throws Exception {
doTestAddForLoopIndicesIntention("idea/testData/intentions/addForLoopIndicesIntention/objectArray.kt");
}
@TestMetadata("simpleIntList.kt") @TestMetadata("simpleIntList.kt")
public void testSimpleIntList() throws Exception { public void testSimpleIntList() throws Exception {
doTestAddForLoopIndicesIntention("idea/testData/intentions/addForLoopIndicesIntention/simpleIntList.kt"); doTestAddForLoopIndicesIntention("idea/testData/intentions/addForLoopIndicesIntention/simpleIntList.kt");
} }
@TestMetadata("simpleSortedMap.kt") @TestMetadata("stream.kt")
public void testSimpleSortedMap() throws Exception { public void testStream() throws Exception {
doTestAddForLoopIndicesIntention("idea/testData/intentions/addForLoopIndicesIntention/simpleSortedMap.kt"); doTestAddForLoopIndicesIntention("idea/testData/intentions/addForLoopIndicesIntention/stream.kt");
}
@TestMetadata("string.kt")
public void testString() throws Exception {
doTestAddForLoopIndicesIntention("idea/testData/intentions/addForLoopIndicesIntention/string.kt");
} }
} }
@@ -4290,6 +4320,11 @@ public class CodeTransformationTestGenerated extends AbstractCodeTransformationT
doTestRemoveForLoopIndicesIntention("idea/testData/intentions/removeForLoopIndicesIntention/inapplicableIndexUse.kt"); doTestRemoveForLoopIndicesIntention("idea/testData/intentions/removeForLoopIndicesIntention/inapplicableIndexUse.kt");
} }
@TestMetadata("inapplicableOverridenFunction.kt")
public void testInapplicableOverridenFunction() throws Exception {
doTestRemoveForLoopIndicesIntention("idea/testData/intentions/removeForLoopIndicesIntention/inapplicableOverridenFunction.kt");
}
@TestMetadata("loopWithType.kt") @TestMetadata("loopWithType.kt")
public void testLoopWithType() throws Exception { public void testLoopWithType() throws Exception {
doTestRemoveForLoopIndicesIntention("idea/testData/intentions/removeForLoopIndicesIntention/loopWithType.kt"); doTestRemoveForLoopIndicesIntention("idea/testData/intentions/removeForLoopIndicesIntention/loopWithType.kt");