use of org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement in project intellij-community by JetBrains.
the class EachToForIntention method updateReturnStatements.
private static GrForStatement updateReturnStatements(GrForStatement forStatement) {
GrStatement body = forStatement.getBody();
assert body != null;
final Set<String> usedLabels = ContainerUtil.newHashSet();
final Ref<Boolean> needLabel = Ref.create(false);
body.accept(new GroovyRecursiveElementVisitor() {
private int myLoops = 0;
@Override
public void visitReturnStatement(@NotNull GrReturnStatement returnStatement) {
if (returnStatement.getReturnValue() != null)
return;
if (myLoops > 0)
needLabel.set(true);
}
@Override
public void visitLabeledStatement(@NotNull GrLabeledStatement labeledStatement) {
super.visitLabeledStatement(labeledStatement);
usedLabels.add(labeledStatement.getName());
}
@Override
public void visitForStatement(@NotNull GrForStatement forStatement) {
myLoops++;
super.visitForStatement(forStatement);
myLoops--;
}
@Override
public void visitWhileStatement(@NotNull GrWhileStatement whileStatement) {
myLoops++;
super.visitWhileStatement(whileStatement);
myLoops--;
}
@Override
public void visitClosure(@NotNull GrClosableBlock closure) {
//don't go into closures
}
@Override
public void visitAnonymousClassDefinition(@NotNull GrAnonymousClassDefinition anonymousClassDefinition) {
//don't go into anonymous
}
});
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(forStatement.getProject());
final String continueText;
if (needLabel.get()) {
int i = 0;
String label = OUTER;
while (usedLabels.contains(label)) {
label = OUTER + i;
i++;
}
continueText = "continue " + label;
GrLabeledStatement labeled = (GrLabeledStatement) factory.createStatementFromText(label + ": while (true){}");
labeled.getStatement().replaceWithStatement(forStatement);
labeled = forStatement.replaceWithStatement(labeled);
forStatement = (GrForStatement) labeled.getStatement();
body = forStatement.getBody();
assert body != null;
} else {
continueText = "continue";
}
final GrStatement continueStatement = factory.createStatementFromText(continueText);
body.accept(new GroovyRecursiveElementVisitor() {
@Override
public void visitReturnStatement(@NotNull GrReturnStatement returnStatement) {
if (returnStatement.getReturnValue() == null) {
returnStatement.replaceWithStatement(continueStatement);
}
}
@Override
public void visitClosure(@NotNull GrClosableBlock closure) {
//don't go into closures
}
@Override
public void visitAnonymousClassDefinition(@NotNull GrAnonymousClassDefinition anonymousClassDefinition) {
//don't go into anonymous
}
});
return forStatement;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement in project intellij-community by JetBrains.
the class ExpandBooleanPredicate method isBooleanReturn.
public static boolean isBooleanReturn(GrStatement statement) {
if (!(statement instanceof GrReturnStatement)) {
return false;
}
final GrReturnStatement returnStatement = (GrReturnStatement) statement;
final GrExpression returnValue = returnStatement.getReturnValue();
if (returnValue == null) {
return false;
}
if (returnValue instanceof GrLiteral) {
return false;
}
final PsiType returnType = returnValue.getType();
if (returnType == null) {
return false;
}
return returnType.equals(PsiType.BOOLEAN) || returnType.equalsToText("java.lang.Boolean");
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement 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.branch.GrReturnStatement 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.branch.GrReturnStatement in project intellij-community by JetBrains.
the class MethodTypeInferencer method compute.
@Override
@Nullable
public PsiType compute() {
List<GrStatement> returns = ControlFlowUtils.collectReturns(myBlock);
if (returns.isEmpty())
return PsiType.VOID;
PsiType result = null;
PsiManager manager = myBlock.getManager();
for (GrStatement returnStatement : returns) {
GrExpression value = null;
if (returnStatement instanceof GrReturnStatement) {
value = ((GrReturnStatement) returnStatement).getReturnValue();
} else if (returnStatement instanceof GrExpression) {
value = (GrExpression) returnStatement;
}
if (value != null) {
result = TypesUtil.getLeastUpperBoundNullable(result, value.getType(), manager);
}
}
return result;
}
Aggregations