use of com.intellij.structuralsearch.impl.matcher.handlers.SubstitutionHandler in project intellij-community by JetBrains.
the class JavaMatchingVisitor method saveOrDropResult.
private void saveOrDropResult(final PsiIdentifier methodNameNode, final boolean typedVar, final PsiIdentifier methodNameNode2) {
MatchResultImpl ourResult = myMatchingVisitor.getMatchContext().hasResult() ? myMatchingVisitor.getMatchContext().getResult() : null;
myMatchingVisitor.getMatchContext().popResult();
if (myMatchingVisitor.getResult()) {
if (typedVar) {
final SubstitutionHandler handler = (SubstitutionHandler) myMatchingVisitor.getMatchContext().getPattern().getHandler(methodNameNode);
if (ourResult != null)
ourResult.setScopeMatch(true);
handler.setNestedResult(ourResult);
myMatchingVisitor.setResult(handler.handle(methodNameNode2, myMatchingVisitor.getMatchContext()));
if (handler.getNestedResult() != null) {
// some constraint prevent from adding
handler.setNestedResult(null);
copyResults(ourResult);
}
} else if (ourResult != null) {
copyResults(ourResult);
}
}
}
use of com.intellij.structuralsearch.impl.matcher.handlers.SubstitutionHandler 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.SubstitutionHandler 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.SubstitutionHandler in project intellij-community by JetBrains.
the class CompiledPattern method createSubstitutionHandler.
public SubstitutionHandler createSubstitutionHandler(String name, String compiledName, boolean target, int minOccurs, int maxOccurs, boolean greedy) {
SubstitutionHandler handler = (SubstitutionHandler) handlers.get(compiledName);
if (handler != null)
return handler;
handler = doCreateSubstitutionHandler(name, target, minOccurs, maxOccurs, greedy);
handlers.put(compiledName, handler);
return handler;
}
use of com.intellij.structuralsearch.impl.matcher.handlers.SubstitutionHandler in project intellij-community by JetBrains.
the class GlobalCompilingVisitor method processPatternStringWithFragments.
@Nullable
public MatchingHandler processPatternStringWithFragments(String pattern, OccurenceKind kind, Pattern substitutionPattern) {
String content;
if (kind == OccurenceKind.LITERAL) {
content = pattern.substring(1, pattern.length() - 1);
} else if (kind == OccurenceKind.COMMENT) {
content = pattern;
} else {
return null;
}
@NonNls StringBuilder buf = new StringBuilder(content.length());
Matcher matcher = substitutionPattern.matcher(content);
List<SubstitutionHandler> handlers = null;
int start = 0;
String word;
boolean hasLiteralContent = false;
SubstitutionHandler handler = null;
while (matcher.find()) {
if (handlers == null)
handlers = new ArrayList<>();
handler = (SubstitutionHandler) getContext().getPattern().getHandler(matcher.group(1));
if (handler != null)
handlers.add(handler);
word = content.substring(start, matcher.start());
if (word.length() > 0) {
buf.append(StructuralSearchUtil.shieldSpecialChars(word));
hasLiteralContent = true;
processTokenizedName(word, false, kind);
}
RegExpPredicate predicate = MatchingHandler.getSimpleRegExpPredicate(handler);
if (predicate == null || !predicate.isWholeWords()) {
buf.append("(.*?)");
} else {
buf.append(".*?\\b(").append(predicate.getRegExp()).append(")\\b.*?");
}
if (isSuitablePredicate(predicate, handler)) {
processTokenizedName(predicate.getRegExp(), false, kind);
}
start = matcher.end();
}
word = content.substring(start, content.length());
if (word.length() > 0) {
hasLiteralContent = true;
buf.append(StructuralSearchUtil.shieldSpecialChars(word));
processTokenizedName(word, false, kind);
}
if (hasLiteralContent) {
if (kind == OccurenceKind.LITERAL) {
buf.insert(0, "[\"']");
buf.append("[\"']");
}
buf.append("$");
}
if (handlers != null) {
return hasLiteralContent ? new LiteralWithSubstitutionHandler(buf.toString(), handlers) : handler;
}
return null;
}
Aggregations