Search in sources :

Example 11 with SubstitutionHandler

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

the class JavaMatchingVisitor method visitLiteralExpression.

@Override
public void visitLiteralExpression(final PsiLiteralExpression const1) {
    final PsiLiteralExpression const2 = (PsiLiteralExpression) myMatchingVisitor.getElement();
    final MatchingHandler handler = (MatchingHandler) const1.getUserData(CompiledPattern.HANDLER_KEY);
    if (handler instanceof SubstitutionHandler) {
        final PsiType type1 = const1.getType();
        if (type1 != null && !type1.equals(const2.getType())) {
            myMatchingVisitor.setResult(false);
        } else {
            int offset = 0;
            int length = const2.getTextLength();
            final String text = const2.getText();
            if (length > 2 && text.charAt(0) == '"' && text.charAt(length - 1) == '"') {
                length--;
                offset++;
            }
            myMatchingVisitor.setResult(((SubstitutionHandler) handler).handle(const2, offset, length, myMatchingVisitor.getMatchContext()));
        }
    } else if (handler != null) {
        myMatchingVisitor.setResult(handler.match(const1, const2, myMatchingVisitor.getMatchContext()));
    } else {
        myMatchingVisitor.setResult(myMatchingVisitor.matchText(const1, const2));
    }
}
Also used : SubstitutionHandler(com.intellij.structuralsearch.impl.matcher.handlers.SubstitutionHandler) MatchingHandler(com.intellij.structuralsearch.impl.matcher.handlers.MatchingHandler)

Example 12 with SubstitutionHandler

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

the class JavaMatchingVisitor method visitNewExpression.

@Override
public void visitNewExpression(final PsiNewExpression new1) {
    final PsiElement other = myMatchingVisitor.getElement();
    final PsiJavaCodeReferenceElement classReference = new1.getClassReference();
    if (other instanceof PsiArrayInitializerExpression && other.getParent() instanceof PsiVariable && new1.getArrayDimensions().length == 0 && new1.getArrayInitializer() != null) {
        final MatchContext matchContext = myMatchingVisitor.getMatchContext();
        final MatchingHandler handler = matchContext.getPattern().getHandler(classReference);
        final boolean looseMatching = myMatchingVisitor.getMatchContext().getOptions().isLooseMatching();
        if ((handler instanceof SubstitutionHandler && ((SubstitutionHandler) handler).getMinOccurs() != 0) || !looseMatching) {
            myMatchingVisitor.setResult(false);
            return;
        }
        final PsiType otherType = ((PsiArrayInitializerExpression) other).getType();
        if (handler instanceof SubstitutionHandler && otherType != null) {
            final PsiElementFactory factory = JavaPsiFacade.getElementFactory(other.getProject());
            final PsiTypeElement otherTypeElement = factory.createTypeElement(otherType.getDeepComponentType());
            final SubstitutionHandler substitutionHandler = (SubstitutionHandler) handler;
            final MatchPredicate predicate = substitutionHandler.getPredicate();
            myMatchingVisitor.setResult(predicate == null || predicate.match(null, otherTypeElement, matchContext));
        } else {
            final PsiType type = new1.getType();
            myMatchingVisitor.setResult(type != null && type.equals(otherType));
        }
        if (myMatchingVisitor.getResult()) {
            myMatchingVisitor.matchSons(new1.getArrayInitializer(), other);
        }
        return;
    }
    if (!(other instanceof PsiNewExpression)) {
        myMatchingVisitor.setResult(false);
        return;
    }
    final PsiNewExpression new2 = (PsiNewExpression) other;
    if (classReference != null) {
        if (new2.getClassReference() != null) {
            myMatchingVisitor.setResult(myMatchingVisitor.match(classReference, new2.getClassReference()) && myMatchingVisitor.matchSons(new1.getArrayInitializer(), new2.getArrayInitializer()));
            if (myMatchingVisitor.getResult()) {
                // matching dims
                matchArrayDims(new1, new2);
            }
            return;
        } else {
            // match array of primitive by new 'T();
            final PsiKeyword newKeyword = PsiTreeUtil.getChildOfType(new2, PsiKeyword.class);
            final PsiElement element = PsiTreeUtil.getNextSiblingOfType(newKeyword, PsiWhiteSpace.class);
            if (element != null && element.getNextSibling() instanceof PsiKeyword) {
                ((LexicalNodesFilter) LexicalNodesFilter.getInstance()).setCareKeyWords(true);
                myMatchingVisitor.setResult(myMatchingVisitor.match(classReference, element.getNextSibling()) && myMatchingVisitor.matchSons(new1.getArrayInitializer(), new2.getArrayInitializer()));
                ((LexicalNodesFilter) LexicalNodesFilter.getInstance()).setCareKeyWords(false);
                if (myMatchingVisitor.getResult()) {
                    // matching dims
                    matchArrayDims(new1, new2);
                }
                return;
            }
        }
    }
    if (classReference == new2.getClassReference()) {
        // probably anonymous class or array of primitive type
        ((LexicalNodesFilter) LexicalNodesFilter.getInstance()).setCareKeyWords(true);
        myMatchingVisitor.setResult(myMatchingVisitor.matchSons(new1, new2));
        ((LexicalNodesFilter) LexicalNodesFilter.getInstance()).setCareKeyWords(false);
    } else if (new1.getAnonymousClass() == null && classReference != null && new2.getAnonymousClass() != null) {
        // allow matching anonymous class without pattern
        myMatchingVisitor.setResult(myMatchingVisitor.match(classReference, new2.getAnonymousClass().getBaseClassReference()) && myMatchingVisitor.matchSons(new1.getArgumentList(), new2.getArgumentList()));
    } else {
        myMatchingVisitor.setResult(false);
    }
}
Also used : LexicalNodesFilter(com.intellij.structuralsearch.impl.matcher.filters.LexicalNodesFilter) MatchingHandler(com.intellij.structuralsearch.impl.matcher.handlers.MatchingHandler) MatchPredicate(com.intellij.structuralsearch.impl.matcher.handlers.MatchPredicate) SubstitutionHandler(com.intellij.structuralsearch.impl.matcher.handlers.SubstitutionHandler)

Example 13 with SubstitutionHandler

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

the class GlobalCompilingVisitor method handle.

public final void handle(PsiElement element) {
    if ((!ourFilter.accepts(element) || StructuralSearchUtil.isIdentifier(element)) && context.getPattern().isRealTypedVar(element) && context.getPattern().getHandlerSimple(element) == null) {
        String name = context.getPattern().getTypedVarString(element);
        // name is the same for named element (clazz,methods, etc) and token (name of ... itself)
        // @todo need fix this
        final SubstitutionHandler handler = (SubstitutionHandler) context.getPattern().getHandler(name);
        if (handler == null)
            return;
        context.getPattern().setHandler(element, handler);
        if (context.getOptions().getVariableConstraint(handler.getName()).isPartOfSearchResults()) {
            handler.setTarget(true);
            context.getPattern().setTargetNode(element);
        }
    }
}
Also used : SubstitutionHandler(com.intellij.structuralsearch.impl.matcher.handlers.SubstitutionHandler) LiteralWithSubstitutionHandler(com.intellij.structuralsearch.impl.matcher.handlers.LiteralWithSubstitutionHandler)

Example 14 with SubstitutionHandler

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

the class XmlMatchingVisitor method visitXmlAttributeValue.

@Override
public void visitXmlAttributeValue(XmlAttributeValue value) {
    final XmlAttributeValue another = (XmlAttributeValue) myMatchingVisitor.getElement();
    final String text = value.getValue();
    final boolean isTypedVar = myMatchingVisitor.getMatchContext().getPattern().isTypedVar(text);
    MatchingHandler handler;
    if (isTypedVar && (handler = myMatchingVisitor.getMatchContext().getPattern().getHandler(text)) instanceof SubstitutionHandler) {
        String text2 = another.getText();
        int offset = text2.length() > 0 && (text2.charAt(0) == '"' || text2.charAt(0) == '\'') ? 1 : 0;
        myMatchingVisitor.setResult(((SubstitutionHandler) handler).handle(another, offset, text2.length() - offset, myMatchingVisitor.getMatchContext()));
    } else {
        myMatchingVisitor.setResult(matches(text, another.getValue()));
    }
}
Also used : SubstitutionHandler(com.intellij.structuralsearch.impl.matcher.handlers.SubstitutionHandler) MatchingHandler(com.intellij.structuralsearch.impl.matcher.handlers.MatchingHandler)

Example 15 with SubstitutionHandler

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

the class GlobalMatchingVisitor method handleTypedElement.

public final boolean handleTypedElement(final PsiElement typedElement, final PsiElement match) {
    MatchingHandler handler = matchContext.getPattern().getHandler(typedElement);
    final MatchingHandler initialHandler = handler;
    if (handler instanceof DelegatingHandler) {
        handler = ((DelegatingHandler) handler).getDelegate();
    }
    assert handler instanceof SubstitutionHandler : handler != null ? handler.getClass() : "null" + ' ' + (initialHandler != null ? initialHandler.getClass() : "null");
    return ((SubstitutionHandler) handler).handle(match, matchContext);
}
Also used : DelegatingHandler(com.intellij.structuralsearch.impl.matcher.handlers.DelegatingHandler) SubstitutionHandler(com.intellij.structuralsearch.impl.matcher.handlers.SubstitutionHandler) MatchingHandler(com.intellij.structuralsearch.impl.matcher.handlers.MatchingHandler)

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