use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable 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.GrVariable 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.GrVariable in project intellij-community by JetBrains.
the class GroovyIntroduceParameterUtil method findVar.
@Nullable
static GrVariable findVar(IntroduceParameterInfo info) {
GrVariable variable = info.getVar();
if (variable != null)
return variable;
final GrStatement[] statements = info.getStatements();
if (statements.length != 1)
return null;
return GrIntroduceHandlerBase.findVariable(statements[0]);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable in project intellij-community by JetBrains.
the class GroovyIntroduceParameterUtil method getOccurrences.
static PsiElement[] getOccurrences(GrIntroduceParameterSettings settings) {
final GrParametersOwner scope = settings.getToReplaceIn();
final GrExpression expression = settings.getExpression();
if (expression != null) {
final PsiElement expr = PsiUtil.skipParentheses(expression, false);
if (expr == null)
return PsiElement.EMPTY_ARRAY;
final PsiElement[] occurrences = GroovyRefactoringUtil.getExpressionOccurrences(expr, scope);
if (occurrences == null || occurrences.length == 0) {
throw new GrRefactoringError(GroovyRefactoringBundle.message("no.occurrences.found"));
}
return occurrences;
} else {
final GrVariable var = settings.getVar();
LOG.assertTrue(var != null);
final List<PsiElement> list = Collections.synchronizedList(new ArrayList<PsiElement>());
ReferencesSearch.search(var, new LocalSearchScope(scope)).forEach(psiReference -> {
final PsiElement element = psiReference.getElement();
if (element != null) {
list.add(element);
}
return true;
});
return list.toArray(new PsiElement[list.size()]);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable in project intellij-community by JetBrains.
the class GrInplaceVariableIntroducer method addAdditionalVariables.
@Override
protected void addAdditionalVariables(TemplateBuilderImpl builder) {
GrVariable variable = getVariable();
assert variable != null && variable.getInitializerGroovy() != null;
final PsiType initializerType = variable.getInitializerGroovy().getType();
TypeConstraint[] constraints = initializerType != null && !initializerType.equals(PsiType.NULL) ? new SupertypeConstraint[] { SupertypeConstraint.create(initializerType) } : TypeConstraint.EMPTY_ARRAY;
ChooseTypeExpression typeExpression = new ChooseTypeExpression(constraints, variable.getManager(), variable.getResolveScope(), true, GroovyApplicationSettings.getInstance().INTRODUCE_LOCAL_SELECT_DEF);
PsiElement element = getTypeELementOrDef(variable);
if (element == null)
return;
builder.replaceElement(element, "Variable_type", typeExpression, true, true);
}
Aggregations