use of org.jetbrains.plugins.groovy.lang.psi.api.formatter.GrControlStatement in project intellij-community by JetBrains.
the class ControlFlowBuilderUtil method isCertainlyReturnStatement.
/**
* check whether statement is return (the statement which provides return value) statement of method or closure.
*
* @param st
* @return
*/
public static boolean isCertainlyReturnStatement(GrStatement st) {
final PsiElement parent = st.getParent();
if (parent instanceof GrOpenBlock) {
if (st != ArrayUtil.getLastElement(((GrOpenBlock) parent).getStatements()))
return false;
PsiElement pparent = parent.getParent();
if (pparent instanceof GrMethod) {
return true;
}
if (pparent instanceof GrBlockStatement || pparent instanceof GrCatchClause || pparent instanceof GrLabeledStatement) {
pparent = pparent.getParent();
}
if (pparent instanceof GrIfStatement || pparent instanceof GrControlStatement || pparent instanceof GrTryCatchStatement) {
return isCertainlyReturnStatement((GrStatement) pparent);
}
} else if (parent instanceof GrClosableBlock) {
return st == ArrayUtil.getLastElement(((GrClosableBlock) parent).getStatements());
} else if (parent instanceof GroovyFileBase) {
return st == ArrayUtil.getLastElement(((GroovyFileBase) parent).getStatements());
} else if (parent instanceof GrForStatement || parent instanceof GrIfStatement && st != ((GrIfStatement) parent).getCondition() || parent instanceof GrSynchronizedStatement && st != ((GrSynchronizedStatement) parent).getMonitor() || parent instanceof GrWhileStatement && st != ((GrWhileStatement) parent).getCondition() || parent instanceof GrConditionalExpression && st != ((GrConditionalExpression) parent).getCondition() || parent instanceof GrElvisExpression) {
return isCertainlyReturnStatement((GrStatement) parent);
} else if (parent instanceof GrCaseSection) {
final GrStatement[] statements = ((GrCaseSection) parent).getStatements();
final GrStatement last = ArrayUtil.getLastElement(statements);
final GrSwitchStatement switchStatement = (GrSwitchStatement) parent.getParent();
if (last instanceof GrBreakStatement && statements.length > 1 && statements[statements.length - 2] == st) {
return isCertainlyReturnStatement(switchStatement);
} else if (st == last) {
if (st instanceof GrBreakStatement || isLastStatementInCaseSection((GrCaseSection) parent, switchStatement)) {
return isCertainlyReturnStatement(switchStatement);
}
}
}
return false;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.formatter.GrControlStatement in project intellij-community by JetBrains.
the class GenerationUtil method writeStatement.
static void writeStatement(@NotNull StringBuilder codeBlockBuilder, @NotNull StringBuilder statementBuilder, @Nullable GrStatement statement, @Nullable ExpressionContext context) {
final PsiElement parent = statement == null ? null : statement.getParent();
final boolean addParentheses;
if (statement == null) {
addParentheses = context != null && context.shouldInsertCurlyBrackets();
} else {
addParentheses = context != null && (context.shouldInsertCurlyBrackets() || !context.myStatements.isEmpty()) && parent instanceof GrControlStatement;
}
if (addParentheses) {
codeBlockBuilder.append("{\n");
}
if (context != null) {
insertStatementFromContextBefore(codeBlockBuilder, context);
}
codeBlockBuilder.append(statementBuilder);
if (addParentheses) {
codeBlockBuilder.append("}\n");
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.formatter.GrControlStatement in project intellij-community by JetBrains.
the class GenerationUtil method writeSimpleVarDeclaration.
public static void writeSimpleVarDeclaration(GrVariableDeclaration variableDeclaration, StringBuilder builder, ExpressionContext expressionContext) {
GrVariable[] variables = variableDeclaration.getVariables();
//if (types.size() > 1) {
if (variables.length > 1 && variableDeclaration.getParent() instanceof GrControlStatement) {
expressionContext.setInsertCurlyBrackets();
}
for (GrVariable variable : variables) {
writeVariableSeparately(variable, builder, expressionContext);
builder.append(";\n");
}
builder.delete(builder.length() - 1, builder.length());
//builder.removeFromTheEnd(1);
/*return;
}
ModifierListGenerator.writeModifiers(builder, variableDeclaration.getModifierList());
PsiType type = getVarType(variables[0]);
writeType(builder, type, variableDeclaration);
builder.append(" ");
for (GrVariable variable : variables) {
writeVariableWithoutType(builder, expressionContext, variable);
builder.append(", ");
}
if (variables.length > 0) {
builder.delete(builder.length() - 2, builder.length());
}
builder.append(";");*/
}
Aggregations