Search in sources :

Example 21 with GrCodeBlock

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

the class InvertIfIntention method generateElseBranchTextAndRemoveTailStatements.

private static void generateElseBranchTextAndRemoveTailStatements(@NotNull GrIfStatement ifStatement, @NotNull GrIfStatement newIf) {
    final GrStatement thenBranch = newIf.getThenBranch();
    assert thenBranch != null;
    GrStatement elseBranch = ifStatement.getElseBranch();
    if (elseBranch != null) {
        thenBranch.replaceWithStatement(elseBranch);
        return;
    }
    PsiElement parent = ifStatement.getParent();
    if (!(parent instanceof GrStatementOwner))
        return;
    if (!isTailAfterIf(ifStatement, ((GrStatementOwner) parent)))
        return;
    final PsiElement start = ifStatement.getNextSibling();
    PsiElement end = parent instanceof GrCodeBlock ? ((GrCodeBlock) parent).getRBrace().getPrevSibling() : parent.getLastChild();
    final GrOpenBlock block = ((GrBlockStatement) thenBranch).getBlock();
    block.addRangeAfter(start, end, block.getLBrace());
    parent.deleteChildRange(start, end);
}
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) PsiElement(com.intellij.psi.PsiElement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)

Example 22 with GrCodeBlock

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

the class GroovyPropertyUtils method generateGetterPrototype.

public static GrMethod generateGetterPrototype(PsiField field) {
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(field.getProject());
    String name = field.getName();
    String getName = getGetterNameNonBoolean(field.getName());
    try {
        PsiType type = field instanceof GrField ? ((GrField) field).getDeclaredType() : field.getType();
        GrMethod getter = factory.createMethod(getName, type);
        if (field.hasModifierProperty(PsiModifier.STATIC)) {
            PsiUtil.setModifierProperty(getter, PsiModifier.STATIC, true);
        }
        annotateWithNullableStuff(field, getter);
        GrCodeBlock body = factory.createMethodBodyFromText("\nreturn " + name + "\n");
        getter.getBlock().replace(body);
        return getter;
    } catch (IncorrectOperationException e) {
        LOG.error(e);
        return null;
    }
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) IncorrectOperationException(com.intellij.util.IncorrectOperationException) GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)

Aggregations

GrCodeBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)22 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)10 PsiElement (com.intellij.psi.PsiElement)6 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)5 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)5 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)4 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)4 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)3 IncorrectOperationException (com.intellij.util.IncorrectOperationException)3 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)3 GrCaseSection (org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection)3 ASTNode (com.intellij.lang.ASTNode)2 ArrayList (java.util.ArrayList)2 GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)2 GrArgumentLabel (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentLabel)2 GrString (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString)2 GrTypeDefinitionBody (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinitionBody)2 GrStatementOwner (org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner)2 LineRange (com.intellij.codeInsight.editorActions.moveUpDown.LineRange)1 FileTemplate (com.intellij.ide.fileTemplates.FileTemplate)1