use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement 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.GrBlockStatement 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);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement 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;
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement 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 GrOpenBlock codeBlock = block.getBlock();
final GrStatement[] statements = codeBlock.getStatements();
if (statements.length == 1) {
return statements[0];
} else {
return block;
}
} else {
return branch;
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement in project intellij-community by JetBrains.
the class GrRedundantElseIntention method processIntention.
@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
final PsiElement parent = element.getParent();
if (!(parent instanceof GrIfStatement))
return;
final GrIfStatement ifStatement = GroovyRefactoringUtil.addBlockIntoParent((GrIfStatement) parent);
assert ifStatement.getParent() instanceof GrStatementOwner;
final PsiElement statementOwner = ifStatement.getParent();
final GrStatement branch = ifStatement.getElseBranch();
if (branch == null)
return;
final PsiElement pos;
if (branch instanceof GrBlockStatement) {
final GrOpenBlock block = ((GrBlockStatement) branch).getBlock();
final PsiElement first = inferFirst(block.getLBrace());
final PsiElement last = inferLast(block.getRBrace());
pos = statementOwner.addRangeAfter(first, last, ifStatement);
} else {
pos = statementOwner.addAfter(branch, ifStatement);
}
branch.delete();
editor.getCaretModel().moveToOffset(pos.getTextRange().getStartOffset());
}
Aggregations