Commit 7febd8f6 authored by Bas Leijdekkers's avatar Bas Leijdekkers
Browse files

SSR: move findRegExpPredicate() to a better place

parent 0d91685e
Showing with 37 additions and 38 deletions
+37 -38
......@@ -787,7 +787,7 @@ public class JavaMatchingVisitor extends JavaElementVisitor {
}
}
else if (matchedArrayDimensions != 0) {
regExpPredicate = MatchingHandler.getSimpleRegExpPredicate(handler);
regExpPredicate = handler.findRegExpPredicate();
if (regExpPredicate != null) {
regExpPredicate.setNodeTextGenerator(new RegExpPredicate.NodeTextGenerator() {
......
......@@ -118,7 +118,7 @@ public class JavaCompilingVisitor extends JavaRecursiveElementWalkingVisitor {
return false;
}
final RegExpPredicate predicate = MatchingHandler.getSimpleRegExpPredicate(handler);
final RegExpPredicate predicate = handler.findRegExpPredicate();
if (predicate != null && predicate.couldBeOptimized()) {
if (handler.isStrictSubtype() || handler.isSubtype()) {
addDescendantsOf(predicate.getRegExp(), handler.isSubtype(), compileContext);
......@@ -213,7 +213,7 @@ public class JavaCompilingVisitor extends JavaRecursiveElementWalkingVisitor {
((RegExpPredicate)handler.getPredicate()).setMultiline(true);
}
RegExpPredicate predicate = MatchingHandler.getSimpleRegExpPredicate(handler);
RegExpPredicate predicate = handler.findRegExpPredicate();
if (GlobalCompilingVisitor.isSuitablePredicate(predicate, handler)) {
myCompilingVisitor.processTokenizedName(predicate.getRegExp(), true, GlobalCompilingVisitor.OccurenceKind.COMMENT);
}
......
......@@ -4,6 +4,7 @@ package com.intellij.structuralsearch.impl.matcher.compiler;
import com.intellij.dupLocator.util.NodeFilter;
import com.intellij.openapi.extensions.Extensions;
import com.intellij.psi.PsiElement;
import com.intellij.structuralsearch.MalformedPatternException;
import com.intellij.structuralsearch.StructuralSearchProfile;
import com.intellij.structuralsearch.StructuralSearchUtil;
import com.intellij.structuralsearch.impl.matcher.filters.CompositeFilter;
......@@ -151,19 +152,14 @@ public class GlobalCompilingVisitor {
@NonNls StringBuilder buf = new StringBuilder(content.length());
Matcher matcher = substitutionPattern.matcher(content);
List<SubstitutionHandler> handlers = null;
List<SubstitutionHandler> handlers = new SmartList<>();
int start = 0;
String word;
boolean hasLiteralContent = false;
SubstitutionHandler handler = null;
while (matcher.find()) {
if (handlers == null) handlers = new SmartList<>();
handler = (SubstitutionHandler)getContext().getPattern().getHandler(matcher.group(1));
if (handler != null) handlers.add(handler);
word = content.substring(start, matcher.start());
if (!word.isEmpty()) {
buf.append(StructuralSearchUtil.shieldRegExpMetaChars(word));
hasLiteralContent = true;
......@@ -171,7 +167,11 @@ public class GlobalCompilingVisitor {
processTokenizedName(word, false, kind);
}
RegExpPredicate predicate = MatchingHandler.getSimpleRegExpPredicate(handler);
handler = (SubstitutionHandler)getContext().getPattern().getHandler(matcher.group(1));
if (handler == null) throw new MalformedPatternException();
handlers.add(handler);
RegExpPredicate predicate = handler.findRegExpPredicate();
if (predicate == null || !predicate.isWholeWords()) {
buf.append("(.*?)");
......@@ -204,7 +204,7 @@ public class GlobalCompilingVisitor {
buf.append("$");
}
if (handlers != null) {
if (!handlers.isEmpty()) {
return hasLiteralContent ? new LiteralWithSubstitutionHandler(buf.toString(), handlers) : handler;
}
......
......@@ -10,7 +10,6 @@ import com.intellij.psi.xml.XmlText;
import com.intellij.psi.xml.XmlToken;
import com.intellij.structuralsearch.impl.matcher.CompiledPattern;
import com.intellij.structuralsearch.impl.matcher.filters.TagValueFilter;
import com.intellij.structuralsearch.impl.matcher.handlers.MatchingHandler;
import com.intellij.structuralsearch.impl.matcher.handlers.SubstitutionHandler;
import com.intellij.structuralsearch.impl.matcher.handlers.TopLevelMatchingHandler;
import com.intellij.structuralsearch.impl.matcher.predicates.RegExpPredicate;
......@@ -79,7 +78,7 @@ public class XmlCompilingVisitor extends XmlRecursiveElementVisitor {
return false;
}
final RegExpPredicate predicate = MatchingHandler.getSimpleRegExpPredicate(handler);
final RegExpPredicate predicate = handler.findRegExpPredicate();
if (predicate != null && predicate.couldBeOptimized()) {
GlobalCompilingVisitor.addFilesToSearchForGivenWord(predicate.getRegExp(), true, CODE, compileContext);
}
......
......@@ -10,10 +10,6 @@ import com.intellij.structuralsearch.impl.matcher.CompiledPattern;
import com.intellij.structuralsearch.impl.matcher.MatchContext;
import com.intellij.structuralsearch.impl.matcher.MatchResultImpl;
import com.intellij.structuralsearch.impl.matcher.filters.DefaultFilter;
import com.intellij.structuralsearch.impl.matcher.predicates.AndPredicate;
import com.intellij.structuralsearch.impl.matcher.predicates.MatchPredicate;
import com.intellij.structuralsearch.impl.matcher.predicates.NotPredicate;
import com.intellij.structuralsearch.impl.matcher.predicates.RegExpPredicate;
import com.intellij.structuralsearch.impl.matcher.strategies.MatchingStrategy;
import java.util.HashSet;
......@@ -104,27 +100,6 @@ public abstract class MatchingHandler {
return !nodes2.hasNext();
}
private static MatchPredicate findRegExpPredicate(MatchPredicate start) {
if (start==null) return null;
if (start instanceof RegExpPredicate) return start;
if(start instanceof AndPredicate) {
AndPredicate binary = (AndPredicate)start;
final MatchPredicate result = findRegExpPredicate(binary.getFirst());
if (result!=null) return result;
return findRegExpPredicate(binary.getSecond());
} else if (start instanceof NotPredicate) {
return null;
}
return null;
}
public static RegExpPredicate getSimpleRegExpPredicate(SubstitutionHandler handler) {
if (handler == null) return null;
return (RegExpPredicate)findRegExpPredicate(handler.getPredicate());
}
static class ClearStateVisitor extends PsiRecursiveElementWalkingVisitor {
private CompiledPattern pattern;
......
......@@ -11,9 +11,13 @@ import com.intellij.structuralsearch.StructuralSearchUtil;
import com.intellij.structuralsearch.impl.matcher.CompiledPattern;
import com.intellij.structuralsearch.impl.matcher.MatchContext;
import com.intellij.structuralsearch.impl.matcher.MatchResultImpl;
import com.intellij.structuralsearch.impl.matcher.predicates.AndPredicate;
import com.intellij.structuralsearch.impl.matcher.predicates.MatchPredicate;
import com.intellij.structuralsearch.impl.matcher.predicates.NotPredicate;
import com.intellij.structuralsearch.impl.matcher.predicates.RegExpPredicate;
import com.intellij.structuralsearch.plugin.ui.Configuration;
import com.intellij.structuralsearch.plugin.util.SmartPsiPointer;
import org.jetbrains.annotations.Nullable;
import java.util.HashSet;
import java.util.List;
......@@ -91,6 +95,27 @@ public class SubstitutionHandler extends MatchingHandler {
return predicate;
}
@Nullable
public RegExpPredicate findRegExpPredicate() {
return findRegExpPredicate(getPredicate());
}
private static RegExpPredicate findRegExpPredicate(MatchPredicate start) {
if (start==null) return null;
if (start instanceof RegExpPredicate) return (RegExpPredicate)start;
if(start instanceof AndPredicate) {
AndPredicate binary = (AndPredicate)start;
final RegExpPredicate result = findRegExpPredicate(binary.getFirst());
if (result!=null) return result;
return findRegExpPredicate(binary.getSecond());
} else if (start instanceof NotPredicate) {
return null;
}
return null;
}
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) {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment