Search in sources :

Example 1 with GrTryCatchStatement

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

the class GroovyTryUnwrapper method doUnwrap.

@Override
protected void doUnwrap(final PsiElement element, final Context context) throws IncorrectOperationException {
    GrTryCatchStatement trySt = (GrTryCatchStatement) element;
    context.extractFromCodeBlock(trySt.getTryBlock(), trySt);
    GrFinallyClause finallyClause = trySt.getFinallyClause();
    if (finallyClause != null) {
        context.extractFromCodeBlock(finallyClause.getBody(), trySt);
    }
    context.delete(trySt);
}
Also used : GrTryCatchStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrTryCatchStatement) GrFinallyClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrFinallyClause)

Example 2 with GrTryCatchStatement

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

the class TryCatchFinallySurrounder method doSurroundElements.

@Override
protected GroovyPsiElement doSurroundElements(PsiElement[] elements, PsiElement context) throws IncorrectOperationException {
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(elements[0].getProject());
    GrTryCatchStatement tryStatement = (GrTryCatchStatement) factory.createStatementFromText("try {\n} catch(exception e){\n} finally{\n}", context);
    addStatements(tryStatement.getTryBlock(), elements);
    return tryStatement;
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrTryCatchStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrTryCatchStatement)

Example 3 with GrTryCatchStatement

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrTryCatchStatement 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)

Example 4 with GrTryCatchStatement

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

the class TryFinallySurrounder method doSurroundElements.

@Override
protected GroovyPsiElement doSurroundElements(PsiElement[] elements, PsiElement context) throws IncorrectOperationException {
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(elements[0].getProject());
    GrTryCatchStatement tryStatement = (GrTryCatchStatement) factory.createStatementFromText("try {\n} finally{\n}", context);
    addStatements(tryStatement.getTryBlock(), elements);
    return tryStatement;
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrTryCatchStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrTryCatchStatement)

Aggregations

GrTryCatchStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrTryCatchStatement)4 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)2 TextRange (com.intellij.openapi.util.TextRange)1 GrCatchClause (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrCatchClause)1 GrFinallyClause (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrFinallyClause)1 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)1 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)1 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)1