Search in sources :

Example 1 with GrVariableDeclarationOwner

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;
}
Also used : GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) GrReturnStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement) GrVariableDeclarationOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrVariableDeclarationOwner) GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 2 with GrVariableDeclarationOwner

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;
}
Also used : GrVariableDeclarationOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrVariableDeclarationOwner) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with GrVariableDeclarationOwner

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;
}
Also used : Project(com.intellij.openapi.project.Project) GrStatementOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner) GrVariableDeclarationOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrVariableDeclarationOwner) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement)

Example 4 with GrVariableDeclarationOwner

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;
}
Also used : GrReturnStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement) GrVariableDeclarationOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrVariableDeclarationOwner) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with GrVariableDeclarationOwner

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();
    }
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) PsiReference(com.intellij.psi.PsiReference) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrVariableDeclarationOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrVariableDeclarationOwner) UsageInfo(com.intellij.usageView.UsageInfo) PsiElement(com.intellij.psi.PsiElement)

Aggregations

GrVariableDeclarationOwner (org.jetbrains.plugins.groovy.lang.psi.api.util.GrVariableDeclarationOwner)6 GrReturnStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement)3 Project (com.intellij.openapi.project.Project)2 PsiElement (com.intellij.psi.PsiElement)2 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)2 NotNull (org.jetbrains.annotations.NotNull)2 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)2 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)2 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)2 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)2 GrStatementOwner (org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner)2 RangeMarker (com.intellij.openapi.editor.RangeMarker)1 TextRange (com.intellij.openapi.util.TextRange)1 PsiReference (com.intellij.psi.PsiReference)1 UsageInfo (com.intellij.usageView.UsageInfo)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 Nullable (org.jetbrains.annotations.Nullable)1 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)1 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)1 GrVariableDeclaration (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration)1