Minor: Drop Match class. Move range to UnificationResult.Matched

This commit is contained in:
Alexey Sedunov
2015-11-17 16:21:31 +03:00
parent 2d57aafef3
commit 94f58c4b3d
3 changed files with 18 additions and 15 deletions
@@ -35,8 +35,8 @@ import org.jetbrains.kotlin.idea.refactoring.introduce.extractionEngine.OutputVa
import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers
import org.jetbrains.kotlin.idea.util.ShortenReferences
import org.jetbrains.kotlin.idea.util.psi.patternMatching.KotlinPsiRange
import org.jetbrains.kotlin.idea.util.psi.patternMatching.KotlinPsiRange.Match
import org.jetbrains.kotlin.idea.util.psi.patternMatching.KotlinPsiUnifier
import org.jetbrains.kotlin.idea.util.psi.patternMatching.UnificationResult
import org.jetbrains.kotlin.idea.util.psi.patternMatching.UnificationResult.StronglyMatched
import org.jetbrains.kotlin.idea.util.psi.patternMatching.UnificationResult.WeaklyMatched
import org.jetbrains.kotlin.idea.util.psi.patternMatching.UnifierParameter
@@ -154,10 +154,10 @@ class DuplicateInfo(
)
fun ExtractableCodeDescriptor.findDuplicates(): List<DuplicateInfo> {
fun processWeakMatch(match: Match, newControlFlow: ControlFlow): Boolean {
fun processWeakMatch(match: UnificationResult.WeaklyMatched, newControlFlow: ControlFlow): Boolean {
val valueCount = controlFlow.outputValues.size
val weakMatches = HashMap((match.result as WeaklyMatched).weakMatches)
val weakMatches = HashMap(match.weakMatches)
val currentValuesToNew = HashMap<OutputValue, OutputValue>()
fun matchValues(currentValue: OutputValue, newValue: OutputValue): Boolean {
@@ -184,7 +184,7 @@ fun ExtractableCodeDescriptor.findDuplicates(): List<DuplicateInfo> {
return currentValuesToNew.size == valueCount && weakMatches.isEmpty()
}
fun getControlFlowIfMatched(match: Match): ControlFlow? {
fun getControlFlowIfMatched(match: UnificationResult.Matched): ControlFlow? {
val analysisResult = extractionData.copy(originalRange = match.range).performAnalysis()
if (analysisResult.status != AnalysisResult.Status.SUCCESS) return null
@@ -192,10 +192,10 @@ fun ExtractableCodeDescriptor.findDuplicates(): List<DuplicateInfo> {
if (newControlFlow.outputValues.isEmpty()) return newControlFlow
if (controlFlow.outputValues.size != newControlFlow.outputValues.size) return null
val matched = when (match.result) {
val matched = when (match) {
is StronglyMatched -> true
is WeaklyMatched -> processWeakMatch(match, newControlFlow)
else -> throw AssertionError("Unexpected unification result: ${match.result}")
else -> throw AssertionError("Unexpected unification result: $match")
}
return if (matched) newControlFlow else null
@@ -214,7 +214,7 @@ fun ExtractableCodeDescriptor.findDuplicates(): List<DuplicateInfo> {
.filter { !(it.range.getTextRange() intersects originalTextRange) }
.mapNotNull { match ->
val controlFlow = getControlFlowIfMatched(match)
controlFlow?.let { DuplicateInfo(match.range, it, unifierParameters.map { match.result.substitution[it]!!.text!! }) }
controlFlow?.let { DuplicateInfo(match.range, it, unifierParameters.map { match.substitution[it]!!.text!! }) }
}
.toList()
}
@@ -47,8 +47,6 @@ public interface KotlinPsiRange {
}
}
public class Match(val range: KotlinPsiRange, val result: UnificationResult.Matched)
val elements: List<PsiElement>
fun getTextRange(): TextRange
@@ -59,11 +57,11 @@ public interface KotlinPsiRange {
fun contains(element: PsiElement): Boolean = getTextRange().contains(element.getTextRange() ?: TextRange.EMPTY_RANGE)
fun match(scope: PsiElement, unifier: KotlinPsiUnifier): List<Match> {
fun match(scope: PsiElement, unifier: KotlinPsiUnifier): List<UnificationResult.Matched> {
val elements = elements.filter(SIGNIFICANT_FILTER)
if (elements.isEmpty()) return Collections.emptyList()
val matches = ArrayList<Match>()
val matches = ArrayList<UnificationResult.Matched>()
scope.accept(
object: KtTreeVisitorVoid() {
override fun visitKtElement(element: KtElement) {
@@ -77,13 +75,13 @@ public interface KotlinPsiRange {
val result = unifier.unify(range, this@KotlinPsiRange)
if (result is UnificationResult.StronglyMatched) {
matches.add(Match(range, result))
matches.add(result)
}
else {
val matchCountSoFar = matches.size()
super.visitKtElement(element)
if (result is UnificationResult.WeaklyMatched && matches.size() == matchCountSoFar) {
matches.add(Match(range, result))
matches.add(result)
}
}
}
@@ -68,13 +68,18 @@ public interface UnificationResult {
}
interface Matched: UnificationResult {
val range: KotlinPsiRange
val substitution: Map<UnifierParameter, KtExpression>
override val status: Status get() = MATCHED
}
class StronglyMatched(override val substitution: Map<UnifierParameter, KtExpression>): Matched
class StronglyMatched(
override val range: KotlinPsiRange,
override val substitution: Map<UnifierParameter, KtExpression>
): Matched
class WeaklyMatched(
override val range: KotlinPsiRange,
override val substitution: Map<UnifierParameter, KtExpression>,
val weakMatches: Map<KtElement, KtElement>
): Matched
@@ -824,7 +829,7 @@ public class KotlinPsiUnifier(
substitution.size() != descriptorToParameter.size() ->
Unmatched
status == MATCHED ->
if (weakMatches.isEmpty()) StronglyMatched(substitution) else WeaklyMatched(substitution, weakMatches)
if (weakMatches.isEmpty()) StronglyMatched(target, substitution) else WeaklyMatched(target, substitution, weakMatches)
else ->
Unmatched
}