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