use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration in project intellij-community by JetBrains.
the class GrSetStrongTypeIntention method getElementPredicate.
@NotNull
@Override
protected PsiElementPredicate getElementPredicate() {
return new PsiElementPredicate() {
@Override
public boolean satisfiedBy(PsiElement element) {
PsiElement parent = element.getParent();
PsiElement pparent;
if (isNameIdentifierOfVariable(element, parent) || isModifierListOfVar(element, parent)) {
pparent = parent.getParent();
} else if (isModifierListOfVarDecl(element, parent)) {
pparent = parent;
} else {
return false;
}
if (pparent instanceof GrVariableDeclaration) {
if (((GrVariableDeclaration) pparent).getTypeElementGroovy() != null)
return false;
GrVariable[] variables = ((GrVariableDeclaration) pparent).getVariables();
for (GrVariable variable : variables) {
if (isVarDeclaredWithInitializer(variable))
return true;
}
} else if (pparent instanceof GrForInClause) {
final GrVariable variable = ((GrForInClause) pparent).getDeclaredVariable();
return variable != null && variable.getTypeElementGroovy() == null && PsiUtil.extractIteratedType((GrForInClause) pparent) != null;
} else if (parent instanceof GrParameter && pparent instanceof GrParameterList) {
return ((GrParameter) parent).getTypeElementGroovy() == null && getClosureParameterType((PsiParameter) parent) != null;
} else {
final GrVariable variable = (GrVariable) parent;
return variable.getTypeElementGroovy() == null && isVarDeclaredWithInitializer(variable);
}
return false;
}
private boolean isModifierListOfVarDecl(PsiElement element, PsiElement parent) {
return parent instanceof GrVariableDeclaration && ((GrVariableDeclaration) parent).getModifierList() == element;
}
private boolean isModifierListOfVar(PsiElement element, PsiElement parent) {
return parent instanceof GrVariable && ((GrVariable) parent).getModifierList() == element;
}
private boolean isNameIdentifierOfVariable(PsiElement element, PsiElement parent) {
return parent instanceof GrVariable && ((GrVariable) parent).getTypeElementGroovy() == null && element == ((GrVariable) parent).getNameIdentifierGroovy();
}
};
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration 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.GrVariableDeclaration in project intellij-community by JetBrains.
the class ConvertSimpleGetterToPropertyIntention method processIntention.
@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
GrMethod method = (GrMethod) element.getParent();
GrOpenBlock block = method.getBlock();
if (block == null)
return;
GrStatement statement = block.getStatements()[0];
GrExpression value;
if (statement instanceof GrReturnStatement) {
value = ((GrReturnStatement) statement).getReturnValue();
} else {
value = (GrExpression) statement;
}
String fieldName = GroovyPropertyUtils.getPropertyNameByGetter(method);
if (fieldName == null)
return;
PsiClass aClass = method.getContainingClass();
if (aClass == null)
return;
List<String> modifiers = ContainerUtil.newArrayList();
for (String modifier : MODIFIERS_TO_CHECK) {
if (method.hasModifierProperty(modifier))
modifiers.add(modifier);
}
modifiers.add(PsiModifier.FINAL);
GrTypeElement returnTypeElement = method.getReturnTypeElementGroovy();
PsiType returnType = returnTypeElement == null ? null : returnTypeElement.getType();
GrVariableDeclaration declaration = GroovyPsiElementFactory.getInstance(project).createFieldDeclaration(ArrayUtil.toStringArray(modifiers), fieldName, value, returnType);
PsiElement replaced = method.replace(declaration);
JavaCodeStyleManager.getInstance(project).shortenClassReferences(replaced);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration in project intellij-community by JetBrains.
the class GrSplitDeclarationIntention method getElementPredicate.
@NotNull
@Override
protected PsiElementPredicate getElementPredicate() {
return new PsiElementPredicate() {
@Override
public boolean satisfiedBy(PsiElement element) {
if (element instanceof GrVariableDeclaration) {
GrVariableDeclaration decl = (GrVariableDeclaration) element;
GrVariable[] variables = decl.getVariables();
if (variables.length > 1 && PsiUtil.isLocalVariable(variables[0])) {
if (!decl.isTuple() || decl.getTupleInitializer() instanceof GrListOrMap) {
myText = GroovyIntentionsBundle.message("split.into.separate.declaration");
} else {
myText = GroovyIntentionsBundle.message("split.into.declaration.and.assignment");
}
return true;
} else if (variables.length == 1 && PsiUtil.isLocalVariable(variables[0]) && variables[0].getInitializerGroovy() != null) {
myText = GroovyIntentionsBundle.message("split.into.declaration.and.assignment");
return true;
}
}
return false;
}
};
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration 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