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