PSI Pattern Matching: Prefer strongly matched children over weakly matched parents
This commit is contained in:
@@ -25,6 +25,8 @@ import com.intellij.psi.PsiComment
|
|||||||
import java.util.Collections
|
import java.util.Collections
|
||||||
import org.jetbrains.jet.lang.psi.psiUtil.siblings
|
import org.jetbrains.jet.lang.psi.psiUtil.siblings
|
||||||
import com.intellij.openapi.util.TextRange
|
import com.intellij.openapi.util.TextRange
|
||||||
|
import java.util.HashSet
|
||||||
|
import org.jetbrains.jet.plugin.util.psi.patternMatching.JetPsiRange.Match
|
||||||
|
|
||||||
private val SIGNIFICANT_FILTER = { (e: PsiElement) -> e !is PsiWhiteSpace && e !is PsiComment && e.getTextLength() > 0 }
|
private val SIGNIFICANT_FILTER = { (e: PsiElement) -> e !is PsiWhiteSpace && e !is PsiComment && e.getTextLength() > 0 }
|
||||||
|
|
||||||
@@ -67,28 +69,25 @@ public trait JetPsiRange {
|
|||||||
val matches = ArrayList<Match>()
|
val matches = ArrayList<Match>()
|
||||||
scope.accept(
|
scope.accept(
|
||||||
object: JetTreeVisitorVoid() {
|
object: JetTreeVisitorVoid() {
|
||||||
private fun processElement(element: JetElement): Boolean {
|
override fun visitJetElement(element: JetElement) {
|
||||||
val candidates = element
|
val range = element
|
||||||
.siblings()
|
.siblings()
|
||||||
.filter(SIGNIFICANT_FILTER)
|
.filter(SIGNIFICANT_FILTER)
|
||||||
.take(elements.size)
|
.take(elements.size)
|
||||||
.toList()
|
.toList()
|
||||||
if (candidates.size != elements.size) return false
|
.toRange()
|
||||||
|
|
||||||
val range = candidates.toRange()
|
|
||||||
|
|
||||||
val result = unifier.unify(range, this@JetPsiRange)
|
val result = unifier.unify(range, this@JetPsiRange)
|
||||||
if (result is UnificationResult.Matched) {
|
|
||||||
|
if (result is UnificationResult.StronglyMatched) {
|
||||||
matches.add(Match(range, result))
|
matches.add(Match(range, result))
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
return false
|
val matchCountSoFar = matches.size
|
||||||
}
|
|
||||||
|
|
||||||
override fun visitJetElement(element: JetElement) {
|
|
||||||
if (!processElement(element)) {
|
|
||||||
super.visitJetElement(element)
|
super.visitJetElement(element)
|
||||||
|
if (result is UnificationResult.WeaklyMatched && matches.size == matchCountSoFar) {
|
||||||
|
matches.add(Match(range, result))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
// SIBLING:
|
||||||
|
fun main(args: Array<String>) {
|
||||||
|
val x = 1 + 1
|
||||||
|
val y = <selection>1 + 1</selection>
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
// SIBLING:
|
||||||
|
fun main(args: Array<String>) {
|
||||||
|
val x = i()
|
||||||
|
val y = i()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun i(): Int {
|
||||||
|
return 1 + 1
|
||||||
|
}
|
||||||
+7
-1
@@ -1086,7 +1086,13 @@ public class JetExtractionTestGenerated extends AbstractJetExtractionTest {
|
|||||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/extractFunction/duplicates/multipleOutputValuesMatching.kt");
|
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/extractFunction/duplicates/multipleOutputValuesMatching.kt");
|
||||||
doExtractFunctionTest(fileName);
|
doExtractFunctionTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("nestedStrongMatch.kt")
|
||||||
|
public void testNestedStrongMatch() throws Exception {
|
||||||
|
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/extractFunction/duplicates/nestedStrongMatch.kt");
|
||||||
|
doExtractFunctionTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("notNullAssertion.kt")
|
@TestMetadata("notNullAssertion.kt")
|
||||||
public void testNotNullAssertion() throws Exception {
|
public void testNotNullAssertion() throws Exception {
|
||||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/extractFunction/duplicates/notNullAssertion.kt");
|
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/extractFunction/duplicates/notNullAssertion.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user