Search in sources :

Example 11 with MatchingHandler

use of com.intellij.structuralsearch.impl.matcher.handlers.MatchingHandler 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 12 with MatchingHandler

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

the class XmlCompilingVisitor method visitXmlText.

@Override
public void visitXmlText(XmlText text) {
    super.visitXmlText(text);
    final MatchingHandler handler = myCompilingVisitor.getContext().getPattern().getHandler(text);
    handler.setFilter(TagValueFilter.getInstance());
}
Also used : TopLevelMatchingHandler(com.intellij.structuralsearch.impl.matcher.handlers.TopLevelMatchingHandler) MatchingHandler(com.intellij.structuralsearch.impl.matcher.handlers.MatchingHandler)

Example 13 with MatchingHandler

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

the class MatcherImpl method checkIfShouldAttemptToMatch.

public static boolean checkIfShouldAttemptToMatch(MatchContext context, NodeIterator matchedNodes) {
    final CompiledPattern pattern = context.getPattern();
    final NodeIterator patternNodes = pattern.getNodes();
    try {
        while (true) {
            final PsiElement patternNode = patternNodes.current();
            if (patternNode == null) {
                return true;
            }
            final PsiElement matchedNode = matchedNodes.current();
            if (matchedNode == null) {
                return false;
            }
            final MatchingHandler matchingHandler = pattern.getHandler(patternNode);
            if (matchingHandler == null || !matchingHandler.canMatch(patternNode, matchedNode, context)) {
                return false;
            }
            matchedNodes.advance();
            patternNodes.advance();
        }
    } finally {
        patternNodes.reset();
        matchedNodes.reset();
    }
}
Also used : ArrayBackedNodeIterator(com.intellij.dupLocator.iterators.ArrayBackedNodeIterator) NodeIterator(com.intellij.dupLocator.iterators.NodeIterator) SsrFilteringNodeIterator(com.intellij.structuralsearch.impl.matcher.iterators.SsrFilteringNodeIterator) TopLevelMatchingHandler(com.intellij.structuralsearch.impl.matcher.handlers.TopLevelMatchingHandler) MatchingHandler(com.intellij.structuralsearch.impl.matcher.handlers.MatchingHandler)

Example 14 with MatchingHandler

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

the class XmlMatchingVisitor method visitXmlAttribute.

@Override
public void visitXmlAttribute(XmlAttribute attribute) {
    final XmlAttribute another = (XmlAttribute) myMatchingVisitor.getElement();
    final boolean isTypedVar = myMatchingVisitor.getMatchContext().getPattern().isTypedVar(attribute.getName());
    myMatchingVisitor.setResult(isTypedVar || matches(attribute.getName(), another.getName()));
    final XmlAttributeValue valueElement = attribute.getValueElement();
    if (myMatchingVisitor.getResult() && valueElement != null) {
        myMatchingVisitor.setResult(myMatchingVisitor.match(valueElement, another.getValueElement()));
    }
    if (myMatchingVisitor.getResult() && isTypedVar) {
        MatchingHandler handler = myMatchingVisitor.getMatchContext().getPattern().getHandler(attribute.getName());
        myMatchingVisitor.setResult(((SubstitutionHandler) handler).handle(another, myMatchingVisitor.getMatchContext()));
    }
}
Also used : MatchingHandler(com.intellij.structuralsearch.impl.matcher.handlers.MatchingHandler)

Example 15 with MatchingHandler

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

Aggregations

MatchingHandler (com.intellij.structuralsearch.impl.matcher.handlers.MatchingHandler)18 SubstitutionHandler (com.intellij.structuralsearch.impl.matcher.handlers.SubstitutionHandler)8 ArrayBackedNodeIterator (com.intellij.dupLocator.iterators.ArrayBackedNodeIterator)3 TopLevelMatchingHandler (com.intellij.structuralsearch.impl.matcher.handlers.TopLevelMatchingHandler)3 NodeIterator (com.intellij.dupLocator.iterators.NodeIterator)2 PsiElement (com.intellij.psi.PsiElement)2 LanguageFileType (com.intellij.openapi.fileTypes.LanguageFileType)1 LexicalNodesFilter (com.intellij.structuralsearch.impl.matcher.filters.LexicalNodesFilter)1 DelegatingHandler (com.intellij.structuralsearch.impl.matcher.handlers.DelegatingHandler)1 MatchPredicate (com.intellij.structuralsearch.impl.matcher.handlers.MatchPredicate)1 SimpleHandler (com.intellij.structuralsearch.impl.matcher.handlers.SimpleHandler)1 HierarchyNodeIterator (com.intellij.structuralsearch.impl.matcher.iterators.HierarchyNodeIterator)1 SsrFilteringNodeIterator (com.intellij.structuralsearch.impl.matcher.iterators.SsrFilteringNodeIterator)1 CollectingMatchResultSink (com.intellij.structuralsearch.plugin.util.CollectingMatchResultSink)1 NotNull (org.jetbrains.annotations.NotNull)1