use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement in project intellij-community by JetBrains.
the class GroovyCodeFragmentFactory method externalParameters.
public static Pair<Map<String, String>, GroovyFile> externalParameters(String text, @NotNull final PsiElement context) {
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(context.getProject());
final GroovyFile toEval = factory.createGroovyFile(text, false, context);
final GrClosableBlock closure = PsiTreeUtil.getParentOfType(context, GrClosableBlock.class);
final Map<String, String> parameters = new THashMap<>();
final Map<GrExpression, String> replacements = new HashMap<>();
toEval.accept(new GroovyRecursiveElementVisitor() {
@Override
public void visitReferenceExpression(@NotNull GrReferenceExpression referenceExpression) {
super.visitReferenceExpression(referenceExpression);
if (PsiUtil.isThisReference(referenceExpression) || PsiUtil.isSuperReference(referenceExpression)) {
replaceWithReference(referenceExpression, "delegate");
return;
}
PsiElement resolved = referenceExpression.resolve();
if (resolved instanceof PsiMember && (resolved instanceof PsiClass || ((PsiMember) resolved).hasModifierProperty(PsiModifier.STATIC))) {
String qName = com.intellij.psi.util.PsiUtil.getMemberQualifiedName((PsiMember) resolved);
if (qName != null && qName.contains(".") && !referenceExpression.isQualified()) {
replaceWithReference(referenceExpression, qName);
return;
}
}
if (shouldDelegate(referenceExpression, resolved)) {
replaceWithReference(referenceExpression, "delegate." + referenceExpression.getReferenceName());
return;
}
if (resolved instanceof GrVariable && !(resolved instanceof GrField) && !PsiTreeUtil.isAncestor(toEval, resolved, false)) {
final String name = ((GrVariable) resolved).getName();
if (resolved instanceof ClosureSyntheticParameter && PsiTreeUtil.isAncestor(toEval, ((ClosureSyntheticParameter) resolved).getClosure(), false)) {
return;
}
if (resolved instanceof GrBindingVariable && !PsiTreeUtil.isAncestor(resolved.getContainingFile(), toEval, false)) {
return;
}
String value;
if (closure != null && PsiTreeUtil.findCommonParent(resolved, closure) != closure && !(resolved instanceof ClosureSyntheticParameter)) {
// Evaluating inside closure for outer variable definitions
// All non-local variables are accessed by references
value = "this." + name;
} else {
value = name;
}
parameters.put(name, value);
return;
}
if (resolved instanceof PsiLocalVariable || resolved instanceof PsiParameter && !(resolved instanceof GrParameter)) {
String name = referenceExpression.getReferenceName();
parameters.put(name, name);
}
}
private boolean shouldDelegate(GrReferenceExpression referenceExpression, @Nullable PsiElement resolved) {
if (referenceExpression.isQualified()) {
return false;
}
if (resolved instanceof GrField) {
return true;
}
if (resolved instanceof PsiMethod) {
String methodName = ((PsiMethod) resolved).getName();
if (closure != null && "getDelegate".equals(methodName) || "call".equals(methodName)) {
return true;
}
}
return closure != null && resolved instanceof GrLightVariable && "owner".equals(((GrLightVariable) resolved).getName());
}
private void replaceWithReference(GrExpression expr, final String exprText) {
replacements.put(expr, exprText);
}
@Override
public void visitCodeReferenceElement(@NotNull GrCodeReferenceElement refElement) {
super.visitCodeReferenceElement(refElement);
if (refElement.getQualifier() == null) {
PsiElement resolved = refElement.resolve();
if (resolved instanceof PsiClass) {
String qName = ((PsiClass) resolved).getQualifiedName();
if (qName != null) {
int dotIndex = qName.lastIndexOf(".");
if (dotIndex < 0)
return;
String packageName = qName.substring(0, dotIndex);
refElement.setQualifier(factory.createReferenceElementFromText(packageName));
}
}
}
}
});
for (GrExpression expression : replacements.keySet()) {
expression.replaceWithExpression(factory.createExpressionFromText(replacements.get(expression)), false);
}
return Pair.create(parameters, toEval);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement in project intellij-community by JetBrains.
the class GroovyChangeContextUtil method encodeContextInfo.
public static void encodeContextInfo(PsiElement element, PsiElement scope) {
if (!(element instanceof GroovyPsiElement))
return;
if (PsiUtil.isThisReference(element)) {
GrReferenceExpression thisExpr = (GrReferenceExpression) element;
final PsiClass containingClass = PsiTreeUtil.getParentOfType(thisExpr, PsiClass.class);
element.putCopyableUserData(KEY_ENCODED, KEY_ENCODED);
thisExpr.putCopyableUserData(QUALIFIER_CLASS_KEY, containingClass);
} else if (element instanceof GrReferenceExpression) {
GrReferenceExpression refExpr = (GrReferenceExpression) element;
final GrExpression qualifier = refExpr.getQualifierExpression();
if (qualifier == null) {
PsiElement refElement = refExpr.resolve();
element.putCopyableUserData(KEY_ENCODED, KEY_ENCODED);
if (refElement != null && !PsiTreeUtil.isContextAncestor(scope, refElement, false)) {
if (refElement instanceof GrAccessorMethod)
refElement = ((GrAccessorMethod) refElement).getProperty();
if (refElement instanceof PsiClass) {
refExpr.putCopyableUserData(REF_TO_CLASS, (PsiClass) refElement);
} else if (refElement instanceof PsiMember) {
refExpr.putCopyableUserData(REF_TO_MEMBER, (PsiMember) refElement);
}
}
}
} else if (element instanceof GrCodeReferenceElement) {
final PsiElement resolvedElement = ((GrCodeReferenceElement) element).resolve();
element.putCopyableUserData(KEY_ENCODED, KEY_ENCODED);
if (resolvedElement instanceof PsiClass && !PsiTreeUtil.isContextAncestor(scope, resolvedElement, false)) {
element.putCopyableUserData(REF_TO_CLASS, (PsiClass) resolvedElement);
}
}
for (PsiElement child = element.getFirstChild(); child != null; child = child.getNextSibling()) {
encodeContextInfo(child, scope);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement in project intellij-community by JetBrains.
the class GroovyRefactoringUtil method isDiamondNewOperator.
public static boolean isDiamondNewOperator(GrExpression expression) {
PsiElement element = org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil.skipParentheses(expression, false);
if (!(element instanceof GrNewExpression))
return false;
if (((GrNewExpression) element).getArrayCount() > 0)
return false;
GrCodeReferenceElement referenceElement = ((GrNewExpression) element).getReferenceElement();
if (referenceElement == null)
return false;
GrTypeArgumentList typeArgumentList = referenceElement.getTypeArgumentList();
return typeArgumentList != null && typeArgumentList.isDiamond();
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement in project intellij-community by JetBrains.
the class ClassItemGeneratorImpl method isInList.
private static boolean isInList(@NotNull PsiClassType type, @Nullable GrReferenceList list) {
if (list == null)
return false;
PsiClass resolved = type.resolve();
if (resolved == null)
return true;
PsiManager manager = list.getManager();
GrCodeReferenceElement[] elements = list.getReferenceElementsGroovy();
for (GrCodeReferenceElement element : elements) {
if (manager.areElementsEquivalent(resolved, element.resolve())) {
return true;
}
}
return false;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement in project intellij-community by JetBrains.
the class ExpressionGenerator method hasFieldInitialization.
private static boolean hasFieldInitialization(GrNewExpression newExpression) {
final GrArgumentList argumentList = newExpression.getArgumentList();
if (argumentList == null)
return false;
if (argumentList.getNamedArguments().length == 0)
return false;
final GrCodeReferenceElement refElement = newExpression.getReferenceElement();
if (refElement == null)
return false;
final GroovyResolveResult resolveResult = newExpression.advancedResolve();
final PsiElement constructor = resolveResult.getElement();
if (constructor instanceof PsiMethod) {
return ((PsiMethod) constructor).getParameterList().getParametersCount() == 0;
}
final PsiElement resolved = refElement.resolve();
return resolved instanceof PsiClass;
}
Aggregations