Search in sources :

Example 1 with MatchingHandler

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

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

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

the class GlobalMatchingVisitor method matchContext.

/**
   * Descents the tree in depth finding matches
   *
   * @param elements the element for which the sons are looked for match
   */
public void matchContext(final NodeIterator elements) {
    if (matchContext == null) {
        return;
    }
    final CompiledPattern pattern = matchContext.getPattern();
    final NodeIterator patternNodes = pattern.getNodes().clone();
    final MatchResultImpl saveResult = matchContext.hasResult() ? matchContext.getResult() : null;
    final List<PsiElement> saveMatchedNodes = matchContext.getMatchedNodes();
    try {
        matchContext.setResult(null);
        matchContext.setMatchedNodes(null);
        if (!patternNodes.hasNext())
            return;
        final MatchingHandler firstMatchingHandler = pattern.getHandler(patternNodes.current());
        for (; elements.hasNext(); elements.advance()) {
            final PsiElement elementNode = elements.current();
            boolean matched = firstMatchingHandler.matchSequentially(patternNodes, elements, matchContext);
            if (matched) {
                MatchingHandler matchingHandler = matchContext.getPattern().getHandler(Configuration.CONTEXT_VAR_NAME);
                if (matchingHandler != null) {
                    matched = ((SubstitutionHandler) matchingHandler).handle(elementNode, matchContext);
                }
            }
            final List<PsiElement> matchedNodes = matchContext.getMatchedNodes();
            if (matched) {
                dispatchMatched(matchedNodes, matchContext.getResult());
            }
            matchContext.setMatchedNodes(null);
            matchContext.setResult(null);
            patternNodes.reset();
            if (matchedNodes != null && matchedNodes.size() > 0 && matched) {
                elements.rewind();
            }
        }
    } finally {
        matchContext.setResult(saveResult);
        matchContext.setMatchedNodes(saveMatchedNodes);
    }
}
Also used : NodeIterator(com.intellij.dupLocator.iterators.NodeIterator) MatchingHandler(com.intellij.structuralsearch.impl.matcher.handlers.MatchingHandler) PsiElement(com.intellij.psi.PsiElement)

Example 4 with MatchingHandler

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

the class MatcherImpl method matchByDownUp.

@NotNull
protected List<MatchResult> matchByDownUp(PsiElement element, final MatchOptions options) {
    final CollectingMatchResultSink sink = new CollectingMatchResultSink();
    final CompiledPattern compiledPattern = prepareMatching(sink, options);
    matchContext.setShouldRecursivelyMatch(false);
    PsiElement targetNode = compiledPattern.getTargetNode();
    PsiElement elementToStartMatching = null;
    if (targetNode == null) {
        targetNode = compiledPattern.getNodes().current();
        if (targetNode != null) {
            compiledPattern.getNodes().advance();
            assert !compiledPattern.getNodes().hasNext();
            compiledPattern.getNodes().rewind();
            element = element.getParent();
            if (element == null) {
                return Collections.emptyList();
            }
            while (element.getClass() != targetNode.getClass()) {
                element = element.getParent();
                if (element == null)
                    return Collections.emptyList();
            }
            elementToStartMatching = element;
        }
    } else {
        final StructuralSearchProfile profile = StructuralSearchUtil.getProfileByPsiElement(element);
        if (profile == null)
            return Collections.emptyList();
        targetNode = profile.extendMatchedByDownUp(targetNode);
        MatchingHandler handler = null;
        while (element.getClass() == targetNode.getClass() || compiledPattern.isTypedVar(targetNode) && compiledPattern.getHandler(targetNode).canMatch(targetNode, element, matchContext)) {
            handler = compiledPattern.getHandler(targetNode);
            handler.setPinnedElement(element);
            elementToStartMatching = element;
            if (handler instanceof TopLevelMatchingHandler)
                break;
            element = element.getParent();
            targetNode = targetNode.getParent();
            if (options.isLooseMatching()) {
                element = profile.updateCurrentNode(element);
                targetNode = profile.updateCurrentNode(targetNode);
            }
        }
        if (!(handler instanceof TopLevelMatchingHandler))
            return Collections.emptyList();
    }
    assert targetNode != null : "Could not match down up when no target node";
    final LanguageFileType fileType = (LanguageFileType) matchContext.getOptions().getFileType();
    match(elementToStartMatching, fileType.getLanguage());
    matchContext.getSink().matchingFinished();
    return sink.getMatches();
}
Also used : CollectingMatchResultSink(com.intellij.structuralsearch.plugin.util.CollectingMatchResultSink) LanguageFileType(com.intellij.openapi.fileTypes.LanguageFileType) TopLevelMatchingHandler(com.intellij.structuralsearch.impl.matcher.handlers.TopLevelMatchingHandler) MatchingHandler(com.intellij.structuralsearch.impl.matcher.handlers.MatchingHandler) TopLevelMatchingHandler(com.intellij.structuralsearch.impl.matcher.handlers.TopLevelMatchingHandler) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with MatchingHandler

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

the class JavaMatchingVisitor method visitForStatement.

@Override
public void visitForStatement(final PsiForStatement for1) {
    final PsiForStatement for2 = (PsiForStatement) myMatchingVisitor.getElement();
    final PsiStatement initialization = for1.getInitialization();
    MatchingHandler handler = myMatchingVisitor.getMatchContext().getPattern().getHandler(initialization);
    myMatchingVisitor.setResult(handler.match(initialization, for2.getInitialization(), myMatchingVisitor.getMatchContext()) && myMatchingVisitor.match(for1.getCondition(), for2.getCondition()) && myMatchingVisitor.match(for1.getUpdate(), for2.getUpdate()) && compareBody(for1.getBody(), for2.getBody()));
}
Also used : 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