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();
}
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)));
}
}
Aggregations