From 94f58c4b3d041e8bb06192612e352c2884426bcf Mon Sep 17 00:00:00 2001 From: Alexey Sedunov Date: Tue, 17 Nov 2015 16:21:31 +0300 Subject: [PATCH] Minor: Drop Match class. Move range to UnificationResult.Matched --- .../introduce/extractionEngine/extractorUtil.kt | 14 +++++++------- .../util/psi/patternMatching/KotlinPsiRange.kt | 10 ++++------ .../util/psi/patternMatching/KotlinPsiUnifier.kt | 9 +++++++-- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/extractionEngine/extractorUtil.kt b/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/extractionEngine/extractorUtil.kt index 2b3aef479b2..43ae4168e66 100644 --- a/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/extractionEngine/extractorUtil.kt +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/extractionEngine/extractorUtil.kt @@ -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 { - 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() fun matchValues(currentValue: OutputValue, newValue: OutputValue): Boolean { @@ -184,7 +184,7 @@ fun ExtractableCodeDescriptor.findDuplicates(): List { 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 { 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 { .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() } diff --git a/idea/src/org/jetbrains/kotlin/idea/util/psi/patternMatching/KotlinPsiRange.kt b/idea/src/org/jetbrains/kotlin/idea/util/psi/patternMatching/KotlinPsiRange.kt index 6c811b9a0c5..07d91f8c2b7 100644 --- a/idea/src/org/jetbrains/kotlin/idea/util/psi/patternMatching/KotlinPsiRange.kt +++ b/idea/src/org/jetbrains/kotlin/idea/util/psi/patternMatching/KotlinPsiRange.kt @@ -47,8 +47,6 @@ public interface KotlinPsiRange { } } - public class Match(val range: KotlinPsiRange, val result: UnificationResult.Matched) - val elements: List 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 { + fun match(scope: PsiElement, unifier: KotlinPsiUnifier): List { val elements = elements.filter(SIGNIFICANT_FILTER) if (elements.isEmpty()) return Collections.emptyList() - val matches = ArrayList() + val matches = ArrayList() 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) } } } diff --git a/idea/src/org/jetbrains/kotlin/idea/util/psi/patternMatching/KotlinPsiUnifier.kt b/idea/src/org/jetbrains/kotlin/idea/util/psi/patternMatching/KotlinPsiUnifier.kt index 587115a78fd..8a2eea70be8 100644 --- a/idea/src/org/jetbrains/kotlin/idea/util/psi/patternMatching/KotlinPsiUnifier.kt +++ b/idea/src/org/jetbrains/kotlin/idea/util/psi/patternMatching/KotlinPsiUnifier.kt @@ -68,13 +68,18 @@ public interface UnificationResult { } interface Matched: UnificationResult { + val range: KotlinPsiRange val substitution: Map override val status: Status get() = MATCHED } - class StronglyMatched(override val substitution: Map): Matched + class StronglyMatched( + override val range: KotlinPsiRange, + override val substitution: Map + ): Matched class WeaklyMatched( + override val range: KotlinPsiRange, override val substitution: Map, val weakMatches: Map ): 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 }