Search in sources :

Example 1 with SubstitutionHandler

use of com.intellij.structuralsearch.impl.matcher.handlers.SubstitutionHandler in project intellij-community by JetBrains.

the class JavaMatchingVisitor method saveOrDropResult.

private void saveOrDropResult(final PsiIdentifier methodNameNode, final boolean typedVar, final PsiIdentifier methodNameNode2) {
    MatchResultImpl ourResult = myMatchingVisitor.getMatchContext().hasResult() ? myMatchingVisitor.getMatchContext().getResult() : null;
    myMatchingVisitor.getMatchContext().popResult();
    if (myMatchingVisitor.getResult()) {
        if (typedVar) {
            final SubstitutionHandler handler = (SubstitutionHandler) myMatchingVisitor.getMatchContext().getPattern().getHandler(methodNameNode);
            if (ourResult != null)
                ourResult.setScopeMatch(true);
            handler.setNestedResult(ourResult);
            myMatchingVisitor.setResult(handler.handle(methodNameNode2, myMatchingVisitor.getMatchContext()));
            if (handler.getNestedResult() != null) {
                // some constraint prevent from adding
                handler.setNestedResult(null);
                copyResults(ourResult);
            }
        } else if (ourResult != null) {
            copyResults(ourResult);
        }
    }
}
Also used : SubstitutionHandler(com.intellij.structuralsearch.impl.matcher.handlers.SubstitutionHandler)

Example 2 with SubstitutionHandler

use of com.intellij.structuralsearch.impl.matcher.handlers.SubstitutionHandler in project intellij-community by JetBrains.

the class JavaMatchingVisitor method visitReferenceExpression.

@Override
public void visitReferenceExpression(final PsiReferenceExpression reference) {
    final PsiExpression qualifier = reference.getQualifierExpression();
    final PsiElement nameElement = reference.getReferenceNameElement();
    final MatchContext context = myMatchingVisitor.getMatchContext();
    MatchingHandler _handler = nameElement != null ? context.getPattern().getHandlerSimple(nameElement) : null;
    if (!(_handler instanceof SubstitutionHandler))
        _handler = context.getPattern().getHandlerSimple(reference);
    final PsiElement element = myMatchingVisitor.getElement();
    PsiElement other = element instanceof PsiExpression && context.getOptions().isLooseMatching() ? PsiUtil.skipParenthesizedExprDown((PsiExpression) element) : element;
    if (_handler instanceof SubstitutionHandler && !(context.getPattern().getHandlerSimple(qualifier) instanceof SubstitutionHandler) && !(qualifier instanceof PsiThisExpression)) {
        final SubstitutionHandler handler = (SubstitutionHandler) _handler;
        if (handler.isSubtype() || handler.isStrictSubtype()) {
            myMatchingVisitor.setResult(checkMatchWithinHierarchy(other, handler, reference));
        } else {
            myMatchingVisitor.setResult(handler.handle(other, context));
        }
        return;
    }
    final boolean multiMatch = other != null && reference.getContainingFile() == other.getContainingFile();
    if (!(other instanceof PsiReferenceExpression)) {
        myMatchingVisitor.setResult(multiMatch && myMatchingVisitor.matchText(reference, other));
        return;
    }
    final PsiReferenceExpression reference2 = (PsiReferenceExpression) other;
    final PsiExpression qualifier2 = reference2.getQualifierExpression();
    if (multiMatch && (qualifier == null || qualifier instanceof PsiThisExpression || qualifier instanceof PsiSuperExpression) && (qualifier2 == null || qualifier2 instanceof PsiThisExpression || qualifier2 instanceof PsiSuperExpression)) {
        final PsiElement target = reference.resolve();
        if (target != null) {
            myMatchingVisitor.setResult(target == reference2.resolve());
            return;
        }
    }
    if (qualifier == null && qualifier2 == null) {
        myMatchingVisitor.setResult(myMatchingVisitor.matchText(reference.getReferenceNameElement(), reference2.getReferenceNameElement()));
        return;
    }
    // handle field selection
    if (!(other.getParent() instanceof PsiMethodCallExpression) && qualifier != null) {
        final PsiElement referenceElement = reference.getReferenceNameElement();
        final PsiElement referenceElement2 = reference2.getReferenceNameElement();
        if (context.getPattern().isTypedVar(referenceElement)) {
            myMatchingVisitor.setResult(myMatchingVisitor.handleTypedElement(referenceElement, referenceElement2));
        } else {
            myMatchingVisitor.setResult(myMatchingVisitor.matchText(referenceElement, referenceElement2));
        }
        if (!myMatchingVisitor.getResult()) {
            return;
        }
        if (qualifier2 != null) {
            myMatchingVisitor.setResult(myMatchingVisitor.match(qualifier, qualifier2));
        } else {
            final PsiElement referencedElement = MatchUtils.getReferencedElement(other);
            if (referencedElement instanceof PsiField) {
                final PsiField field = (PsiField) referencedElement;
                if (qualifier instanceof PsiThisExpression) {
                    myMatchingVisitor.setResult(!field.hasModifierProperty(PsiModifier.STATIC));
                    return;
                }
            }
            final MatchingHandler handler = context.getPattern().getHandler(qualifier);
            matchImplicitQualifier(handler, referencedElement, context);
        }
        return;
    }
    myMatchingVisitor.setResult(false);
}
Also used : SubstitutionHandler(com.intellij.structuralsearch.impl.matcher.handlers.SubstitutionHandler) MatchingHandler(com.intellij.structuralsearch.impl.matcher.handlers.MatchingHandler)

