Search in sources :

Example 1 with GrForStatement

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

the class ForToEachIntention method processIntention.

@Override
public void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    final GrForStatement parentStatement = (GrForStatement) element;
    final GrForInClause clause = (GrForInClause) parentStatement.getClause();
    final GrVariable var = clause.getDeclaredVariable();
    final GrStatement body = parentStatement.getBody();
    final String bodyText;
    if (body instanceof GrBlockStatement) {
        final String text = body.getText();
        bodyText = text.substring(1, text.length() - 1);
    } else {
        bodyText = body.getText();
    }
    GrExpression collection = clause.getIteratedExpression();
    assert collection != null;
    @NonNls final String statement = "x.each{" + var.getText() + " -> " + bodyText + " }";
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(parentStatement.getProject());
    final GrMethodCallExpression eachExpression = (GrMethodCallExpression) factory.createTopElementFromText(statement);
    ((GrReferenceExpression) eachExpression.getInvokedExpression()).getQualifierExpression().replaceWithExpression(collection, true);
    parentStatement.replaceWithStatement(eachExpression);
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) NonNls(org.jetbrains.annotations.NonNls) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrForInClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForInClause) GrForStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrForStatement) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrBlockStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 2 with GrForStatement

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

the class GroovySmartEnterProcessor method reformat.

@Override
protected void reformat(PsiElement atCaret) throws IncorrectOperationException {
    PsiElement parent = atCaret.getParent();
    if (parent instanceof GrCodeBlock) {
        final GrCodeBlock block = (GrCodeBlock) parent;
        if (block.getStatements().length > 0 && block.getStatements()[0] == atCaret) {
            atCaret = block;
        }
    } else if (parent instanceof GrForStatement || parent instanceof GrSwitchStatement) {
        atCaret = parent;
    }
    super.reformat(atCaret);
}
Also used : GrForStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrForStatement) GrSwitchStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrSwitchStatement) GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)

Example 3 with GrForStatement

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

the class GrForBodyFixer method apply.

@Override
public void apply(@NotNull Editor editor, @NotNull GroovySmartEnterProcessor processor, @NotNull PsiElement psiElement) {
    GrForStatement forStatement = PsiTreeUtil.getParentOfType(psiElement, GrForStatement.class);
    if (forStatement == null)
        return;
    final Document doc = editor.getDocument();
    PsiElement body = forStatement.getBody();
    if (body instanceof GrBlockStatement)
        return;
    if (body != null && startLine(doc, body) == startLine(doc, forStatement))
        return;
    PsiElement eltToInsertAfter = forStatement.getRParenth();
    String text = "{}";
    if (eltToInsertAfter == null) {
        eltToInsertAfter = forStatement;
        text = "){}";
    }
    doc.insertString(eltToInsertAfter.getTextRange().getEndOffset(), text);
}
Also used : GrForStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrForStatement) Document(com.intellij.openapi.editor.Document) GrBlockStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement) PsiElement(com.intellij.psi.PsiElement)

Example 4 with GrForStatement

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

the class ForSurrounder method doSurroundElements.

@Override
protected GroovyPsiElement doSurroundElements(PsiElement[] elements, PsiElement context) throws IncorrectOperationException {
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(elements[0].getProject());
    GrForStatement whileStatement = (GrForStatement) factory.createStatementFromText("for(a in b){\n}", context);
    addStatements(((GrBlockStatement) whileStatement.getBody()).getBlock(), elements);
    return whileStatement;
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrForStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrForStatement)

Example 5 with GrForStatement

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

the class ForSurrounder method getSurroundSelectionRange.

@Override
protected TextRange getSurroundSelectionRange(GroovyPsiElement element) {
    assert element instanceof GrForStatement;
    GrForClause clause = ((GrForStatement) element).getClause();
    int endOffset = element.getTextRange().getEndOffset();
    if (clause != null) {
        endOffset = clause.getTextRange().getStartOffset();
        clause.getParent().getNode().removeChild(clause.getNode());
    }
    return new TextRange(endOffset, endOffset);
}
Also used : GrForStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrForStatement) GrForClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForClause) TextRange(com.intellij.openapi.util.TextRange)

Aggregations

GrForStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrForStatement)7 GrForClause (org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForClause)3 GrForInClause (org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForInClause)3 PsiElement (com.intellij.psi.PsiElement)2 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)2 GrBlockStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement)2 Document (com.intellij.openapi.editor.Document)1 TextRange (com.intellij.openapi.util.TextRange)1 PsiFile (com.intellij.psi.PsiFile)1 NonNls (org.jetbrains.annotations.NonNls)1 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)1 GrSwitchStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrSwitchStatement)1 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)1 GrCodeBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)1 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)1 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)1