From 7febd8f60dfdd4256ec93f6988085f54ca41fc25 Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Sat, 25 Nov 2017 16:27:32 +0100 Subject: [PATCH] SSR: move findRegExpPredicate() to a better place --- .../impl/matcher/JavaMatchingVisitor.java | 2 +- .../compiler/JavaCompilingVisitor.java | 4 +-- .../compiler/GlobalCompilingVisitor.java | 16 ++++++------ .../matcher/compiler/XmlCompilingVisitor.java | 3 +-- .../matcher/handlers/MatchingHandler.java | 25 ------------------- .../matcher/handlers/SubstitutionHandler.java | 25 +++++++++++++++++++ 6 files changed, 37 insertions(+), 38 deletions(-) diff --git a/java/structuralsearch-java/src/com/intellij/structuralsearch/impl/matcher/JavaMatchingVisitor.java b/java/structuralsearch-java/src/com/intellij/structuralsearch/impl/matcher/JavaMatchingVisitor.java index 5c505eeffe4e..cae2675f92df 100644 --- a/java/structuralsearch-java/src/com/intellij/structuralsearch/impl/matcher/JavaMatchingVisitor.java +++ b/java/structuralsearch-java/src/com/intellij/structuralsearch/impl/matcher/JavaMatchingVisitor.java @@ -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() { diff --git a/java/structuralsearch-java/src/com/intellij/structuralsearch/impl/matcher/compiler/JavaCompilingVisitor.java b/java/structuralsearch-java/src/com/intellij/structuralsearch/impl/matcher/compiler/JavaCompilingVisitor.java index 01ff26808bf6..0b832a8cf52e 100644 --- a/java/structuralsearch-java/src/com/intellij/structuralsearch/impl/matcher/compiler/JavaCompilingVisitor.java +++ b/java/structuralsearch-java/src/com/intellij/structuralsearch/impl/matcher/compiler/JavaCompilingVisitor.java @@ -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); } diff --git a/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/compiler/GlobalCompilingVisitor.java b/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/compiler/GlobalCompilingVisitor.java index 846bdafb9bc3..a1b1a17793ce 100644 --- a/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/compiler/GlobalCompilingVisitor.java +++ b/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/compiler/GlobalCompilingVisitor.java @@ -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 handlers = null; + List 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; } diff --git a/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/compiler/XmlCompilingVisitor.java b/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/compiler/XmlCompilingVisitor.java index 06957270a0f8..692633eada5f 100644 --- a/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/compiler/XmlCompilingVisitor.java +++ b/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/compiler/XmlCompilingVisitor.java @@ -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); } diff --git a/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/handlers/MatchingHandler.java b/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/handlers/MatchingHandler.java index 0528a27c77af..f939f4668154 100644 --- a/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/handlers/MatchingHandler.java +++ b/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/handlers/MatchingHandler.java @@ -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; diff --git a/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/handlers/SubstitutionHandler.java b/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/handlers/SubstitutionHandler.java index 060cadcb4b4a..3ea15c252613 100644 --- a/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/handlers/SubstitutionHandler.java +++ b/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/handlers/SubstitutionHandler.java @@ -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) {