use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression in project intellij-community by JetBrains.
the class GrAliasImportIntention method getElementPredicate.
@NotNull
@Override
protected PsiElementPredicate getElementPredicate() {
return new PsiElementPredicate() {
@Override
public boolean satisfiedBy(PsiElement element) {
if (element instanceof GrReferenceExpression) {
GroovyResolveResult result = ((GrReferenceExpression) element).advancedResolve();
PsiElement context = result.getCurrentFileResolveContext();
if (!(context instanceof GrImportStatement))
return false;
GrImportStatement importStatement = (GrImportStatement) context;
if (!importStatement.isStatic() || importStatement.isAliasedImport())
return false;
return true;
} else if (element instanceof GrImportStatement) {
final GrImportStatement importStatement = (GrImportStatement) element;
if (!importStatement.isStatic())
return false;
if (importStatement.isOnDemand())
return false;
if (importStatement.isAliasedImport())
return false;
return true;
}
return false;
}
};
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression in project intellij-community by JetBrains.
the class GrPullUpHelper method postProcessMember.
@Override
public void postProcessMember(PsiMember member) {
((GrMember) member).accept(myExplicitSuperDeleter);
((GrMember) member).accept(myThisSuperAdjuster);
GroovyChangeContextUtil.decodeContextInfo(member, null, null);
((GroovyPsiElement) member).accept(new GroovyRecursiveElementVisitor() {
@Override
public void visitReferenceExpression(@NotNull GrReferenceExpression referenceExpression) {
if (processRef(referenceExpression))
return;
super.visitReferenceExpression(referenceExpression);
}
@Override
public void visitCodeReferenceElement(@NotNull GrCodeReferenceElement refElement) {
if (processRef(refElement))
return;
super.visitCodeReferenceElement(refElement);
}
private boolean processRef(@NotNull GrReferenceElement<? extends GroovyPsiElement> refElement) {
final PsiElement qualifier = refElement.getQualifier();
if (qualifier != null) {
final Boolean preserveQualifier = qualifier.getCopyableUserData(PRESERVE_QUALIFIER);
if (preserveQualifier != null && !preserveQualifier) {
refElement.setQualifier(null);
return true;
}
}
return false;
}
});
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression in project intellij-community by JetBrains.
the class RenameGrFieldProcessor method qualify.
private static void qualify(PsiMember member, GrReferenceExpression refExpr) {
String name = refExpr.getReferenceName();
final PsiClass clazz = member.getContainingClass();
if (clazz == null)
return;
if (refExpr.getQualifierExpression() != null)
return;
final PsiElement replaced;
if (member.hasModifierProperty(PsiModifier.STATIC)) {
final GrReferenceExpression newRefExpr = GroovyPsiElementFactory.getInstance(member.getProject()).createReferenceExpressionFromText(clazz.getQualifiedName() + "." + name);
replaced = refExpr.replace(newRefExpr);
} else {
final PsiClass containingClass = PsiTreeUtil.getParentOfType(refExpr, PsiClass.class);
if (member.getManager().areElementsEquivalent(containingClass, clazz)) {
final GrReferenceExpression newRefExpr = GroovyPsiElementFactory.getInstance(member.getProject()).createReferenceExpressionFromText("this." + name);
replaced = refExpr.replace(newRefExpr);
} else {
final GrReferenceExpression newRefExpr = GroovyPsiElementFactory.getInstance(member.getProject()).createReferenceExpressionFromText(clazz.getQualifiedName() + ".this." + name);
replaced = refExpr.replace(newRefExpr);
}
}
JavaCodeStyleManager.getInstance(replaced.getProject()).shortenClassReferences(replaced);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression in project intellij-community by JetBrains.
the class RenameGrFieldProcessor method renameElement.
@Override
public void renameElement(final PsiElement psiElement, String newName, final UsageInfo[] usages, @Nullable RefactoringElementListener listener) throws IncorrectOperationException {
GrField field = (GrField) psiElement;
Map<GrReferenceExpression, PsiElement> handled = ContainerUtil.newHashMap();
for (UsageInfo usage : usages) {
final PsiReference ref = usage.getReference();
if (ref instanceof GrReferenceExpression) {
PsiElement resolved = ref.resolve();
ref.handleElementRename(getNewNameFromTransformations(resolved, newName));
handled.put((GrReferenceExpression) ref, resolved);
} else if (ref != null) {
handleElementRename(newName, ref, field.getName());
}
}
field.setName(newName);
PsiManager manager = psiElement.getManager();
for (GrReferenceExpression expression : handled.keySet()) {
PsiElement oldResolved = handled.get(expression);
if (oldResolved == null)
continue;
PsiElement resolved = expression.resolve();
if (resolved == null)
continue;
if (manager.areElementsEquivalent(oldResolved, resolved))
continue;
if (oldResolved.equals(field) || isQualificationNeeded(manager, oldResolved, resolved)) {
qualify(field, expression);
}
}
if (listener != null) {
listener.elementRenamed(psiElement);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression in project intellij-community by JetBrains.
the class RenameGrFieldProcessor method findCollisions.
@Override
public void findCollisions(PsiElement element, String newName, Map<? extends PsiElement, String> allRenames, List<UsageInfo> result) {
List<UsageInfo> collisions = new ArrayList<>();
for (UsageInfo info : result) {
if (!(info instanceof MoveRenameUsageInfo))
continue;
final PsiElement infoElement = info.getElement();
final PsiElement referencedElement = ((MoveRenameUsageInfo) info).getReferencedElement();
if (!(infoElement instanceof GrReferenceExpression))
continue;
final GrReferenceExpression refExpr = (GrReferenceExpression) infoElement;
if (!(referencedElement instanceof GrField || refExpr.advancedResolve().isInvokedOnProperty()))
continue;
if (!(refExpr.getParent() instanceof GrCall))
continue;
final PsiType[] argTypes = PsiUtil.getArgumentTypes(refExpr, false);
final PsiType[] typeArguments = refExpr.getTypeArguments();
final MethodResolverProcessor processor = new MethodResolverProcessor(newName, refExpr, false, null, argTypes, typeArguments);
final PsiMethod resolved = ResolveUtil.resolveExistingElement(refExpr, processor, PsiMethod.class);
if (resolved == null)
continue;
collisions.add(new UnresolvableCollisionUsageInfo(resolved, refExpr) {
@Override
public String getDescription() {
return GroovyRefactoringBundle.message("usage.will.be.overriden.by.method", refExpr.getParent().getText(), PsiFormatUtil.formatMethod(resolved, PsiSubstitutor.EMPTY, PsiFormatUtilBase.SHOW_NAME, PsiFormatUtilBase.SHOW_TYPE));
}
});
}
result.addAll(collisions);
super.findCollisions(element, newName, allRenames, result);
}
Aggregations