Example 3 with SubstitutionHandler

use of com.intellij.structuralsearch.impl.matcher.handlers.SubstitutionHandler in project intellij-community by JetBrains.

the class JavaMatchingVisitor method visitNameValuePair.

@Override
public void visitNameValuePair(PsiNameValuePair pair) {
    final PsiNameValuePair elementNameValuePair = (PsiNameValuePair) myMatchingVisitor.getElement();
    final PsiAnnotationMemberValue annotationInitializer = pair.getValue();
    myMatchingVisitor.setResult(myMatchingVisitor.match(annotationInitializer, elementNameValuePair.getValue()));
    if (myMatchingVisitor.getResult()) {
        final PsiIdentifier nameIdentifier = pair.getNameIdentifier();
        final PsiIdentifier otherIdentifier = elementNameValuePair.getNameIdentifier();
        if (nameIdentifier == null) {
            myMatchingVisitor.setResult(otherIdentifier == null || otherIdentifier.getText().equals(PsiAnnotation.DEFAULT_REFERENCED_METHOD_NAME));
        } else {
            final MatchingHandler handler = myMatchingVisitor.getMatchContext().getPattern().getHandler(nameIdentifier);
            if (handler instanceof SubstitutionHandler) {
                myMatchingVisitor.setResult(((SubstitutionHandler) handler).handle(otherIdentifier, myMatchingVisitor.getMatchContext()));
            } else {
                myMatchingVisitor.setResult(myMatchingVisitor.match(nameIdentifier, otherIdentifier));
            }
        }
    }
}
Also used : SubstitutionHandler(com.intellij.structuralsearch.impl.matcher.handlers.SubstitutionHandler) MatchingHandler(com.intellij.structuralsearch.impl.matcher.handlers.MatchingHandler)

Example 4 with SubstitutionHandler

use of com.intellij.structuralsearch.impl.matcher.handlers.SubstitutionHandler in project intellij-community by JetBrains.

the class CompiledPattern method createSubstitutionHandler.

public SubstitutionHandler createSubstitutionHandler(String name, String compiledName, boolean target, int minOccurs, int maxOccurs, boolean greedy) {
    SubstitutionHandler handler = (SubstitutionHandler) handlers.get(compiledName);
    if (handler != null)
        return handler;
    handler = doCreateSubstitutionHandler(name, target, minOccurs, maxOccurs, greedy);
    handlers.put(compiledName, handler);
    return handler;
}
Also used : SubstitutionHandler(com.intellij.structuralsearch.impl.matcher.handlers.SubstitutionHandler)

Example 5 with SubstitutionHandler

use of com.intellij.structuralsearch.impl.matcher.handlers.SubstitutionHandler 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)

Aggregations

SubstitutionHandler (com.intellij.structuralsearch.impl.matcher.handlers.SubstitutionHandler)15 MatchingHandler (com.intellij.structuralsearch.impl.matcher.handlers.MatchingHandler)8 MatchPredicate (com.intellij.structuralsearch.impl.matcher.handlers.MatchPredicate)3 LiteralWithSubstitutionHandler (com.intellij.structuralsearch.impl.matcher.handlers.LiteralWithSubstitutionHandler)2 RegExpPredicate (com.intellij.structuralsearch.impl.matcher.predicates.RegExpPredicate)2 Template (com.intellij.codeInsight.template.Template)1 ArrayBackedNodeIterator (com.intellij.dupLocator.iterators.ArrayBackedNodeIterator)1 NodeFilter (com.intellij.dupLocator.util.NodeFilter)1 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)1 LexicalNodesFilter (com.intellij.structuralsearch.impl.matcher.filters.LexicalNodesFilter)1 DelegatingHandler (com.intellij.structuralsearch.impl.matcher.handlers.DelegatingHandler)1 HierarchyNodeIterator (com.intellij.structuralsearch.impl.matcher.iterators.HierarchyNodeIterator)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 TIntArrayList (gnu.trove.TIntArrayList)1 Matcher (java.util.regex.Matcher)1 NonNls (org.jetbrains.annotations.NonNls)1 Nullable (org.jetbrains.annotations.Nullable)1