use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement in project intellij-community by JetBrains.
the class ReplaceTernaryWithIfElseIntention method processIntention.
@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
GrConditionalExpression parentTernary = findTernary(element);
GroovyPsiElementFactory groovyPsiElementFactory = GroovyPsiElementFactory.getInstance(project);
GrReturnStatement parentReturn = (GrReturnStatement) parentTernary.getParent();
String condition = parentTernary.getCondition().getText();
GrExpression thenBranch = parentTernary.getThenBranch();
String thenText = thenBranch != null ? thenBranch.getText() : "";
GrExpression elseBranch = parentTernary.getElseBranch();
String elseText = elseBranch != null ? elseBranch.getText() : "";
String text = "if (" + condition + ") { \nreturn " + thenText + "\n} else {\n return " + elseText + "\n}";
GrIfStatement ifStatement = (GrIfStatement) groovyPsiElementFactory.createStatementFromText(text);
ifStatement = parentReturn.replaceWithStatement(ifStatement);
editor.getCaretModel().moveToOffset(ifStatement.getRParenth().getTextRange().getEndOffset());
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement in project intellij-community by JetBrains.
the class GroovyElseUnwrapperBase method collectElementsToIgnore.
@Override
public void collectElementsToIgnore(PsiElement element, Set<PsiElement> result) {
PsiElement parent = element.getParent();
while (parent instanceof GrIfStatement) {
result.add(parent);
parent = parent.getParent();
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement in project intellij-community by JetBrains.
the class GroovyIfUnwrapper method doUnwrap.
@Override
protected void doUnwrap(PsiElement element, Context context) throws IncorrectOperationException {
GrStatement then = ((GrIfStatement) element).getThenBranch();
context.extractFromBlockOrSingleStatement(then, element);
context.delete(element);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement in project intellij-community by JetBrains.
the class IfElseExprSurrounder method surroundExpression.
@Override
protected TextRange surroundExpression(@NotNull GrExpression expression, PsiElement context) {
GrIfStatement ifStatement = (GrIfStatement) GroovyPsiElementFactory.getInstance(expression.getProject()).createStatementFromText("if(a){4\n} else{\n}", context);
replaceToOldExpression(ifStatement.getCondition(), expression);
ifStatement = expression.replaceWithStatement(ifStatement);
GrStatement psiElement = ifStatement.getThenBranch();
assert psiElement instanceof GrBlockStatement;
GrStatement[] statements = ((GrBlockStatement) psiElement).getBlock().getStatements();
assert statements.length > 0;
GrStatement statement = statements[0];
int endOffset = statement.getTextRange().getStartOffset();
statement.getNode().getTreeParent().removeChild(statement.getNode());
return new TextRange(endOffset, endOffset);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement in project intellij-community by JetBrains.
the class GrMissingIfStatement method apply.
@Override
public void apply(@NotNull Editor editor, @NotNull GroovySmartEnterProcessor processor, @NotNull PsiElement psiElement) {
if (!(psiElement instanceof GrIfStatement))
return;
GrIfStatement ifStatement = (GrIfStatement) psiElement;
final Document document = editor.getDocument();
final GrStatement elseBranch = ifStatement.getElseBranch();
final PsiElement elseElement = ifStatement.getElseKeyword();
if (elseElement != null && (elseBranch == null || !(elseBranch instanceof GrBlockStatement) && GrForBodyFixer.startLine(editor.getDocument(), elseBranch) > GrForBodyFixer.startLine(editor.getDocument(), elseElement))) {
document.insertString(elseElement.getTextRange().getEndOffset(), "{}");
}
GrStatement thenBranch = ifStatement.getThenBranch();
if (thenBranch instanceof GrBlockStatement)
return;
boolean transformingOneLiner = false;
if (thenBranch != null && GrForBodyFixer.startLine(editor.getDocument(), thenBranch) == GrForBodyFixer.startLine(editor.getDocument(), ifStatement)) {
if (ifStatement.getCondition() != null) {
return;
}
transformingOneLiner = true;
}
final PsiElement rParenth = ifStatement.getRParenth();
if (rParenth == null)
return;
if (elseBranch == null && !transformingOneLiner || thenBranch == null) {
document.insertString(rParenth.getTextRange().getEndOffset(), "{}");
} else {
document.insertString(rParenth.getTextRange().getEndOffset(), "{");
document.insertString(thenBranch.getTextRange().getEndOffset() + 1, "}");
}
}
Aggregations