Search in sources :

Example 1 with RegExpPredicate

use of com.intellij.structuralsearch.impl.matcher.predicates.RegExpPredicate in project intellij-community by JetBrains.

the class JavaCompilingVisitor method createAndSetSubstitutionHandlerFromReference.

private SubstitutionHandler createAndSetSubstitutionHandlerFromReference(final PsiElement expr, final String referenceText, boolean classQualifier) {
    final SubstitutionHandler substitutionHandler = new SubstitutionHandler("__" + referenceText.replace('.', '_'), false, classQualifier ? 0 : 1, 1, false);
    final boolean caseSensitive = myCompilingVisitor.getContext().getOptions().isCaseSensitiveMatch();
    substitutionHandler.setPredicate(new RegExpPredicate(referenceText.replaceAll("\\.", "\\\\."), caseSensitive, null, false, false));
    myCompilingVisitor.getContext().getPattern().setHandler(expr, substitutionHandler);
    return substitutionHandler;
}
Also used : RegExpPredicate(com.intellij.structuralsearch.impl.matcher.predicates.RegExpPredicate)

Example 2 with RegExpPredicate

use of com.intellij.structuralsearch.impl.matcher.predicates.RegExpPredicate in project intellij-community by JetBrains.

the class GlobalCompilingVisitor method processPatternStringWithFragments.

@Nullable
public MatchingHandler processPatternStringWithFragments(String pattern, OccurenceKind kind, Pattern substitutionPattern) {
    String content;
    if (kind == OccurenceKind.LITERAL) {
        content = pattern.substring(1, pattern.length() - 1);
    } else if (kind == OccurenceKind.COMMENT) {
        content = pattern;
    } else {
        return null;
    }
    @NonNls StringBuilder buf = new StringBuilder(content.length());
    Matcher matcher = substitutionPattern.matcher(content);
    List<SubstitutionHandler> handlers = null;
    int start = 0;
    String word;
    boolean hasLiteralContent = false;
    SubstitutionHandler handler = null;
    while (matcher.find()) {
        if (handlers == null)
            handlers = new ArrayList<>();
        handler = (SubstitutionHandler) getContext().getPattern().getHandler(matcher.group(1));
        if (handler != null)
            handlers.add(handler);
        word = content.substring(start, matcher.start());
        if (word.length() > 0) {
            buf.append(StructuralSearchUtil.shieldSpecialChars(word));
            hasLiteralContent = true;
            processTokenizedName(word, false, kind);
        }
        RegExpPredicate predicate = MatchingHandler.getSimpleRegExpPredicate(handler);
        if (predicate == null || !predicate.isWholeWords()) {
            buf.append("(.*?)");
        } else {
            buf.append(".*?\\b(").append(predicate.getRegExp()).append(")\\b.*?");
        }
        if (isSuitablePredicate(predicate, handler)) {
            processTokenizedName(predicate.getRegExp(), false, kind);
        }
        start = matcher.end();
    }
    word = content.substring(start, content.length());
    if (word.length() > 0) {
        hasLiteralContent = true;
        buf.append(StructuralSearchUtil.shieldSpecialChars(word));
        processTokenizedName(word, false, kind);
    }
    if (hasLiteralContent) {
        if (kind == OccurenceKind.LITERAL) {
            buf.insert(0, "[\"']");
            buf.append("[\"']");
        }
        buf.append("$");
    }
    if (handlers != null) {
        return hasLiteralContent ? new LiteralWithSubstitutionHandler(buf.toString(), handlers) : handler;
    }
    return null;
}
Also used : NonNls(org.jetbrains.annotations.NonNls) SubstitutionHandler(com.intellij.structuralsearch.impl.matcher.handlers.SubstitutionHandler) LiteralWithSubstitutionHandler(com.intellij.structuralsearch.impl.matcher.handlers.LiteralWithSubstitutionHandler) RegExpPredicate(com.intellij.structuralsearch.impl.matcher.predicates.RegExpPredicate) Matcher(java.util.regex.Matcher) LiteralWithSubstitutionHandler(com.intellij.structuralsearch.impl.matcher.handlers.LiteralWithSubstitutionHandler) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with RegExpPredicate

use of com.intellij.structuralsearch.impl.matcher.predicates.RegExpPredicate in project intellij-community by JetBrains.

the class JavaCompilingVisitor method visitComment.

