use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField in project intellij-community by JetBrains.
the class GrVariableInplaceRenameHandler method isAvailable.
@Override
protected boolean isAvailable(PsiElement element, Editor editor, PsiFile file) {
if (!editor.getSettings().isVariableInplaceRenameEnabled())
return false;
if (!(element instanceof GrVariable))
return false;
if (element instanceof GrField)
return false;
final SearchScope scope = element.getUseScope();
if (!(scope instanceof LocalSearchScope))
return false;
final PsiElement[] scopeElements = ((LocalSearchScope) scope).getScope();
return scopeElements.length == 1 || scopeElements.length == 2 && (scopeElements[0] instanceof GrDocComment ^ scopeElements[1] instanceof GrDocComment);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField in project intellij-community by JetBrains.
the class MoveGroovyMemberHandler method doMove.
@Override
@NotNull
public PsiMember doMove(@NotNull MoveMembersOptions options, @NotNull PsiMember member, PsiElement anchor, @NotNull PsiClass targetClass) {
GroovyChangeContextUtil.encodeContextInfo(member);
final PsiDocComment docComment;
if (member instanceof PsiDocCommentOwner) {
docComment = ((PsiDocCommentOwner) member).getDocComment();
} else {
docComment = null;
}
PsiMember moved;
if (options.makeEnumConstant() && member instanceof GrVariable && EnumConstantsUtil.isSuitableForEnumConstant(((PsiVariable) member).getType(), targetClass)) {
final GrEnumConstant prototype = createEnumConstant(member.getName(), ((GrVariable) member).getInitializerGroovy(), member.getProject());
moved = (PsiMember) addEnumConstant(targetClass, prototype, anchor);
member.delete();
} else if (member instanceof GrEnumConstant) {
moved = (PsiMember) addEnumConstant(targetClass, (GrEnumConstant) member, null);
} else if (member instanceof GrField) {
if (anchor != null)
anchor = anchor.getParent();
final GrVariableDeclaration parent = (GrVariableDeclaration) member.getParent();
GrVariableDeclaration movedDeclaration = (GrVariableDeclaration) targetClass.addAfter(parent, anchor);
int number = ArrayUtil.find(parent.getMembers(), member);
final GrMember[] members = movedDeclaration.getMembers();
for (int i = 0; i < number; i++) {
members[i].delete();
}
for (int i = number + 1; i < members.length; i++) {
members[i].delete();
}
if (member.getContainingClass().isInterface() && !targetClass.isInterface()) {
//might need to make modifiers explicit, see IDEADEV-11416
final PsiModifierList list = movedDeclaration.getModifierList();
VisibilityUtil.setVisibility(list, VisibilityUtil.getVisibilityModifier(member.getModifierList()));
list.setModifierProperty(PsiModifier.STATIC, member.hasModifierProperty(PsiModifier.STATIC));
list.setModifierProperty(PsiModifier.FINAL, member.hasModifierProperty(PsiModifier.FINAL));
}
moved = movedDeclaration.getMembers()[0];
} else if (member instanceof GrMethod) {
moved = (PsiMember) targetClass.addAfter(member, anchor);
if (member.getContainingClass().isInterface() && !targetClass.isInterface()) {
//might need to make modifiers explicit, see IDEADEV-11416
final PsiModifierList list = moved.getModifierList();
assert list != null;
list.setModifierProperty(PsiModifier.STATIC, member.hasModifierProperty(PsiModifier.STATIC));
list.setModifierProperty(PsiModifier.FINAL, member.hasModifierProperty(PsiModifier.FINAL));
VisibilityUtil.setVisibility(list, VisibilityUtil.getVisibilityModifier(member.getModifierList()));
}
} else {
moved = (PsiMember) targetClass.addAfter(member, anchor);
}
if (docComment != null) {
PsiElement insertedDocComment = targetClass.addBefore(docComment, moved);
PsiElement prevSibling = insertedDocComment.getPrevSibling();
addLineFeedIfNeeded(prevSibling);
docComment.delete();
}
member.delete();
return moved;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField in project intellij-community by JetBrains.
the class ConvertClosureToMethodIntention method processIntention.
@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
final GrField field;
if (element.getParent() instanceof GrField) {
field = (GrField) element.getParent();
} else {
final PsiReference ref = element.getReference();
LOG.assertTrue(ref != null);
PsiElement resolved = ref.resolve();
if (resolved instanceof GrAccessorMethod) {
resolved = ((GrAccessorMethod) resolved).getProperty();
}
LOG.assertTrue(resolved instanceof GrField);
field = (GrField) resolved;
}
final HashSet<PsiReference> usages = new HashSet<>();
usages.addAll(ReferencesSearch.search(field).findAll());
final GrAccessorMethod[] getters = field.getGetters();
for (GrAccessorMethod getter : getters) {
usages.addAll(MethodReferencesSearch.search(getter).findAll());
}
final GrAccessorMethod setter = field.getSetter();
if (setter != null) {
usages.addAll(MethodReferencesSearch.search(setter).findAll());
}
final String fieldName = field.getName();
LOG.assertTrue(fieldName != null);
final Collection<PsiElement> fieldUsages = new HashSet<>();
MultiMap<PsiElement, String> conflicts = new MultiMap<>();
for (PsiReference usage : usages) {
final PsiElement psiElement = usage.getElement();
if (PsiUtil.isMethodUsage(psiElement))
continue;
if (!GroovyLanguage.INSTANCE.equals(psiElement.getLanguage())) {
conflicts.putValue(psiElement, GroovyIntentionsBundle.message("closure.is.accessed.outside.of.groovy", fieldName));
} else {
if (psiElement instanceof GrReferenceExpression) {
fieldUsages.add(psiElement);
if (PsiUtil.isAccessedForWriting((GrExpression) psiElement)) {
conflicts.putValue(psiElement, GroovyIntentionsBundle.message("write.access.to.closure.variable", fieldName));
}
} else if (psiElement instanceof GrArgumentLabel) {
conflicts.putValue(psiElement, GroovyIntentionsBundle.message("field.is.used.in.argument.label", fieldName));
}
}
}
final PsiClass containingClass = field.getContainingClass();
final GrExpression initializer = field.getInitializerGroovy();
LOG.assertTrue(initializer != null);
final PsiType type = initializer.getType();
LOG.assertTrue(type instanceof GrClosureType);
final GrSignature signature = ((GrClosureType) type).getSignature();
final List<MethodSignature> signatures = GrClosureSignatureUtil.generateAllMethodSignaturesBySignature(fieldName, signature);
for (MethodSignature s : signatures) {
final PsiMethod method = MethodSignatureUtil.findMethodBySignature(containingClass, s, true);
if (method != null) {
conflicts.putValue(method, GroovyIntentionsBundle.message("method.with.signature.already.exists", GroovyPresentationUtil.getSignaturePresentation(s)));
}
}
if (!conflicts.isEmpty()) {
final ConflictsDialog conflictsDialog = new ConflictsDialog(project, conflicts, () -> execute(field, fieldUsages));
conflictsDialog.show();
if (conflictsDialog.getExitCode() != DialogWrapper.OK_EXIT_CODE)
return;
}
execute(field, fieldUsages);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField in project intellij-community by JetBrains.
the class GrVariableInliner method inlineReference.
static void inlineReference(UsageInfo usage, PsiElement referenced, GrExpression initializer) {
if (initializer == null)
return;
GrExpression exprToBeReplaced = (GrExpression) usage.getElement();
if (exprToBeReplaced == null)
return;
if ((referenced instanceof GrAccessorMethod || referenced instanceof GrField) && exprToBeReplaced instanceof GrReferenceExpression) {
final GroovyResolveResult resolveResult = ((GrReferenceExpression) exprToBeReplaced).advancedResolve();
if (resolveResult.getElement() instanceof GrAccessorMethod && !resolveResult.isInvokedOnProperty()) {
final PsiElement parent = exprToBeReplaced.getParent();
if (parent instanceof GrCall && parent instanceof GrExpression) {
exprToBeReplaced = (GrExpression) parent;
} else {
return;
}
}
}
GrExpression newExpr = exprToBeReplaced.replaceWithExpression((GrExpression) initializer.copy(), true);
final Project project = usage.getProject();
JavaCodeStyleManager.getInstance(project).shortenClassReferences(newExpr);
Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor();
GroovyRefactoringUtil.highlightOccurrences(project, editor, new PsiElement[] { newExpr });
WindowManager.getInstance().getStatusBar(project).setInfo(GroovyRefactoringBundle.message("press.escape.to.remove.the.highlighting"));
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField in project intellij-community by JetBrains.
the class GrIntroduceConstantProcessor method run.
@Nullable
public GrField run() {
final PsiClass targetClass = settings.getTargetClass();
if (targetClass == null)
return null;
if (checkErrors(targetClass)) {
return null;
}
final GrVariableDeclaration declaration = addDeclaration(targetClass);
final GrField field = (GrField) declaration.getVariables()[0];
GrVariable localVar = GrIntroduceHandlerBase.resolveLocalVar(context);
if (localVar != null) {
assert localVar.getInitializerGroovy() != null : "initializer should exist: " + localVar.getText();
GrIntroduceHandlerBase.deleteLocalVar(localVar);
if (settings.replaceAllOccurrences()) {
processOccurrences(field);
} else {
replaceOccurrence(field, localVar.getInitializerGroovy(), isEscalateVisibility());
}
} else if (context.getStringPart() != null) {
final GrExpression ref = context.getStringPart().replaceLiteralWithConcatenation(field.getName());
final PsiElement element = replaceOccurrence(field, ref, isEscalateVisibility());
updateCaretPosition(element);
} else if (context.getExpression() != null) {
if (settings.replaceAllOccurrences()) {
processOccurrences(field);
} else {
replaceOccurrence(field, context.getExpression(), isEscalateVisibility());
}
}
return field;
}
Aggregations