Minor code improvements

This commit is contained in:
Valentin Kipyatkov
2014-11-19 21:57:14 +03:00
parent 8010c0f09d
commit 3e437adbb4
2 changed files with 18 additions and 24 deletions
@@ -70,6 +70,7 @@ class PartialBodyResolveFilter(
return statement in statementsToResolve return statement in statementsToResolve
} }
tailRecursive
private fun addStatementsToResolve(element: JetElement) { private fun addStatementsToResolve(element: JetElement) {
if (element == body) return if (element == body) return
val parent = element.getParent() as? JetElement ?: return val parent = element.getParent() as? JetElement ?: return
@@ -89,7 +90,7 @@ class PartialBodyResolveFilter(
val smartCastPlaces = potentialSmartCastPlaces(statement) val smartCastPlaces = potentialSmartCastPlaces(statement)
if (!smartCastPlaces.isEmpty()) { if (!smartCastPlaces.isEmpty()) {
statementsToResolve.add(statement) statementsToResolve.add(statement)
statementsToResolve.addStatementsForPlaces(statement, smartCastPlaces.values().flatMap { it }) statementsToResolve.addStatementsForPlaces(smartCastPlaces.values().flatMap { it })
} }
else if (statement is JetDeclaration) { else if (statement is JetDeclaration) {
statementsToResolve.add(statement) statementsToResolve.add(statement)
@@ -113,22 +114,12 @@ class PartialBodyResolveFilter(
val map = HashMap<String, ArrayList<JetExpression>>(0) val map = HashMap<String, ArrayList<JetExpression>>(0)
fun addPlace(name: String, place: JetExpression) { fun addPlace(name: String, place: JetExpression) {
var list = map[name] map.getOrPut(name, { ArrayList(1) }).add(place)
if (list == null) {
list = ArrayList(1)
map[name] = list
}
list!!.add(place)
} }
fun addPlaces(name: String, places: Collection<JetExpression>) { fun addPlaces(name: String, places: Collection<JetExpression>) {
assert(!places.isEmpty()) assert(!places.isEmpty())
var list = map[name] map.getOrPut(name, { ArrayList(places.size) }).addAll(places)
if (list == null) {
list = ArrayList(places.size)
map[name] = list
}
list!!.addAll(places)
} }
fun addIfCanBeSmartCasted(expression: JetExpression) { fun addIfCanBeSmartCasted(expression: JetExpression) {
@@ -138,7 +129,7 @@ class PartialBodyResolveFilter(
} }
} }
expression.accept(object : ControlFlowVisitor(){ expression.accept(object : ControlFlowVisitor() {
override fun visitPostfixExpression(expression: JetPostfixExpression) { override fun visitPostfixExpression(expression: JetPostfixExpression) {
expression.acceptChildren(this) expression.acceptChildren(this)
@@ -339,14 +330,11 @@ class PartialBodyResolveFilter(
private fun JetElement.noControlFlowInside() = this is JetFunction || this is JetClass || this is JetClassBody private fun JetElement.noControlFlowInside() = this is JetFunction || this is JetClass || this is JetClassBody
private fun MutableSet<JetExpression>.addStatementsForPlaces(thisStatement: JetExpression, places: Collection<JetExpression>) { private fun MutableSet<JetExpression>.addStatementsForPlaces(places: Collection<JetExpression>) {
@PlacesLoop
for (place in places) { for (place in places) {
var parent: PsiElement = place var parent: PsiElement = place
while (parent != thisStatement) { while (true) {
if (parent.isStatement()) { if (parent.isStatement() && !add(parent as JetExpression)) break
if (!add(parent as JetExpression)) continue@PlacesLoop
}
parent = parent.getParent() parent = parent.getParent()
} }
} }
@@ -398,6 +386,6 @@ class PartialBodyResolveFilter(
} }
private fun JetBlockExpression.lastStatement(): JetExpression? private fun JetBlockExpression.lastStatement(): JetExpression?
= getLastChild().siblings(forward = false).filterIsInstance<JetExpression>().firstOrNull() = getLastChild()?.siblings(forward = false)?.filterIsInstance<JetExpression>()?.firstOrNull()
} }
@@ -30,12 +30,12 @@ import java.io.File
import org.jetbrains.jet.lang.psi.JetBlockExpression import org.jetbrains.jet.lang.psi.JetBlockExpression
import org.junit.Assert import org.junit.Assert
import org.jetbrains.jet.lang.types.JetType import org.jetbrains.jet.lang.types.JetType
import org.jetbrains.jet.lang.psi.psiUtil.parents
import org.jetbrains.jet.renderer.DescriptorRenderer import org.jetbrains.jet.renderer.DescriptorRenderer
import org.jetbrains.jet.lang.descriptors.VariableDescriptor import org.jetbrains.jet.lang.descriptors.VariableDescriptor
import org.jetbrains.jet.lang.psi.JetSimpleNameExpression import org.jetbrains.jet.lang.psi.JetSimpleNameExpression
import org.jetbrains.jet.lang.psi.psiUtil.getReceiverExpression import org.jetbrains.jet.lang.psi.psiUtil.getReceiverExpression
import org.jetbrains.jet.plugin.caches.resolve.getResolutionFacade import org.jetbrains.jet.plugin.caches.resolve.getResolutionFacade
import org.jetbrains.jet.lang.psi.psiUtil.parents
public abstract class AbstractPartialBodyResolveTest : JetLightCodeInsightFixtureTestCase() { public abstract class AbstractPartialBodyResolveTest : JetLightCodeInsightFixtureTestCase() {
override fun getTestDataPath() = JetTestCaseBuilder.getHomeDirectory() override fun getTestDataPath() = JetTestCaseBuilder.getHomeDirectory()
@@ -76,7 +76,13 @@ public abstract class AbstractPartialBodyResolveTest : JetLightCodeInsightFixtur
Assert.assertEquals(target2.presentation(type2), target1.presentation(type1)) Assert.assertEquals(target2.presentation(type2), target1.presentation(type1))
} }
private fun doResolve(refExpression: JetSimpleNameExpression, bindingContext: BindingContext): Triple<DeclarationDescriptor?, JetType?, Collection<JetExpression>> { private data class ResolveData(
val target: DeclarationDescriptor?,
val type: JetType?,
val processedStatements: Collection<JetExpression>
)
private fun doResolve(refExpression: JetSimpleNameExpression, bindingContext: BindingContext): ResolveData {
val target = bindingContext[BindingContext.REFERENCE_TARGET, refExpression] val target = bindingContext[BindingContext.REFERENCE_TARGET, refExpression]
val processedStatements = bindingContext.getSliceContents(BindingContext.PROCESSED) val processedStatements = bindingContext.getSliceContents(BindingContext.PROCESSED)
@@ -93,7 +99,7 @@ public abstract class AbstractPartialBodyResolveTest : JetLightCodeInsightFixtur
} }
val type = bindingContext[BindingContext.EXPRESSION_TYPE, expressionWithType] val type = bindingContext[BindingContext.EXPRESSION_TYPE, expressionWithType]
return Triple(target, type, processedStatements) return ResolveData(target, type, processedStatements)
} }
private fun DeclarationDescriptor?.presentation(type: JetType?): String { private fun DeclarationDescriptor?.presentation(type: JetType?): String {