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(), "{}");
}
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);
}
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);
}
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;
}
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(), ")");
}
}
}
Aggregations