mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
SSR: more lenient if statement matching
This commit is contained in:
+2
-1
@@ -1205,9 +1205,10 @@ public class JavaMatchingVisitor extends JavaElementVisitor {
|
||||
public void visitIfStatement(final PsiIfStatement if1) {
|
||||
final PsiIfStatement if2 = (PsiIfStatement)myMatchingVisitor.getElement();
|
||||
|
||||
final PsiStatement elseBranch = if1.getElseBranch();
|
||||
myMatchingVisitor.setResult(myMatchingVisitor.match(if1.getCondition(), if2.getCondition()) &&
|
||||
compareBody(if1.getThenBranch(), if2.getThenBranch()) &&
|
||||
compareBody(if1.getElseBranch(), if2.getElseBranch()));
|
||||
(elseBranch == null || compareBody(elseBranch, if2.getElseBranch())));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+5
-1
@@ -4,6 +4,7 @@ import com.intellij.openapi.fileTypes.FileType;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.structuralsearch.MatchOptions;
|
||||
import com.intellij.structuralsearch.MatchResult;
|
||||
import com.intellij.structuralsearch.Matcher;
|
||||
@@ -35,6 +36,9 @@ public class WithinPredicate extends AbstractStringBasedPredicate {
|
||||
|
||||
public boolean match(PsiElement node, PsiElement match, int start, int end, MatchContext context) {
|
||||
final MatchResult result = matcher.isMatchedByDownUp(match, myMatchOptions);
|
||||
return result != null;
|
||||
if (result == null) {
|
||||
return false;
|
||||
}
|
||||
return PsiTreeUtil.isAncestor(result.getMatch(), match, false);
|
||||
}
|
||||
}
|
||||
+12
@@ -842,6 +842,18 @@ public class StructuralSearchTest extends StructuralSearchTestCase {
|
||||
"return !(x);";
|
||||
String s10034 = "return ('a);";
|
||||
assertEquals("Find statement with parenthesized expr",2,findMatchesCount(s10033,s10034));
|
||||
|
||||
String in = "if (true) {" +
|
||||
" System.out.println();" +
|
||||
"} else {" +
|
||||
" System.out.println();" +
|
||||
"}" +
|
||||
"if (true) System.out.println();";
|
||||
String pattern1 = "if ('_exp) { '_statement*; }";
|
||||
assertEquals("Find if statement with else", 2, findMatchesCount(in, pattern1));
|
||||
|
||||
String pattern2 = "if ('_exp) { '_statement*; } else { '_statement2{0,0}; }";
|
||||
assertEquals("Find if statement without else", 1, findMatchesCount(in, pattern2));
|
||||
}
|
||||
|
||||
public void testSearchClass() {
|
||||
|
||||
Reference in New Issue
Block a user