Search in sources :

Example 36 with GrStatement

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

the class WhileExprSurrounder method surroundExpression.

@Override
protected TextRange surroundExpression(@NotNull GrExpression expression, PsiElement context) {
    GrWhileStatement whileStatement = (GrWhileStatement) GroovyPsiElementFactory.getInstance(expression.getProject()).createStatementFromText("while(a){4\n}", context);
    replaceToOldExpression((GrExpression) whileStatement.getCondition(), expression);
    whileStatement = expression.replaceWithStatement(whileStatement);
    GrStatement body = whileStatement.getBody();
    assert body instanceof GrBlockStatement;
    GrStatement[] statements = ((GrBlockStatement) body).getBlock().getStatements();
    assert statements.length > 0;
    GrStatement statement = statements[0];
    int offset = statement.getTextRange().getStartOffset();
    statement.getNode().getTreeParent().removeChild(statement.getNode());
    return new TextRange(offset, offset);
}
Also used : GrWhileStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrWhileStatement) TextRange(com.intellij.openapi.util.TextRange) GrBlockStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 37 with GrStatement

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

the class Dependency method getGroovyElements.

@Override
@NotNull
public List<PsiElement> getGroovyElements(@NotNull GroovyPsiElementFactory factory) {
    String extraGroovyCode;
    switch(type) {
        case EXTERNAL:
            if (extraClosure != null) {
                extraGroovyCode = "(" + escapeAndQuote(data) + ")";
            } else {
                extraGroovyCode = " " + escapeAndQuote(data);
            }
            break;
        case MODULE:
            if (data instanceof Map) {
                extraGroovyCode = " project(" + GradleGroovyFile.convertMapToGroovySource((Map<String, Object>) data) + ")";
            } else {
                extraGroovyCode = " project(" + escapeAndQuote(data) + ")";
            }
            if (extraClosure != null) {
                // If there's a closure with exclusion rules, then we need extra parentheses:
                // compile project(':foo') { ... } is not valid Groovy syntax
                // compile(project(':foo')) { ... } is correct
                extraGroovyCode = "(" + extraGroovyCode.substring(1) + ")";
            }
            break;
        case FILES:
            extraGroovyCode = " files(" + escapeAndQuote(data) + ")";
            break;
        case FILETREE:
            extraGroovyCode = " fileTree(" + GradleGroovyFile.convertMapToGroovySource((Map<String, Object>) data) + ")";
            break;
        default:
            extraGroovyCode = "";
            break;
    }
    GrStatement statement = factory.createStatementFromText(scope.getGroovyMethodCall() + extraGroovyCode);
    if (statement instanceof GrMethodCall && extraClosure != null) {
        statement.add(factory.createClosureFromText(extraClosure));
    }
    return ImmutableList.of((PsiElement) statement);
}
Also used : GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) BuildFileKey.escapeLiteralString(com.android.tools.idea.gradle.parser.BuildFileKey.escapeLiteralString) GrString(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString) Map(java.util.Map) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) NotNull(org.jetbrains.annotations.NotNull)

Example 38 with GrStatement

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

Example 39 with GrStatement

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

the class GrUnnecessarySemicolonInspection method isSemicolonNecessary.

private static boolean isSemicolonNecessary(@NotNull PsiElement semicolon) {
    if (semicolon.getParent() instanceof GrTraditionalForClause)
        return true;
    PsiElement prevSibling = PsiUtil.skipSet(semicolon, false, BACKWARD_SET);
    PsiElement nextSibling = PsiUtil.skipSet(semicolon, true, FORWARD_SET);
    return prevSibling instanceof GrStatement && (nextSibling instanceof GrStatement || nextSibling != null && nextSibling.getNextSibling() instanceof GrClosableBlock);
}
Also used : GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) PsiElement(com.intellij.psi.PsiElement) GrTraditionalForClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrTraditionalForClause) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 40 with GrStatement

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

the class ConditionalUtils method stripBraces.

public static GrStatement stripBraces(GrStatement branch) {
    if (branch instanceof GrBlockStatement) {
        final GrBlockStatement block = (GrBlockStatement) branch;
        final GrStatement[] statements = block.getBlock().getStatements();
        if (statements.length == 1) {
            return statements[0];
        } else {
            return block;
        }
    } else {
        return branch;
    }
}
Also used : GrBlockStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement) 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