@Override
public void visitComment(PsiComment comment) {
    super.visitComment(comment);
    final String text = comment.getText();
    Matcher matcher = ourPattern.matcher(text);
    boolean matches = false;
    if (!matcher.matches()) {
        matcher = ourPattern2.matcher(text);
        if (!matcher.matches()) {
            matcher = ourPattern3.matcher(text);
        } else {
            matches = true;
        }
    } else {
        matches = true;
    }
    if (matches || matcher.matches()) {
        String str = matcher.group(1);
        comment.putUserData(CompiledPattern.HANDLER_KEY, str);
        GlobalCompilingVisitor.setFilter(myCompilingVisitor.getContext().getPattern().getHandler(comment), CommentFilter.getInstance());
        SubstitutionHandler handler = (SubstitutionHandler) myCompilingVisitor.getContext().getPattern().getHandler(str);
        if (handler == null) {
            throw new MalformedPatternException();
        }
        if (handler.getPredicate() != null) {
            ((RegExpPredicate) handler.getPredicate()).setMultiline(true);
        }
        RegExpPredicate predicate = MatchingHandler.getSimpleRegExpPredicate(handler);
        if (GlobalCompilingVisitor.isSuitablePredicate(predicate, handler)) {
            myCompilingVisitor.processTokenizedName(predicate.getRegExp(), true, GlobalCompilingVisitor.OccurenceKind.COMMENT);
        }
        matches = true;
    }
    if (!matches) {
        MatchingHandler handler = myCompilingVisitor.processPatternStringWithFragments(text, GlobalCompilingVisitor.OccurenceKind.COMMENT);
        if (handler != null)
            comment.putUserData(CompiledPattern.HANDLER_KEY, handler);
    }
}
Also used : RegExpPredicate(com.intellij.structuralsearch.impl.matcher.predicates.RegExpPredicate) Matcher(java.util.regex.Matcher) MalformedPatternException(com.intellij.structuralsearch.MalformedPatternException)

Example 4 with RegExpPredicate

use of com.intellij.structuralsearch.impl.matcher.predicates.RegExpPredicate in project intellij-community by JetBrains.

the class JavaCompilingVisitor method handleReferenceText.

private static void handleReferenceText(String refname, CompileContext compileContext) {
    if (refname == null)
        return;
    if (compileContext.getPattern().isTypedVar(refname)) {
        SubstitutionHandler handler = (SubstitutionHandler) compileContext.getPattern().getHandler(refname);
        RegExpPredicate predicate = MatchingHandler.getSimpleRegExpPredicate(handler);
        if (!GlobalCompilingVisitor.isSuitablePredicate(predicate, handler)) {
            return;
        }
        refname = predicate.getRegExp();
        if (handler.isStrictSubtype() || handler.isSubtype()) {
            final OptimizingSearchHelper searchHelper = compileContext.getSearchHelper();
            if (addDescendantsOf(refname, handler.isSubtype(), searchHelper, compileContext)) {
                searchHelper.endTransaction();
            }
            return;
        }
    }
    GlobalCompilingVisitor.addFilesToSearchForGivenWord(refname, true, GlobalCompilingVisitor.OccurenceKind.CODE, compileContext);
}
Also used : RegExpPredicate(com.intellij.structuralsearch.impl.matcher.predicates.RegExpPredicate)

Example 5 with RegExpPredicate

use of com.intellij.structuralsearch.impl.matcher.predicates.RegExpPredicate in project intellij-community by JetBrains.

the class JavaMatchingVisitor method matchType.

