this.scribe.space();
}
ifStatement.condition.traverse(this, scope);
this.scribe.printNextToken(TerminalTokens.TokenNameRPAREN, this.preferences.insert_space_before_closing_paren_in_if);
final Statement thenStatement = ifStatement.thenStatement;
final Statement elseStatement = ifStatement.elseStatement;
boolean thenStatementIsBlock = false;
if (thenStatement != null) {
if (thenStatement instanceof Block) {
thenStatementIsBlock = true;
if (isGuardClause((Block)thenStatement) && elseStatement == null && this.preferences.keep_guardian_clause_on_one_line) {
/*
* Need a specific formatting for guard clauses
* guard clauses are block with a single return or throw
* statement
*/
formatGuardClauseBlock((Block) thenStatement, scope);
} else {
formatLeftCurlyBrace(line, this.preferences.brace_position_for_block);
thenStatement.traverse(this, scope);
if (elseStatement != null && (this.preferences.insert_new_line_before_else_in_if_statement)) {
this.scribe.printNewLine();
}
}
} else if (elseStatement == null && this.preferences.keep_simple_if_on_one_line) {
Alignment compactIfAlignment = this.scribe.createAlignment(
"compactIf", //$NON-NLS-1$
this.preferences.alignment_for_compact_if,
Alignment.R_OUTERMOST,
1,
this.scribe.scanner.currentPosition,
1,
false);
this.scribe.enterAlignment(compactIfAlignment);
boolean ok = false;
do {
try {
this.scribe.alignFragment(compactIfAlignment, 0);
this.scribe.space();
thenStatement.traverse(this, scope);
if (thenStatement instanceof Expression) {
this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
this.scribe.printTrailingComment();
}
ok = true;
} catch (AlignmentException e) {
this.scribe.redoAlignment(e);
}
} while (!ok);
this.scribe.exitAlignment(compactIfAlignment, true);
} else if (this.preferences.keep_then_statement_on_same_line) {
this.scribe.space();
thenStatement.traverse(this, scope);
if (thenStatement instanceof Expression) {
this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
this.scribe.printTrailingComment();
}
if (elseStatement != null) {
this.scribe.printNewLine();
}
} else {
this.scribe.printTrailingComment();
this.scribe.printNewLine();
this.scribe.indent();
thenStatement.traverse(this, scope);
if (thenStatement instanceof Expression) {
this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
this.scribe.printTrailingComment();
}
if (elseStatement != null) {
this.scribe.printNewLine();
}
this.scribe.unIndent();
}
}
if (elseStatement != null) {
if (thenStatementIsBlock) {
this.scribe.printNextToken(TerminalTokens.TokenNameelse, this.preferences.insert_space_after_closing_brace_in_block);
} else {
this.scribe.printNextToken(TerminalTokens.TokenNameelse, true);
}
if (elseStatement instanceof Block) {
elseStatement.traverse(this, scope);
} else if (elseStatement instanceof IfStatement) {
if (!this.preferences.compact_else_if) {
this.scribe.printNewLine();
this.scribe.indent();
}
this.scribe.space();
elseStatement.traverse(this, scope);
if (!this.preferences.compact_else_if) {
this.scribe.unIndent();
}
} else if (this.preferences.keep_else_statement_on_same_line) {
this.scribe.space();
elseStatement.traverse(this, scope);
if (elseStatement instanceof Expression) {
this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
this.scribe.printTrailingComment();
}
} else {
this.scribe.printNewLine();
this.scribe.indent();
elseStatement.traverse(this, scope);
if (elseStatement instanceof Expression) {
this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
this.scribe.printTrailingComment();
}
this.scribe.unIndent();