Search in sources :

Example 16 with GrOpenBlock

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

the class CodeBlockGenerator method visitCatchClause.

@Override
public void visitCatchClause(@NotNull GrCatchClause catchClause) {
    final GrParameter parameter = catchClause.getParameter();
    builder.append("catch (");
    writeVariableWithoutSemicolonAndInitializer(builder, parameter, context);
    builder.append(") ");
    final GrOpenBlock body = catchClause.getBody();
    if (body != null) {
        body.accept(this);
    }
}
Also used : GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)

Example 17 with GrOpenBlock

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

the class CodeBlockGenerator method visitTryStatement.

@Override
public void visitTryStatement(@NotNull GrTryCatchStatement tryCatchStatement) {
    final GrOpenBlock tryBlock = tryCatchStatement.getTryBlock();
    final GrCatchClause[] catchClauses = tryCatchStatement.getCatchClauses();
    final GrFinallyClause finallyClause = tryCatchStatement.getFinallyClause();
    builder.append("try");
    tryBlock.accept(this);
    for (GrCatchClause catchClause : catchClauses) {
        catchClause.accept(this);
    }
    if (finallyClause != null) {
        finallyClause.accept(this);
    }
}
Also used : GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)

Example 18 with GrOpenBlock

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

the class CodeBlockGenerator method generateMethodBody.

public void generateMethodBody(GrMethod method) {
    final GrOpenBlock block = method.getBlock();
    boolean shouldInsertReturnNull;
    myExitPoints.clear();
    PsiType returnType = context.typeProvider.getReturnType(method);
    if (GroovyObjectTransformationSupport.isGroovyObjectSupportMethod(method)) {
        shouldInsertReturnNull = !(returnType instanceof PsiPrimitiveType);
    } else if (!method.isConstructor() && !PsiType.VOID.equals(returnType)) {
        myExitPoints.addAll(ControlFlowUtils.collectReturns(block));
        shouldInsertReturnNull = block != null && !(returnType instanceof PsiPrimitiveType) && MissingReturnInspection.methodMissesSomeReturns(block, MissingReturnInspection.ReturnStatus.getReturnStatus(method));
    } else {
        shouldInsertReturnNull = false;
    }
    generateCodeBlock(method.getParameters(), block, shouldInsertReturnNull);
}
Also used : GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)

Example 19 with GrOpenBlock

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

the class ReplaceAbstractClassInstanceByMapIntention method processIntention.

@Override
protected void processIntention(@NotNull PsiElement psiElement, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    PsiDocumentManager.getInstance(project).commitAllDocuments();
    GrCodeReferenceElement ref = (GrCodeReferenceElement) psiElement;
    final GrAnonymousClassDefinition anonymous = (GrAnonymousClassDefinition) ref.getParent();
    final GrNewExpression newExpr = (GrNewExpression) anonymous.getParent();
    final PsiElement resolved = ref.resolve();
    // && ((PsiClass)resolved).isInterface();
    assert resolved instanceof PsiClass;
    GrTypeDefinitionBody body = anonymous.getBody();
    assert body != null;
    List<Pair<PsiMethod, GrOpenBlock>> methods = new ArrayList<>();
    for (GrMethod method : body.getMethods()) {
        methods.add(new Pair<>(method, method.getBlock()));
    }
    final PsiClass iface = (PsiClass) resolved;
    final Collection<CandidateInfo> collection = OverrideImplementExploreUtil.getMethodsToOverrideImplement(anonymous, true);
    for (CandidateInfo info : collection) {
        methods.add(new Pair<>((PsiMethod) info.getElement(), null));
    }
    StringBuilder buffer = new StringBuilder();
    if (methods.size() == 1) {
        final Pair<PsiMethod, GrOpenBlock> pair = methods.get(0);
        appendClosureTextByMethod(pair.getFirst(), buffer, pair.getSecond(), newExpr);
        if (!GroovyConfigUtils.getInstance().isVersionAtLeast(psiElement, GroovyConfigUtils.GROOVY2_2)) {
            buffer.append(" as ").append(iface.getQualifiedName());
        }
    } else {
        buffer.append("[");
        buffer.append("\n");
        for (Pair<PsiMethod, GrOpenBlock> pair : methods) {
            final PsiMethod method = pair.getFirst();
            final GrOpenBlock block = pair.getSecond();
            buffer.append(method.getName()).append(": ");
            appendClosureTextByMethod(method, buffer, block, newExpr);
            buffer.append(",\n");
        }
        if (!methods.isEmpty()) {
            buffer.delete(buffer.length() - 2, buffer.length());
            buffer.append('\n');
        }
        buffer.append("]");
        buffer.append(" as ").append(iface.getQualifiedName());
    }
    createAndAdjustNewExpression(project, newExpr, buffer);
}
Also used : CandidateInfo(com.intellij.psi.infos.CandidateInfo) GrAnonymousClassDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrNewExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression) GrTypeDefinitionBody(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinitionBody) GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) Pair(com.intellij.openapi.util.Pair)

Example 20 with GrOpenBlock

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

the class GroovyFix method replaceStatement.

/**
   * unwraps surrounding blocks from newStatement.
   */
protected static void replaceStatement(GrStatement oldStatement, GrStatement newStatement) throws IncorrectOperationException {
    if (newStatement instanceof GrBlockStatement) {
        GrBlockStatement blockStatement = (GrBlockStatement) newStatement;
        final GrOpenBlock openBlock = blockStatement.getBlock();
        final GrStatement[] statements = openBlock.getStatements();
        if (statements.length == 0) {
            oldStatement.removeStatement();
        } else {
            final PsiElement parent = oldStatement.getParent();
            if (parent instanceof GrStatementOwner) {
                GrStatementOwner statementOwner = (GrStatementOwner) parent;
                for (GrStatement statement : statements) {
                    statementOwner.addStatementBefore(statement, oldStatement);
                }
                oldStatement.removeStatement();
            } else if (parent instanceof GrControlStatement) {
                oldStatement.replace(newStatement);
            }
        }
    } else {
        oldStatement.replaceWithStatement(newStatement);
    }
}
Also used : GrStatementOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) GrBlockStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement) GrControlStatement(org.jetbrains.plugins.groovy.lang.psi.api.formatter.GrControlStatement) PsiElement(com.intellij.psi.PsiElement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Aggregations

GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)68 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)24 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)21 PsiElement (com.intellij.psi.PsiElement)13 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)10 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)10 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)9 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)9 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)8 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)8 GrReturnStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement)7 Nullable (org.jetbrains.annotations.Nullable)6 NotNull (org.jetbrains.annotations.NotNull)5 GrBlockStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement)5 Instruction (org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction)5 ReadWriteVariableInstruction (org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction)5 TextRange (com.intellij.openapi.util.TextRange)4 GroovyRecursiveElementVisitor (org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor)4 GrCodeBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)4 UsageInfo (com.intellij.usageView.UsageInfo)3