SSR: cleanup

This commit is contained in:
Bas Leijdekkers
2017-06-04 12:32:11 +02:00
parent 19dc490e2d
commit 63c7f28fd1
2 changed files with 34 additions and 29 deletions
@@ -1,3 +1,18 @@
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.structuralsearch.impl.matcher;
import com.intellij.psi.PsiElement;
@@ -8,9 +23,6 @@ import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* Class describing the match result
*/
public final class MatchResultImpl extends MatchResult {
private String name;
private SmartPsiPointer matchRef;
@@ -171,10 +183,6 @@ public final class MatchResultImpl extends MatchResult {
this.target = target;
}
public boolean isMatchImageNull() {
return matchImage==null;
}
@Override
public int getStart() {
return start;
@@ -100,28 +100,22 @@ public class SubstitutionHandler extends MatchingHandler {
predicate = handler;
}
// Matcher
public MatchPredicate getPredicate() {
return predicate;
}
private static boolean validateOneMatch(final PsiElement match, int start, int end, final MatchResultImpl result, final MatchContext matchContext) {
final boolean matchresult;
if (match!=null) {
if (start==0 && end==-1 && result.getStart()==0 && result.getEnd()==-1) {
matchresult = matchContext.getMatcher().match(match,result.getMatch());
} else {
matchresult = StructuralSearchUtil.getProfileByPsiElement(match).getText(match, start, end).equals(
result.getMatchImage()
);
private static boolean validateOneMatch(final PsiElement match, int start, int end, final MatchResult result, final MatchContext matchContext) {
if (match != null) {
if (start == 0 && end == -1 && result.getStart() == 0 && result.getEnd() == -1) {
return matchContext.getMatcher().match(match, result.getMatch());
}
else {
return StructuralSearchUtil.getProfileByPsiElement(match).getText(match, start, end).equals(result.getMatchImage());
}
} else {
matchresult = result.isMatchImageNull();
}
return matchresult;
else {
return result.getMatchImage() == null;
}
}
public boolean validate(final PsiElement match, int start, int end, MatchContext context) {
@@ -134,7 +128,7 @@ public class SubstitutionHandler extends MatchingHandler {
return false;
}
MatchResultImpl result = context.hasResult() ? context.getResult().findSon(name) : null;
MatchResult result = context.hasResult() ? context.getResult().findSon(name) : null;
if (result == null && context.getPreviousResult() != null) {
result = context.getPreviousResult().findSon(name);
@@ -151,7 +145,7 @@ public class SubstitutionHandler extends MatchingHandler {
return false;
}
if (size != 0) {
result = (MatchResultImpl)result.getAllSons().get(matchedOccurs);
result = result.getAllSons().get(matchedOccurs);
}
}
// check if they are the same
@@ -162,6 +156,7 @@ public class SubstitutionHandler extends MatchingHandler {
return true;
}
@Override
public boolean match(final PsiElement node, final PsiElement match, MatchContext context) {
if (!super.match(node,match,context)) return false;
@@ -174,7 +169,7 @@ public class SubstitutionHandler extends MatchingHandler {
return handle(match,0,-1,context);
}
public void addResult(PsiElement match,int start, int end,MatchContext context) {
public void addResult(PsiElement match, int start, int end, MatchContext context) {
if (totalMatchedOccurs == -1) {
final MatchResultImpl matchResult = context.getResult();
final MatchResultImpl substitution = matchResult.findSon(name);
@@ -195,9 +190,7 @@ public class SubstitutionHandler extends MatchingHandler {
target
);
substitution.setMatchRef(
new SmartPsiPointer(match == null ? null : match)
);
substitution.setMatchRef(new SmartPsiPointer(match));
substitution.setMultipleMatch(true);
@@ -312,6 +305,7 @@ public class SubstitutionHandler extends MatchingHandler {
}
}
@Override
public boolean matchInAnyOrder(NodeIterator patternNodes, NodeIterator matchedNodes, final MatchContext context) {
final MatchResultImpl saveResult = context.hasResult() ? context.getResult() : null;
context.setResult(null);
@@ -387,6 +381,7 @@ public class SubstitutionHandler extends MatchingHandler {
}
}
@Override
public boolean matchSequentially(NodeIterator nodes, NodeIterator nodes2, MatchContext context) {
return doMatchSequentially(nodes, nodes2, context);
}
@@ -550,11 +545,13 @@ public class SubstitutionHandler extends MatchingHandler {
return name;
}
@Override
public void reset() {
super.reset();
totalMatchedOccurs = -1;
}
@Override
public boolean shouldAdvanceThePatternFor(PsiElement patternElement, PsiElement matchedElement) {
if(maxOccurs > 1) return false;
return super.shouldAdvanceThePatternFor(patternElement,matchedElement);