use of org.jetbrains.plugins.groovy.lang.psi.api.util.GrVariableDeclarationOwner in project intellij-community by JetBrains.
the class GroovyMethodInliner method isOnExpressionOrReturnPlace.
/*
Method call is used as expression in some enclosing expression or
is method return result
*/
private static boolean isOnExpressionOrReturnPlace(@NotNull GrCallExpression call) {
PsiElement parent = call.getParent();
if (!(parent instanceof GrVariableDeclarationOwner)) {
return true;
}
// tail calls in methods and closures
GrVariableDeclarationOwner owner = (GrVariableDeclarationOwner) parent;
if (owner instanceof GrClosableBlock || owner instanceof GrOpenBlock && owner.getParent() instanceof GrMethod) {
GrStatement[] statements = ((GrCodeBlock) owner).getStatements();
assert statements.length > 0;
GrStatement last = statements[statements.length - 1];
if (last == call)
return true;
if (last instanceof GrReturnStatement && call == ((GrReturnStatement) last).getReturnValue()) {
return true;
}
}
return false;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.util.GrVariableDeclarationOwner in project intellij-community by JetBrains.
the class InlineMethodConflictSolver method suggestNewName.
@NotNull
public static String suggestNewName(@NotNull String startName, @Nullable GrMethod method, @NotNull PsiElement call, String... otherNames) {
String newName;
int i = 1;
PsiElement parent = call.getParent();
while (!(parent instanceof GrVariableDeclarationOwner) && parent != null) {
parent = parent.getParent();
}
if (parent == null || isValidName(startName, parent, call) && isValid(startName, otherNames)) {
return startName;
}
do {
newName = startName + i;
i++;
} while (!((method == null || isValidNameInMethod(newName, method)) && isValidName(newName, parent, call) && isValid(newName, otherNames)));
return newName;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.util.GrVariableDeclarationOwner in project intellij-community by JetBrains.
the class GroovyRefactoringUtil method createTempVar.
public static String createTempVar(GrExpression expr, final GroovyPsiElement context, boolean declareFinal) {
expr = addBlockIntoParent(expr);
final GrVariableDeclarationOwner block = PsiTreeUtil.getParentOfType(expr, GrVariableDeclarationOwner.class);
LOG.assertTrue(block != null);
final PsiElement anchorStatement = PsiTreeUtil.findPrevParent(block, expr);
LOG.assertTrue(anchorStatement instanceof GrStatement);
Project project = expr.getProject();
String[] suggestedNames = GroovyNameSuggestionUtil.suggestVariableNames(expr, new NameValidator() {
@Override
public String validateName(String name, boolean increaseNumber) {
return name;
}
@Override
public Project getProject() {
return context.getProject();
}
});
/*
JavaCodeStyleManager.getInstance(project).suggestVariableName(VariableKind.LOCAL_VARIABLE, null, expr, null).names;*/
final String prefix = suggestedNames[0];
final String id = JavaCodeStyleManager.getInstance(project).suggestUniqueVariableName(prefix, context, true);
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(expr.getProject());
String[] modifiers;
if (declareFinal) {
modifiers = finalModifiers;
} else {
modifiers = ArrayUtil.EMPTY_STRING_ARRAY;
}
GrVariableDeclaration decl = factory.createVariableDeclaration(modifiers, (GrExpression) org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil.skipParentheses(expr, false), expr.getType(), id);
/* if (declareFinal) {
com.intellij.psi.util.PsiUtil.setModifierProperty((decl.getMembers()[0]), PsiModifier.FINAL, true);
}*/
final GrStatement statement = ((GrStatementOwner) anchorStatement.getParent()).addStatementBefore(decl, (GrStatement) anchorStatement);
JavaCodeStyleManager.getInstance(statement.getProject()).shortenClassReferences(statement);
return id;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.util.GrVariableDeclarationOwner in project intellij-community by JetBrains.
the class GroovyRefactoringUtil method addBlockIntoParent.
/**
* adds block statement in parent of statement if needed. For Example:
* while (true) a=foo()
* will be replaced with
* while(true) {a=foo()}
* @param statement
* @return corresponding statement inside block if it has been created or statement itself.
* @throws com.intellij.util.IncorrectOperationException
*/
@NotNull
public static <Type extends PsiElement> Type addBlockIntoParent(@NotNull Type statement) throws IncorrectOperationException {
PsiElement parent = statement.getParent();
PsiElement child = statement;
while (!(parent instanceof GrLoopStatement) && !(parent instanceof GrIfStatement) && !(parent instanceof GrVariableDeclarationOwner) && parent != null) {
parent = parent.getParent();
child = child.getParent();
}
if (parent instanceof GrWhileStatement && child == ((GrWhileStatement) parent).getCondition() || parent instanceof GrIfStatement && child == ((GrIfStatement) parent).getCondition()) {
parent = parent.getParent();
}
assert parent != null;
if (parent instanceof GrVariableDeclarationOwner) {
return statement;
}
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(statement.getProject());
PsiElement tempStmt = statement;
while (parent != tempStmt.getParent()) {
tempStmt = tempStmt.getParent();
}
GrStatement toAdd = (GrStatement) tempStmt.copy();
GrBlockStatement blockStatement = factory.createBlockStatement();
if (parent instanceof GrLoopStatement) {
((GrLoopStatement) parent).replaceBody(blockStatement);
} else {
GrIfStatement ifStatement = (GrIfStatement) parent;
if (tempStmt == ifStatement.getThenBranch()) {
ifStatement.replaceThenBranch(blockStatement);
} else if (tempStmt == ifStatement.getElseBranch()) {
ifStatement.replaceElseBranch(blockStatement);
}
}
GrStatement result = blockStatement.getBlock().addStatementBefore(toAdd, null);
if (result instanceof GrReturnStatement) {
//noinspection ConstantConditions,unchecked
statement = (Type) ((GrReturnStatement) result).getReturnValue();
} else {
//noinspection unchecked
statement = (Type) result;
}
return statement;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.util.GrVariableDeclarationOwner in project intellij-community by JetBrains.
the class GroovyInlineLocalProcessor method performRefactoring.
@Override
protected void performRefactoring(@NotNull UsageInfo[] usages) {
CommonRefactoringUtil.sortDepthFirstRightLeftOrder(usages);
final GrExpression initializer = mySettings.getInitializer();
GrExpression initializerToUse = GrIntroduceHandlerBase.insertExplicitCastIfNeeded(myLocal, mySettings.getInitializer());
for (UsageInfo usage : usages) {
GrVariableInliner.inlineReference(usage, myLocal, initializerToUse);
}
final PsiElement initializerParent = initializer.getParent();
if (initializerParent instanceof GrAssignmentExpression) {
initializerParent.delete();
return;
}
if (initializerParent instanceof GrVariable) {
final Collection<PsiReference> all = ReferencesSearch.search(myLocal).findAll();
if (!all.isEmpty()) {
initializer.delete();
return;
}
}
final PsiElement owner = myLocal.getParent().getParent();
if (owner instanceof GrVariableDeclarationOwner) {
((GrVariableDeclarationOwner) owner).removeVariable(myLocal);
} else {
myLocal.delete();
}
}
Aggregations