private boolean matchType(final PsiElement patternType, final PsiElement matchedType) {
    PsiElement patternElement = getInnermostComponent(patternType);
    PsiElement matchedElement = getInnermostComponent(matchedType);
    PsiElement[] typeParameters = null;
    if (matchedElement instanceof PsiJavaCodeReferenceElement) {
        final PsiJavaCodeReferenceElement referenceElement = (PsiJavaCodeReferenceElement) matchedElement;
        typeParameters = getTypeParameters(referenceElement, !hasDiamondTypeParameter(patternElement));
    } else if (matchedElement instanceof PsiTypeParameter) {
        matchedElement = ((PsiTypeParameter) matchedElement).getNameIdentifier();
    } else if (matchedElement instanceof PsiClass && ((PsiClass) matchedElement).hasTypeParameters()) {
        typeParameters = ((PsiClass) matchedElement).getTypeParameters();
        matchedElement = ((PsiClass) matchedElement).getNameIdentifier();
    } else if (matchedElement instanceof PsiMethod && ((PsiMethod) matchedElement).hasTypeParameters()) {
        typeParameters = ((PsiMethod) matchedType).getTypeParameters();
        matchedElement = ((PsiMethod) matchedType).getNameIdentifier();
    }
    if (patternElement instanceof PsiJavaCodeReferenceElement) {
        final PsiJavaCodeReferenceElement referenceElement = (PsiJavaCodeReferenceElement) patternElement;
        final PsiReferenceParameterList list = referenceElement.getParameterList();
        if (list != null) {
            final PsiTypeElement[] elements = list.getTypeParameterElements();
            if (elements.length > 0 && (typeParameters == null || !myMatchingVisitor.matchSequentially(elements, typeParameters))) {
                return false;
            }
        }
        patternElement = referenceElement.getReferenceNameElement();
    }
    final int matchedArrayDimensions = getArrayDimensions(matchedType);
    final int patternArrayDimensions = getArrayDimensions(patternType);
    if (myMatchingVisitor.getMatchContext().getPattern().isTypedVar(patternElement)) {
        final SubstitutionHandler handler = (SubstitutionHandler) myMatchingVisitor.getMatchContext().getPattern().getHandler(patternElement);
        RegExpPredicate regExpPredicate = null;
        if (patternArrayDimensions != 0) {
            if (patternArrayDimensions != matchedArrayDimensions) {
                return false;
            }
        } else if (matchedArrayDimensions != 0) {
            regExpPredicate = MatchingHandler.getSimpleRegExpPredicate(handler);
            if (regExpPredicate != null) {
                regExpPredicate.setNodeTextGenerator(new RegExpPredicate.NodeTextGenerator() {

                    public String getText(PsiElement element) {
                        StringBuilder builder = new StringBuilder(RegExpPredicate.getMeaningfulText(element));
                        for (int i = 0; i < matchedArrayDimensions; ++i) builder.append("[]");
                        return builder.toString();
                    }
                });
            }
        }
        try {
            if (handler.isSubtype() || handler.isStrictSubtype()) {
                return checkMatchWithinHierarchy(matchedElement, handler, patternElement);
            } else {
                return handler.handle(matchedElement, myMatchingVisitor.getMatchContext());
            }
        } finally {
            if (regExpPredicate != null)
                regExpPredicate.setNodeTextGenerator(null);
        }
    }
    if (matchedArrayDimensions != patternArrayDimensions) {
        return false;
    }
    if (patternElement instanceof PsiIdentifier) {
        final PsiElement parent = patternElement.getParent();
        if (parent instanceof PsiJavaCodeReferenceElement) {
            patternElement = parent;
        }
    }
    if (matchedElement instanceof PsiIdentifier) {
        final PsiElement parent = matchedElement.getParent();
        if (parent instanceof PsiJavaCodeReferenceElement) {
            matchedElement = parent;
        }
    }
    final String text = getText(patternElement);
    final String text2 = getText(matchedElement);
    final boolean caseSensitive = myMatchingVisitor.getMatchContext().getOptions().isCaseSensitiveMatch();
    final boolean equalsIgnorePackage = MatchUtils.compareWithNoDifferenceToPackage(text, text2, !caseSensitive);
    if (equalsIgnorePackage || !(matchedElement instanceof PsiJavaReference)) {
        return equalsIgnorePackage;
    } else {
        final PsiElement element2 = ((PsiJavaReference) matchedElement).resolve();
        if (!(element2 instanceof PsiClass)) {
            return false;
        }
        final String name = ((PsiClass) element2).getQualifiedName();
        return caseSensitive ? text.equals(name) : text.equalsIgnoreCase(name);
    }
}
Also used : RegExpPredicate(com.intellij.structuralsearch.impl.matcher.predicates.RegExpPredicate) SubstitutionHandler(com.intellij.structuralsearch.impl.matcher.handlers.SubstitutionHandler)

Aggregations

RegExpPredicate (com.intellij.structuralsearch.impl.matcher.predicates.RegExpPredicate)6 SubstitutionHandler (com.intellij.structuralsearch.impl.matcher.handlers.SubstitutionHandler)2 Matcher (java.util.regex.Matcher)2 MalformedPatternException (com.intellij.structuralsearch.MalformedPatternException)1 LiteralWithSubstitutionHandler (com.intellij.structuralsearch.impl.matcher.handlers.LiteralWithSubstitutionHandler)1 BinaryPredicate (com.intellij.structuralsearch.impl.matcher.predicates.BinaryPredicate)1 NotPredicate (com.intellij.structuralsearch.impl.matcher.predicates.NotPredicate)1 NonNls (org.jetbrains.annotations.NonNls)1 Nullable (org.jetbrains.annotations.Nullable)1