use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrConditionalExpression in project intellij-community by JetBrains.
the class ReplaceIfWithTernaryIntention method processIntention.
@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
final GrIfStatement ifStatement = (GrIfStatement) element.getParent();
final PsiElement thenBranch = skipBlock(ifStatement.getThenBranch());
final PsiElement elseBranch = skipBlock(ifStatement.getElseBranch());
if (thenBranch instanceof GrAssignmentExpression && elseBranch instanceof GrAssignmentExpression) {
final GrAssignmentExpression assignment = (GrAssignmentExpression) GroovyPsiElementFactory.getInstance(project).createStatementFromText("a = b ? c : d");
assignment.getLValue().replaceWithExpression(((GrAssignmentExpression) thenBranch).getLValue(), true);
final GrConditionalExpression conditional = (GrConditionalExpression) assignment.getRValue();
replaceConditional(conditional, ifStatement.getCondition(), ((GrAssignmentExpression) thenBranch).getRValue(), ((GrAssignmentExpression) elseBranch).getRValue());
ifStatement.replaceWithStatement(assignment);
}
if (thenBranch instanceof GrReturnStatement && elseBranch instanceof GrReturnStatement) {
final GrReturnStatement returnSt = (GrReturnStatement) GroovyPsiElementFactory.getInstance(project).createStatementFromText("return a ? b : c");
final GrConditionalExpression conditional = (GrConditionalExpression) returnSt.getReturnValue();
replaceConditional(conditional, ifStatement.getCondition(), ((GrReturnStatement) thenBranch).getReturnValue(), ((GrReturnStatement) elseBranch).getReturnValue());
ifStatement.replaceWithStatement(returnSt);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrConditionalExpression 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.expressions.GrConditionalExpression in project intellij-community by JetBrains.
the class GroovyConditionalUnwrapper method doUnwrap.
@Override
protected void doUnwrap(PsiElement element, Context context) throws IncorrectOperationException {
GrConditionalExpression cond = (GrConditionalExpression) element.getParent();
PsiElement savedBlock;
if (cond.getElseBranch() == element) {
savedBlock = element;
} else {
savedBlock = cond.getThenBranch();
}
context.extractElement(savedBlock, cond);
context.deleteExactly(cond);
}
Aggregations