Search in sources :

Example 1 with GrWhileStatement

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

the class GrWhileBodyFixer method apply.

@Override
public void apply(@NotNull Editor editor, @NotNull GroovySmartEnterProcessor processor, @NotNull PsiElement psiElement) {
    if (!(psiElement instanceof GrWhileStatement))
        return;
    GrWhileStatement whileStatement = (GrWhileStatement) psiElement;
    final Document doc = editor.getDocument();
    PsiElement body = whileStatement.getBody();
    if (body instanceof GrBlockStatement)
        return;
    if (body != null && GrForBodyFixer.startLine(editor.getDocument(), body) == GrForBodyFixer.startLine(editor.getDocument(), whileStatement) && whileStatement.getCondition() != null)
        return;
    final PsiElement rParenth = whileStatement.getRParenth();
    assert rParenth != null;
    doc.insertString(rParenth.getTextRange().getEndOffset(), "{}");
}
Also used : GrWhileStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrWhileStatement) Document(com.intellij.openapi.editor.Document) GrBlockStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement) PsiElement(com.intellij.psi.PsiElement)

Example 2 with GrWhileStatement

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

the class GroovyWhileUnwrapper method doUnwrap.

@Override
protected void doUnwrap(PsiElement element, Context context) throws IncorrectOperationException {
    GrStatement body = ((GrWhileStatement) element).getBody();
    context.extractFromBlockOrSingleStatement(body, element);
    context.delete(element);
}
Also used : GrWhileStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrWhileStatement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 3 with GrWhileStatement

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrWhileStatement 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 4 with GrWhileStatement

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

the class WhileSurrounder method doSurroundElements.

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

Example 5 with GrWhileStatement

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

the class GrWhileConditionFixer method apply.

@Override
public void apply(@NotNull Editor editor, @NotNull GroovySmartEnterProcessor processor, @NotNull PsiElement psiElement) {
    if (psiElement instanceof GrWhileStatement) {
        final Document doc = editor.getDocument();
        final GrWhileStatement whileStatement = (GrWhileStatement) psiElement;
        final PsiElement rParenth = whileStatement.getRParenth();
        final PsiElement lParenth = whileStatement.getLParenth();
        final GrCondition condition = whileStatement.getCondition();
        if (condition == null) {
            if (lParenth == null || rParenth == null) {
                int stopOffset = doc.getLineEndOffset(doc.getLineNumber(whileStatement.getTextRange().getStartOffset()));
                final GrStatement block = whileStatement.getBody();
                if (block != null) {
                    stopOffset = Math.min(stopOffset, block.getTextRange().getStartOffset());
                }
                stopOffset = Math.min(stopOffset, whileStatement.getTextRange().getEndOffset());
                doc.replaceString(whileStatement.getTextRange().getStartOffset(), stopOffset, "while ()");
                processor.registerUnresolvedError(whileStatement.getTextRange().getStartOffset() + "while (".length());
            } else {
                processor.registerUnresolvedError(lParenth.getTextRange().getEndOffset());
            }
        } else if (rParenth == null) {
            doc.insertString(condition.getTextRange().getEndOffset(), ")");
        }
    }
}
Also used : GrWhileStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrWhileStatement) Document(com.intellij.openapi.editor.Document) GrCondition(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrCondition) PsiElement(com.intellij.psi.PsiElement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Aggregations

GrWhileStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrWhileStatement)6 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)3 Document (com.intellij.openapi.editor.Document)2 TextRange (com.intellij.openapi.util.TextRange)2 PsiElement (com.intellij.psi.PsiElement)2 GrCondition (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrCondition)2 GrBlockStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement)2 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)1