diff options
Diffstat (limited to 'platform/structuralsearch/source/com/intellij/structuralsearch')
6 files changed, 42 insertions, 27 deletions
diff --git a/platform/structuralsearch/source/com/intellij/structuralsearch/StructuralSearchProfile.java b/platform/structuralsearch/source/com/intellij/structuralsearch/StructuralSearchProfile.java index e40683dce04e..bd3d773f26f5 100644 --- a/platform/structuralsearch/source/com/intellij/structuralsearch/StructuralSearchProfile.java +++ b/platform/structuralsearch/source/com/intellij/structuralsearch/StructuralSearchProfile.java @@ -184,9 +184,13 @@ public abstract class StructuralSearchProfile { return element.getClass(); } + @NotNull public String getTypedVarString(PsiElement element) { if (element instanceof PsiNamedElement) { - return ((PsiNamedElement)element).getName(); + final String name = ((PsiNamedElement)element).getName(); + if (name != null) { + return name; + } } return element.getText(); } diff --git a/platform/structuralsearch/source/com/intellij/structuralsearch/StructuralSearchProfileBase.java b/platform/structuralsearch/source/com/intellij/structuralsearch/StructuralSearchProfileBase.java index 9e9496115809..47222f932714 100644 --- a/platform/structuralsearch/source/com/intellij/structuralsearch/StructuralSearchProfileBase.java +++ b/platform/structuralsearch/source/com/intellij/structuralsearch/StructuralSearchProfileBase.java @@ -176,6 +176,7 @@ public abstract class StructuralSearchProfileBase extends StructuralSearchProfil return false; } + @NotNull @Override public String getTypedVarString(PsiElement element) { final PsiElement initialElement = element; @@ -480,10 +481,7 @@ public abstract class StructuralSearchProfileBase extends StructuralSearchProfil private void visitLiteral(PsiElement literal) { String value = literal.getText(); - if (value.length() > 2 && - (value.charAt(0) == '"' && value.charAt(value.length() - 1) == '"') || - (value.charAt(0) == '\'' && value.charAt(value.length() - 1) == '\'')) { - + if (StringUtil.isQuotedString(value)) { if (mySubstitutionPatterns == null) { final String[] prefixes = myGlobalVisitor.getContext().getPattern().getTypedVarPrefixes(); mySubstitutionPatterns = createPatterns(prefixes); diff --git a/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/CompiledPattern.java b/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/CompiledPattern.java index 70c1cdccd03e..55aa79bfcf0b 100644 --- a/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/CompiledPattern.java +++ b/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/CompiledPattern.java @@ -12,6 +12,7 @@ import com.intellij.structuralsearch.impl.matcher.handlers.MatchingHandler; import com.intellij.structuralsearch.impl.matcher.handlers.SimpleHandler; import com.intellij.structuralsearch.impl.matcher.handlers.SubstitutionHandler; import com.intellij.structuralsearch.impl.matcher.strategies.MatchingStrategy; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.HashMap; @@ -82,7 +83,7 @@ public abstract class CompiledPattern { public boolean isRealTypedVar(PsiElement element) { if (element!=null && element.getTextLength()>0) { String str = getTypedVarString(element); - if (str == null || str.length()==0) { + if (str.length() == 0) { return false; } return isTypedVar( str ); @@ -91,6 +92,7 @@ public abstract class CompiledPattern { } } + @NotNull public String getTypedVarString(PsiElement element) { final StructuralSearchProfile profile = StructuralSearchUtil.getProfileByPsiElement(element); if (profile == null) { diff --git a/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/compiler/CompileContext.java b/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/compiler/CompileContext.java index 09758a5e2191..f28e83b7ad1c 100644 --- a/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/compiler/CompileContext.java +++ b/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/compiler/CompileContext.java @@ -41,10 +41,6 @@ public class CompileContext { return searchHelper; } - void setSearchHelper(OptimizingSearchHelper searchHelper) { - this.searchHelper = searchHelper; - } - public CompiledPattern getPattern() { return pattern; } diff --git a/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/compiler/FindInFilesOptimizingSearchHelper.java b/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/compiler/FindInFilesOptimizingSearchHelper.java index ff7f06a36d5b..c2fa05fef149 100644 --- a/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/compiler/FindInFilesOptimizingSearchHelper.java +++ b/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/compiler/FindInFilesOptimizingSearchHelper.java @@ -3,7 +3,6 @@ package com.intellij.structuralsearch.impl.matcher.compiler; import com.intellij.lang.Language; import com.intellij.lang.LanguageNamesValidation; import com.intellij.lang.refactoring.NamesValidator; -import com.intellij.lexer.Lexer; import com.intellij.openapi.fileTypes.FileType; import com.intellij.openapi.fileTypes.LanguageFileType; import com.intellij.openapi.project.Project; @@ -22,7 +21,6 @@ class FindInFilesOptimizingSearchHelper extends OptimizingSearchHelperBase { private PsiSearchHelper helper; private THashMap<PsiFile,PsiFile> filesToScan; private THashMap<PsiFile,PsiFile> filesToScan2; - private Lexer javaLexer; private final boolean findMatchingFiles; diff --git a/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/compiler/PatternCompiler.java b/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/compiler/PatternCompiler.java index 6dba855121cb..07808d33c003 100644 --- a/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/compiler/PatternCompiler.java +++ b/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/compiler/PatternCompiler.java @@ -10,10 +10,7 @@ import com.intellij.openapi.fileTypes.FileType; import com.intellij.openapi.fileTypes.LanguageFileType; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.text.StringUtil; -import com.intellij.psi.PsiElement; -import com.intellij.psi.PsiErrorElement; -import com.intellij.psi.PsiFile; -import com.intellij.psi.PsiRecursiveElementWalkingVisitor; +import com.intellij.psi.*; import com.intellij.psi.impl.source.PsiFileImpl; import com.intellij.psi.impl.source.tree.LeafElement; import com.intellij.psi.search.GlobalSearchScope; @@ -26,6 +23,7 @@ import com.intellij.structuralsearch.impl.matcher.MatcherImplUtil; import com.intellij.structuralsearch.impl.matcher.PatternTreeContext; import com.intellij.structuralsearch.impl.matcher.filters.LexicalNodesFilter; import com.intellij.structuralsearch.impl.matcher.handlers.MatchPredicate; +import com.intellij.structuralsearch.impl.matcher.handlers.MatchingHandler; import com.intellij.structuralsearch.impl.matcher.handlers.SubstitutionHandler; import com.intellij.structuralsearch.impl.matcher.predicates.*; import com.intellij.structuralsearch.plugin.ui.Configuration; @@ -73,7 +71,9 @@ public class PatternCompiler { List<PsiElement> elements = compileByAllPrefixes(project, options, result, context, prefixes); - context.getPattern().setNodes(elements); + final CompiledPattern pattern = context.getPattern(); + checkForUnknownVariables(pattern, elements); + pattern.setNodes(elements); if (context.getSearchHelper().doOptimizing() && context.getSearchHelper().isScannedSomething()) { final Set<PsiFile> set = context.getSearchHelper().getFilesSetToScan(); @@ -105,6 +105,28 @@ public class PatternCompiler { return result; } + private static void checkForUnknownVariables(final CompiledPattern pattern, List<PsiElement> elements) { + for (PsiElement element : elements) { + element.accept(new PsiRecursiveElementWalkingVisitor() { + @Override + public void visitElement(PsiElement element) { + if (element instanceof PsiComment) { + return; + } + super.visitElement(element); + + if (!(element instanceof LeafElement) || !pattern.isTypedVar(element)) { + return; + } + final MatchingHandler handler = pattern.getHandler(pattern.getTypedVarString(element)); + if (handler == null) { + throw new MalformedPatternException(); + } + } + }); + } + } + public static String getLastFindPlan() { return ((TestModeOptimizingSearchHelper)lastTestingContext.getSearchHelper()).getSearchPlan(); } @@ -322,11 +344,8 @@ public class PatternCompiler { @Override public String getPrefix(int varIndex) { - try { - return myPrefixes[varIndex]; - } catch (ArrayIndexOutOfBoundsException e) { - return null; - } + if (varIndex >= myPrefixes.length) return null; + return myPrefixes[varIndex]; } } @@ -462,10 +481,9 @@ public class PatternCompiler { PsiElement[] matchStatements; try { - final String pattern = buf.toString(); - matchStatements = MatcherImplUtil.createTreeFromText(pattern, PatternTreeContext.Block, options.getFileType(), + matchStatements = MatcherImplUtil.createTreeFromText(buf.toString(), PatternTreeContext.Block, options.getFileType(), options.getDialect(), options.getPatternContext(), project, false); - if (matchStatements.length==0) throw new MalformedPatternException(pattern); + if (matchStatements.length==0) throw new MalformedPatternException(); } catch (IncorrectOperationException e) { throw new MalformedPatternException(e.getMessage()); } @@ -487,12 +505,11 @@ public class PatternCompiler { } private static void addScriptConstraint(String name, MatchVariableConstraint constraint, SubstitutionHandler handler) { - MatchPredicate predicate; if (constraint.getScriptCodeConstraint()!= null && constraint.getScriptCodeConstraint().length() > 2) { final String script = StringUtil.stripQuotesAroundValue(constraint.getScriptCodeConstraint()); final String s = ScriptSupport.checkValidScript(script); if (s != null) throw new MalformedPatternException("Script constraint for " + constraint.getName() + " has problem "+s); - predicate = new ScriptPredicate(name, script); + MatchPredicate predicate = new ScriptPredicate(name, script); addPredicate(handler,predicate); } } |