Search in sources :

Example 6 with GrStatement

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement in project intellij-community by JetBrains.

the class GroovyElseRemover method deleteSelectedElseIf.

private static void deleteSelectedElseIf(GrIfStatement selectedBranch, Context context) throws IncorrectOperationException {
    GrIfStatement parentIf = (GrIfStatement) selectedBranch.getParent();
    GrStatement childElse = selectedBranch.getElseBranch();
    if (childElse == null) {
        context.delete(selectedBranch);
        return;
    }
    context.setElseBranch(parentIf, childElse);
}
Also used : GrIfStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 7 with GrStatement

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement in project intellij-community by JetBrains.

the class GroovyElseUnwrapperBase method doUnwrap.

@Override
protected void doUnwrap(PsiElement element, Context context) throws IncorrectOperationException {
    GrStatement elseBranch;
    if (isElseKeyword(element)) {
        elseBranch = ((GrIfStatement) element.getParent()).getElseBranch();
    } else {
        elseBranch = (GrStatement) element;
    }
    unwrapElseBranch(elseBranch, element.getParent(), context);
}
Also used : GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 8 with GrStatement

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement in project intellij-community by JetBrains.

the class GrIntroduceVariableHandler method addVariable.

private static GrVariable addVariable(@NotNull GrIntroduceContext context, @NotNull GroovyIntroduceVariableSettings settings) {
    GrStatement anchor = findAnchor(context, settings.replaceAllOccurrences());
    PsiElement parent = anchor.getParent();
    assert parent instanceof GrStatementOwner;
    GrVariableDeclaration generated = generateDeclaration(context, settings);
    GrStatement declaration = ((GrStatementOwner) parent).addStatementBefore(generated, anchor);
    declaration = (GrStatement) JavaCodeStyleManager.getInstance(context.getProject()).shortenClassReferences(declaration);
    return ((GrVariableDeclaration) declaration).getVariables()[0];
}
Also used : GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GrStatementOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner) PsiElement(com.intellij.psi.PsiElement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 9 with GrStatement

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement 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]);
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) Nullable(org.jetbrains.annotations.Nullable)

Example 10 with GrStatement

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement in project intellij-community by JetBrains.

the class TrySurrounder method getSurroundSelectionRange.

@Override
protected TextRange getSurroundSelectionRange(GroovyPsiElement element) {
    assert element instanceof GrTryCatchStatement;
    int endOffset = element.getTextRange().getEndOffset();
    GrTryCatchStatement tryCatchStatement = (GrTryCatchStatement) element;
    GrCatchClause[] catchClauses = tryCatchStatement.getCatchClauses();
    if (catchClauses != null && catchClauses.length > 0) {
        GrParameter parameter = catchClauses[0].getParameter();
        if (parameter == null) {
            GrOpenBlock block = catchClauses[0].getBody();
            assert block != null;
            endOffset = block.getTextRange().getEndOffset();
        } else {
            endOffset = parameter.getTextRange().getStartOffset();
            parameter.getParent().getNode().removeChild(parameter.getNode());
        }
    } else {
        GrOpenBlock block = tryCatchStatement.getTryBlock();
        if (block != null) {
            GrStatement[] statements = block.getStatements();
            if (statements.length > 0) {
                endOffset = statements[0].getTextRange().getStartOffset();
            }
        }
    }
    return new TextRange(endOffset, endOffset);
}
Also used : TextRange(com.intellij.openapi.util.TextRange) GrTryCatchStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrTryCatchStatement) GrCatchClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrCatchClause) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Aggregations

GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)113 PsiElement (com.intellij.psi.PsiElement)36 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)26 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)22 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)21 GrIfStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement)17 TextRange (com.intellij.openapi.util.TextRange)14 Nullable (org.jetbrains.annotations.Nullable)14 GrBlockStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement)13 NotNull (org.jetbrains.annotations.NotNull)12 GrReturnStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement)12 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)12 GrStatementOwner (org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner)11 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)10 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)10 GrVariableDeclaration (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration)9 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)9 GrAssignmentExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression)8 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)8 Document (com.intellij.openapi.editor.Document)6