Search in sources :

Example 1 with TopLevelMatchingHandler

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

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

the class XmlCompilingVisitor method visitXmlTag.

@Override
public void visitXmlTag(XmlTag xmlTag) {
    myCompilingVisitor.setCodeBlockLevel(myCompilingVisitor.getCodeBlockLevel() + 1);
    super.visitXmlTag(xmlTag);
    myCompilingVisitor.setCodeBlockLevel(myCompilingVisitor.getCodeBlockLevel() - 1);
    if (myCompilingVisitor.getCodeBlockLevel() == 1) {
        final CompiledPattern pattern = myCompilingVisitor.getContext().getPattern();
        pattern.setStrategy(XmlMatchingStrategy.getInstance());
        pattern.setHandler(xmlTag, new TopLevelMatchingHandler(pattern.getHandler(xmlTag)));
    }
}
Also used : CompiledPattern(com.intellij.structuralsearch.impl.matcher.CompiledPattern) TopLevelMatchingHandler(com.intellij.structuralsearch.impl.matcher.handlers.TopLevelMatchingHandler)

Aggregations

TopLevelMatchingHandler (com.intellij.structuralsearch.impl.matcher.handlers.TopLevelMatchingHandler)2 LanguageFileType (com.intellij.openapi.fileTypes.LanguageFileType)1 CompiledPattern (com.intellij.structuralsearch.impl.matcher.CompiledPattern)1 MatchingHandler (com.intellij.structuralsearch.impl.matcher.handlers.MatchingHandler)1 CollectingMatchResultSink (com.intellij.structuralsearch.plugin.util.CollectingMatchResultSink)1 NotNull (org.jetbrains.annotations.NotNull)1