1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-08 16:55:38 +02:00

Extracted couple commonly used methods.

This commit is contained in:
Sergey Prigogin 2012-04-14 16:51:01 -07:00
parent 2b8e34b777
commit b6cf5bbc41

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2006, 2011 Wind River Systems, Inc. and others. * Copyright (c) 2006, 2012 Wind River Systems, Inc. and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -467,7 +467,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
continue; continue;
} }
try { try {
int pos = scribe.scanner.getCurrentPosition(); int pos = getCurrentPosition();
IASTFileLocation declarationLocation = declaration.getFileLocation(); IASTFileLocation declarationLocation = declaration.getFileLocation();
int declarationOffset = declarationLocation.getNodeOffset(); int declarationOffset = declarationLocation.getNodeOffset();
for (; m < macroExpansions.length; m++) { for (; m < macroExpansions.length; m++) {
@ -552,7 +552,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
preferences.alignment_for_arguments_in_method_invocation, preferences.alignment_for_arguments_in_method_invocation,
Alignment.R_OUTERMOST, Alignment.R_OUTERMOST,
binding.getParameterList().length, binding.getParameterList().length,
scribe.scanner.getCurrentPosition(), getCurrentPosition(),
continuationIndentation, continuationIndentation,
false); false);
scribe.enterAlignment(listAlignment); scribe.enterAlignment(listAlignment);
@ -641,7 +641,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
try { try {
return formatDeclaration(node); return formatDeclaration(node);
} finally { } finally {
endOfNode(node); finishNode(node);
} }
} }
@ -705,7 +705,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
formatRaw(node); formatRaw(node);
} }
} finally { } finally {
endOfNode(node); finishNode(node);
} }
return PROCESS_SKIP; return PROCESS_SKIP;
} }
@ -738,7 +738,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
formatRaw(node); formatRaw(node);
} }
} finally { } finally {
endOfNode(node); finishNode(node);
} }
return PROCESS_SKIP; return PROCESS_SKIP;
} }
@ -765,7 +765,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
declarator.accept(this); declarator.accept(this);
} }
} finally { } finally {
endOfNode(node); finishNode(node);
} }
return PROCESS_SKIP; return PROCESS_SKIP;
} }
@ -827,7 +827,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
initializer.accept(this); initializer.accept(this);
} }
} finally { } finally {
endOfNode(node); finishNode(node);
} }
return PROCESS_SKIP; return PROCESS_SKIP;
} }
@ -870,7 +870,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
formatRaw(node); formatRaw(node);
} }
} finally { } finally {
endOfNode(node); finishNode(node);
} }
return PROCESS_SKIP; return PROCESS_SKIP;
} }
@ -918,7 +918,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
formatRaw(node); formatRaw(node);
} }
} finally { } finally {
endOfNode(node); finishNode(node);
} }
return PROCESS_SKIP; return PROCESS_SKIP;
} }
@ -928,7 +928,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
*/ */
@Override @Override
public int visit(IASTStatement node) { public int visit(IASTStatement node) {
if (scribe.scanner.getCurrentPosition() <= node.getFileLocation().getNodeOffset() && if (getCurrentPosition() <= nodeOffset(node) &&
startsWithMacroExpansion(node)) { startsWithMacroExpansion(node)) {
scribe.printCommentPreservingNewLines(); scribe.printCommentPreservingNewLines();
} }
@ -986,7 +986,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
} }
} }
} finally { } finally {
endOfNode(node); finishNode(node);
} }
return PROCESS_SKIP; return PROCESS_SKIP;
} }
@ -1016,7 +1016,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
declarator.accept(this); declarator.accept(this);
} }
} finally { } finally {
endOfNode(node); finishNode(node);
} }
return PROCESS_SKIP; return PROCESS_SKIP;
} }
@ -1041,7 +1041,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
value.accept(this); value.accept(this);
} }
} finally { } finally {
endOfNode(enumerator); finishNode(enumerator);
} }
return PROCESS_SKIP; return PROCESS_SKIP;
} }
@ -1073,7 +1073,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
} }
specifier.getName().accept(this); specifier.getName().accept(this);
} finally { } finally {
endOfNode(specifier); finishNode(specifier);
} }
return PROCESS_SKIP; return PROCESS_SKIP;
} }
@ -1105,7 +1105,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
} }
formatClosingBrace(preferences.brace_position_for_namespace_declaration); formatClosingBrace(preferences.brace_position_for_namespace_declaration);
} finally { } finally {
endOfNode(node); finishNode(node);
} }
return PROCESS_SKIP; return PROCESS_SKIP;
} }
@ -1219,7 +1219,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
} catch (ASTProblemException e) { } catch (ASTProblemException e) {
skipNode(node); skipNode(node);
} finally { } finally {
endOfNode(node); finishNode(node);
} }
return PROCESS_SKIP; return PROCESS_SKIP;
} }
@ -1290,7 +1290,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
// Format like a function call // Format like a function call
formatFunctionCallArguments(node.getArguments()); formatFunctionCallArguments(node.getArguments());
} finally { } finally {
endOfNode(node); finishNode(node);
} }
return PROCESS_SKIP; return PROCESS_SKIP;
} }
@ -1329,8 +1329,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
if (DefaultCodeFormatterConstants.END_OF_LINE.equals(preferences.brace_position_for_method_declaration) && if (DefaultCodeFormatterConstants.END_OF_LINE.equals(preferences.brace_position_for_method_declaration) &&
!hasMemberInitializers(node) && !(node instanceof ICPPASTFunctionWithTryBlock)) { !hasMemberInitializers(node) && !(node instanceof ICPPASTFunctionWithTryBlock)) {
if (bodyStmt instanceof IASTCompoundStatement && !startsWithMacroExpansion(bodyStmt)) { if (bodyStmt instanceof IASTCompoundStatement && !startsWithMacroExpansion(bodyStmt)) {
tailFormatter = new TrailingTokenFormatter(Token.tLBRACE, tailFormatter = new TrailingTokenFormatter(Token.tLBRACE, nodeOffset(bodyStmt),
bodyStmt.getFileLocation().getNodeOffset(),
preferences.insert_space_before_opening_brace_in_method_declaration, false); preferences.insert_space_before_opening_brace_in_method_declaration, false);
scribe.setTailFormatter(tailFormatter); scribe.setTailFormatter(tailFormatter);
} }
@ -1376,7 +1375,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
if (bodyStmt instanceof IASTCompoundStatement) { if (bodyStmt instanceof IASTCompoundStatement) {
if (startNode(bodyStmt)) { if (startNode(bodyStmt)) {
try { try {
if (scribe.scanner.getCurrentPosition() <= bodyStmt.getFileLocation().getNodeOffset()) { if (getCurrentPosition() <= nodeOffset(bodyStmt)) {
formatLeftCurlyBrace(line, preferences.brace_position_for_method_declaration); formatLeftCurlyBrace(line, preferences.brace_position_for_method_declaration);
} }
formatBlock((IASTCompoundStatement) bodyStmt, formatBlock((IASTCompoundStatement) bodyStmt,
@ -1384,7 +1383,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
preferences.insert_space_before_opening_brace_in_method_declaration, preferences.insert_space_before_opening_brace_in_method_declaration,
preferences.indent_statements_compare_to_body); preferences.indent_statements_compare_to_body);
} finally { } finally {
endOfNode(bodyStmt); finishNode(bodyStmt);
} }
} }
} else { } else {
@ -1468,7 +1467,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
Alignment.EXCEPTION_SPECIFICATION, Alignment.EXCEPTION_SPECIFICATION,
preferences.alignment_for_throws_clause_in_method_declaration, preferences.alignment_for_throws_clause_in_method_declaration,
exceptionSpecification.length, exceptionSpecification.length,
scribe.scanner.getCurrentPosition()); getCurrentPosition());
scribe.enterAlignment(alignment); scribe.enterAlignment(alignment);
boolean ok = false; boolean ok = false;
@ -1789,7 +1788,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
} }
// Consider macro expansion // Consider macro expansion
if (withinMacroExpansion(node, scribe.scanner.getCurrentPosition())) { if (withinMacroExpansion(node, getCurrentPosition())) {
scribe.printNextToken(peekNextToken()); scribe.printNextToken(peekNextToken());
continueNode(node); continueNode(node);
if (scribe.printComment()) scribe.space(); if (scribe.printComment()) scribe.space();
@ -1842,7 +1841,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
final int headerIndent= scribe.numberOfIndentations; final int headerIndent= scribe.numberOfIndentations;
// Consider macro expansion // Consider macro expansion
if (withinMacroExpansion(node, scribe.scanner.getCurrentPosition())) { if (withinMacroExpansion(node, getCurrentPosition())) {
scribe.printNextToken(peekNextToken()); scribe.printNextToken(peekNextToken());
continueNode(node); continueNode(node);
if (scribe.printComment()) if (scribe.printComment())
@ -1890,7 +1889,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
if (preferences.indent_access_specifier_compare_to_type_header) { if (preferences.indent_access_specifier_compare_to_type_header) {
scribe.indent(); scribe.indent();
} }
if (scribe.scanner.getCurrentPosition() >= getNodeEndPosition(node)) { if (getCurrentPosition() >= nodeEndOffset(node)) {
return PROCESS_SKIP; return PROCESS_SKIP;
} }
scribe.startNewLine(); scribe.startNewLine();
@ -1911,7 +1910,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
visit((ICPPASTVisibilityLabel) declaration); visit((ICPPASTVisibilityLabel) declaration);
scribe.startNewLine(); scribe.startNewLine();
} finally { } finally {
endOfNode(declaration); finishNode(declaration);
} }
} }
} else { } else {
@ -1920,7 +1919,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
scribe.startNewLine(); scribe.startNewLine();
formatDeclaration(declaration); formatDeclaration(declaration);
} finally { } finally {
endOfNode(declaration); finishNode(declaration);
} }
} else { } else {
skipNode(declaration); skipNode(declaration);
@ -2087,7 +2086,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
options.fMode, options.fMode,
options.fTieBreakRule, options.fTieBreakRule,
elementsLength + (addEllipsis ? 1 : 0), elementsLength + (addEllipsis ? 1 : 0),
scribe.scanner.getCurrentPosition(), getCurrentPosition(),
continuationIndentation, continuationIndentation,
false); false);
scribe.enterAlignment(alignment); scribe.enterAlignment(alignment);
@ -2230,7 +2229,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
preferences.alignment_for_conditional_expression_chain, preferences.alignment_for_conditional_expression_chain,
Alignment.R_OUTERMOST, Alignment.R_OUTERMOST,
numConditions, numConditions,
scribe.scanner.getCurrentPosition()); getCurrentPosition());
scribe.enterAlignment(alignment); scribe.enterAlignment(alignment);
boolean ok = false; boolean ok = false;
@ -2270,7 +2269,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
preferences.alignment_for_conditional_expression, preferences.alignment_for_conditional_expression,
Alignment.R_OUTERMOST, Alignment.R_OUTERMOST,
negativeExpression instanceof IASTConditionalExpression ? 1 : 2, negativeExpression instanceof IASTConditionalExpression ? 1 : 2,
scribe.scanner.getCurrentPosition()); getCurrentPosition());
scribe.enterAlignment(alignment); scribe.enterAlignment(alignment);
boolean ok = false; boolean ok = false;
@ -2278,7 +2277,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
try { try {
// In case of macros we may have already passed the expression position. // In case of macros we may have already passed the expression position.
if (positiveExpression != null && if (positiveExpression != null &&
scribe.scanner.getCurrentPosition() <= positiveExpression.getFileLocation().getNodeOffset()) { getCurrentPosition() <= nodeOffset(positiveExpression)) {
scribe.alignFragment(alignment, 0); scribe.alignFragment(alignment, 0);
} }
scribe.setTailFormatter(new TrailingTokenFormatter(Token.tCOLON, node, scribe.setTailFormatter(new TrailingTokenFormatter(Token.tCOLON, node,
@ -2292,7 +2291,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
if (!(negativeExpression instanceof IASTConditionalExpression)) { if (!(negativeExpression instanceof IASTConditionalExpression)) {
// In case of macros we may have already passed the expression position. // In case of macros we may have already passed the expression position.
if (scribe.scanner.getCurrentPosition() <= negativeExpression.getFileLocation().getNodeOffset()) { if (getCurrentPosition() <= nodeOffset(negativeExpression)) {
scribe.alignFragment(alignment, 1); scribe.alignFragment(alignment, 1);
} }
scribe.setTailFormatter(tailFormatter); scribe.setTailFormatter(tailFormatter);
@ -2448,7 +2447,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
preferences.alignment_for_assignment, preferences.alignment_for_assignment,
Alignment.R_INNERMOST, Alignment.R_INNERMOST,
1, 1,
scribe.scanner.getCurrentPosition()); getCurrentPosition());
Runnable tailFormatter = scribe.getTailFormatter(); Runnable tailFormatter = scribe.getTailFormatter();
scribe.enterAlignment(alignment); scribe.enterAlignment(alignment);
@ -2489,7 +2488,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
Alignment.DESIGNATED_INITIALIZER, Alignment.DESIGNATED_INITIALIZER,
preferences.alignment_for_assignment, preferences.alignment_for_assignment,
1, 1,
scribe.scanner.getCurrentPosition()); getCurrentPosition());
scribe.enterAlignment(expressionAlignment); scribe.enterAlignment(expressionAlignment);
boolean ok = false; boolean ok = false;
@ -2654,7 +2653,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
preferences.alignment_for_binary_expression, preferences.alignment_for_binary_expression,
Alignment.R_OUTERMOST, Alignment.R_OUTERMOST,
operands.length, operands.length,
scribe.scanner.getCurrentPosition()); getCurrentPosition());
scribe.enterAlignment(alignment); scribe.enterAlignment(alignment);
boolean ok = false; boolean ok = false;
@ -2663,7 +2662,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
for (int i = 0; i < operands.length; i++) { for (int i = 0; i < operands.length; i++) {
final IASTExpression operand = operands[i]; final IASTExpression operand = operands[i];
// In case of macros we may have already passed the operator position. // In case of macros we may have already passed the operator position.
if (i > 0 && scribe.scanner.getCurrentPosition() < operand.getFileLocation().getNodeOffset()) { if (i > 0 && getCurrentPosition() < nodeOffset(operand)) {
scribe.alignFragment(alignment, i); scribe.alignFragment(alignment, i);
// Operator // Operator
@ -2689,7 +2688,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
scribe.setTailFormatter(tailFormatter); scribe.setTailFormatter(tailFormatter);
} }
operand.accept(this); operand.accept(this);
scribe.restartAtOffset(getNodeEndPosition(operand)); scribe.restartAtOffset(nodeEndOffset(operand));
scribe.printTrailingComment(); scribe.printTrailingComment();
} }
@ -2710,7 +2709,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
op1.accept(this); op1.accept(this);
// In case of macros we may have already passed the equal sign position. // In case of macros we may have already passed the equal sign position.
if (scribe.scanner.getCurrentPosition() < node.getOperand2().getFileLocation().getNodeOffset()) { if (getCurrentPosition() < nodeOffset(node.getOperand2())) {
// Operator // Operator
final int nextToken= peekNextToken(); final int nextToken= peekNextToken();
// In case of C++ alternative operators, like 'and', 'not', etc. a space // In case of C++ alternative operators, like 'and', 'not', etc. a space
@ -2727,7 +2726,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
preferences.alignment_for_assignment, preferences.alignment_for_assignment,
Alignment.R_INNERMOST, Alignment.R_INNERMOST,
1, 1,
scribe.scanner.getCurrentPosition()); getCurrentPosition());
scribe.enterAlignment(expressionAlignment); scribe.enterAlignment(expressionAlignment);
boolean ok = false; boolean ok = false;
@ -2792,7 +2791,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
preferences.alignment_for_overloaded_left_shift_chain, preferences.alignment_for_overloaded_left_shift_chain,
Alignment.R_OUTERMOST, Alignment.R_OUTERMOST,
elements.size(), elements.size(),
scribe.scanner.getCurrentPosition(), getCurrentPosition(),
preferences.continuation_indentation, preferences.continuation_indentation,
false); false);
scribe.enterAlignment(alignment); scribe.enterAlignment(alignment);
@ -2802,7 +2801,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
for (int i = 0; i < elements.size(); i++) { for (int i = 0; i < elements.size(); i++) {
node= elements.get(i); node= elements.get(i);
// In case of macros we may have already passed the operator position. // In case of macros we may have already passed the operator position.
if (scribe.scanner.getCurrentPosition() < node.getFileLocation().getNodeOffset()) { if (getCurrentPosition() < nodeOffset(node)) {
scribe.alignFragment(alignment, i); scribe.alignFragment(alignment, i);
int token= peekNextToken(); int token= peekNextToken();
if (token == Token.tSHIFTL) { if (token == Token.tSHIFTL) {
@ -2895,7 +2894,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
preferences.alignment_for_member_access, preferences.alignment_for_member_access,
Alignment.R_OUTERMOST, Alignment.R_OUTERMOST,
1, 1,
scribe.scanner.getCurrentPosition()); getCurrentPosition());
scribe.enterAlignment(alignment); scribe.enterAlignment(alignment);
boolean ok = false; boolean ok = false;
@ -3073,7 +3072,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
final IASTStatement action = node.getBody(); final IASTStatement action = node.getBody();
formatAction(line, action, preferences.brace_position_for_block); formatAction(line, action, preferences.brace_position_for_block);
if (scribe.scanner.getCurrentPosition() < getNodeEndPosition(node)) { if (getCurrentPosition() < nodeEndOffset(node)) {
if (peekNextToken() == Token.t_while) { if (peekNextToken() == Token.t_while) {
if (preferences.insert_new_line_before_while_in_do_statement) { if (preferences.insert_new_line_before_while_in_do_statement) {
scribe.startNewLine(); scribe.startNewLine();
@ -3096,8 +3095,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
} }
private int visit(IASTNullStatement node) { private int visit(IASTNullStatement node) {
if (!fInsideFor && if (!fInsideFor && nodeOffset(node) == getCurrentPosition()) {
node.getFileLocation().getNodeOffset() == scribe.scanner.getCurrentPosition()) {
scribe.printNextToken(Token.tSEMI, preferences.insert_space_before_semicolon); scribe.printNextToken(Token.tSEMI, preferences.insert_space_before_semicolon);
scribe.printTrailingComment(); scribe.printTrailingComment();
} }
@ -3145,8 +3143,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
} }
if (DefaultCodeFormatterConstants.END_OF_LINE.equals(preferences.brace_position_for_block) && if (DefaultCodeFormatterConstants.END_OF_LINE.equals(preferences.brace_position_for_block) &&
body instanceof IASTCompoundStatement && !startsWithMacroExpansion(body)) { body instanceof IASTCompoundStatement && !startsWithMacroExpansion(body)) {
tailFormatter = new TrailingTokenFormatter(Token.tLBRACE, tailFormatter = new TrailingTokenFormatter(Token.tLBRACE, nodeOffset(body),
body.getFileLocation().getNodeOffset(),
preferences.insert_space_before_opening_brace_in_block, false); preferences.insert_space_before_opening_brace_in_block, false);
} }
tailFormatter = new ClosingParensesisTailFormatter( tailFormatter = new ClosingParensesisTailFormatter(
@ -3163,7 +3160,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
Alignment.M_COMPACT_SPLIT, Alignment.M_COMPACT_SPLIT,
Alignment.R_OUTERMOST, Alignment.R_OUTERMOST,
2, 2,
scribe.scanner.getCurrentPosition()); getCurrentPosition());
scribe.enterAlignment(alignment); scribe.enterAlignment(alignment);
boolean ok = false; boolean ok = false;
@ -3216,7 +3213,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
if (body instanceof IASTCompoundStatement && !startsWithMacroExpansion(body)) { if (body instanceof IASTCompoundStatement && !startsWithMacroExpansion(body)) {
if (startNode(body)) { if (startNode(body)) {
try { try {
if (scribe.scanner.getCurrentPosition() <= body.getFileLocation().getNodeOffset()) { if (getCurrentPosition() <= nodeOffset(body)) {
formatLeftCurlyBrace(line, preferences.brace_position_for_block); formatLeftCurlyBrace(line, preferences.brace_position_for_block);
} }
formatBlock((IASTCompoundStatement) body, formatBlock((IASTCompoundStatement) body,
@ -3224,7 +3221,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
preferences.insert_space_before_opening_brace_in_block, preferences.insert_space_before_opening_brace_in_block,
preferences.indent_statements_compare_to_block); preferences.indent_statements_compare_to_block);
} finally { } finally {
endOfNode(body); finishNode(body);
} }
} }
} else { } else {
@ -3284,8 +3281,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
Runnable tailFormatter = null; Runnable tailFormatter = null;
if (DefaultCodeFormatterConstants.END_OF_LINE.equals(preferences.brace_position_for_block) && if (DefaultCodeFormatterConstants.END_OF_LINE.equals(preferences.brace_position_for_block) &&
thenStatement instanceof IASTCompoundStatement && !startsWithMacroExpansion(thenStatement)) { thenStatement instanceof IASTCompoundStatement && !startsWithMacroExpansion(thenStatement)) {
tailFormatter = new TrailingTokenFormatter(Token.tLBRACE, tailFormatter = new TrailingTokenFormatter(Token.tLBRACE, nodeOffset(thenStatement),
thenStatement.getFileLocation().getNodeOffset(),
preferences.insert_space_before_opening_brace_in_block, false); preferences.insert_space_before_opening_brace_in_block, false);
} }
tailFormatter = new ClosingParensesisTailFormatter( tailFormatter = new ClosingParensesisTailFormatter(
@ -3316,7 +3312,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
if (isGuardClause(block, statements) && elseStatement == null && preferences.keep_guardian_clause_on_one_line) { if (isGuardClause(block, statements) && elseStatement == null && preferences.keep_guardian_clause_on_one_line) {
// Specific formatting for guard clauses. A guard clause is a block // Specific formatting for guard clauses. A guard clause is a block
// with a single return or throw statement. // with a single return or throw statement.
if (scribe.scanner.getCurrentPosition() <= thenStatement.getFileLocation().getNodeOffset()) { if (getCurrentPosition() <= nodeOffset(thenStatement)) {
scribe.printNextToken(Token.tLBRACE, preferences.insert_space_before_opening_brace_in_block); scribe.printNextToken(Token.tLBRACE, preferences.insert_space_before_opening_brace_in_block);
scribe.space(); scribe.space();
} }
@ -3324,7 +3320,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
scribe.printNextToken(Token.tRBRACE, true); scribe.printNextToken(Token.tRBRACE, true);
scribe.printTrailingComment(); scribe.printTrailingComment();
} else { } else {
if (scribe.scanner.getCurrentPosition() <= thenStatement.getFileLocation().getNodeOffset()) { if (getCurrentPosition() <= nodeOffset(thenStatement)) {
formatLeftCurlyBrace(line, preferences.brace_position_for_block); formatLeftCurlyBrace(line, preferences.brace_position_for_block);
} }
thenStatement.accept(this); thenStatement.accept(this);
@ -3342,7 +3338,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
preferences.alignment_for_compact_if, preferences.alignment_for_compact_if,
Alignment.R_OUTERMOST, Alignment.R_OUTERMOST,
1, 1,
scribe.scanner.getCurrentPosition(), getCurrentPosition(),
1, 1,
false); false);
scribe.enterAlignment(compactIfAlignment); scribe.enterAlignment(compactIfAlignment);
@ -3638,7 +3634,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
skipToNode(nextStatement); skipToNode(nextStatement);
} }
} finally { } finally {
endOfNode(statement); finishNode(statement);
} }
if (preferences.indent_switchstatements_compare_to_cases) { if (preferences.indent_switchstatements_compare_to_cases) {
scribe.indent(); scribe.indent();
@ -3656,7 +3652,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
skipToNode(nextStatement); skipToNode(nextStatement);
} }
} finally { } finally {
endOfNode(statement); finishNode(statement);
} }
} }
wasAStatement = true; wasAStatement = true;
@ -3696,7 +3692,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
formatClosingBrace(brace_position); formatClosingBrace(brace_position);
} }
} finally { } finally {
endOfNode(bodyStmt); finishNode(bodyStmt);
} }
return PROCESS_SKIP; return PROCESS_SKIP;
} }
@ -3769,7 +3765,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
IASTPreprocessorMacroExpansion macroExpansion = location.getExpansion(); IASTPreprocessorMacroExpansion macroExpansion = location.getExpansion();
IASTFileLocation macroLocation = macroExpansion.getFileLocation(); IASTFileLocation macroLocation = macroExpansion.getFileLocation();
IASTFileLocation nodeLocation = node.getFileLocation(); IASTFileLocation nodeLocation = node.getFileLocation();
if (macroLocation.getNodeOffset() >= scribe.scanner.getCurrentPosition() && if (macroLocation.getNodeOffset() >= getCurrentPosition() &&
!scribe.shouldSkip(macroLocation.getNodeOffset()) && !scribe.shouldSkip(macroLocation.getNodeOffset()) &&
(nodeLocation.getNodeOffset() + nodeLocation.getNodeLength() == (nodeLocation.getNodeOffset() + nodeLocation.getNodeLength() ==
macroLocation.getNodeOffset() + macroLocation.getNodeLength() || macroLocation.getNodeOffset() + macroLocation.getNodeLength() ||
@ -3783,7 +3779,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
int startOffset= expansionLocation.getNodeOffset(); int startOffset= expansionLocation.getNodeOffset();
int endOffset= startOffset + expansionLocation.getNodeLength(); int endOffset= startOffset + expansionLocation.getNodeLength();
scribe.skipRange(startOffset, endOffset); scribe.skipRange(startOffset, endOffset);
if (locations.length == 1 && endOffset <= scribe.scanner.getCurrentPosition()) { if (locations.length == 1 && endOffset <= getCurrentPosition()) {
scribe.restartAtOffset(endOffset); scribe.restartAtOffset(endOffset);
continueNode(node.getParent()); continueNode(node.getParent());
return false; return false;
@ -3800,7 +3796,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
* *
* @param node * @param node
*/ */
private void endOfNode(IASTNode node) { private void finishNode(IASTNode node) {
if (node instanceof IASTProblemHolder) { if (node instanceof IASTProblemHolder) {
return; return;
} }
@ -3832,7 +3828,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
} }
int nodeOffset= fileLocation.getNodeOffset(); int nodeOffset= fileLocation.getNodeOffset();
int nodeEndOffset= nodeOffset + fileLocation.getNodeLength(); int nodeEndOffset= nodeOffset + fileLocation.getNodeLength();
int currentOffset= scribe.scanner.getCurrentPosition(); int currentOffset= getCurrentPosition();
if (currentOffset > nodeEndOffset) { if (currentOffset > nodeEndOffset) {
return; return;
} }
@ -3856,7 +3852,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
} }
private int getNextTokenOffset() { private int getNextTokenOffset() {
localScanner.resetTo(scribe.scanner.getCurrentPosition(), scribe.scannerEndPosition); localScanner.resetTo(getCurrentPosition(), scribe.scannerEndPosition);
localScanner.getNextToken(); localScanner.getNextToken();
return localScanner.getCurrentTokenStartPosition(); return localScanner.getCurrentTokenStartPosition();
} }
@ -3865,7 +3861,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
final IASTNodeLocation fileLocation= node.getFileLocation(); final IASTNodeLocation fileLocation= node.getFileLocation();
if (fileLocation != null && fileLocation.getNodeLength() > 0) { if (fileLocation != null && fileLocation.getNodeLength() > 0) {
final int endOffset= fileLocation.getNodeOffset() + fileLocation.getNodeLength(); final int endOffset= fileLocation.getNodeOffset() + fileLocation.getNodeLength();
final int currentOffset= scribe.scanner.getCurrentPosition(); final int currentOffset= getCurrentPosition();
final int restLength= endOffset - currentOffset; final int restLength= endOffset - currentOffset;
if (restLength > 0) { if (restLength > 0) {
scribe.printRaw(currentOffset, restLength); scribe.printRaw(currentOffset, restLength);
@ -3877,7 +3873,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
final IASTNodeLocation fileLocation= node.getFileLocation(); final IASTNodeLocation fileLocation= node.getFileLocation();
if (fileLocation != null) { if (fileLocation != null) {
final int startOffset= fileLocation.getNodeOffset(); final int startOffset= fileLocation.getNodeOffset();
final int currentOffset= scribe.scanner.getCurrentPosition(); final int currentOffset= getCurrentPosition();
final int restLength= startOffset - currentOffset; final int restLength= startOffset - currentOffset;
if (restLength > 0) { if (restLength > 0) {
scribe.printRaw(currentOffset, restLength); scribe.printRaw(currentOffset, restLength);
@ -3891,7 +3887,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
final int startOffset= fileLocation.getNodeOffset(); final int startOffset= fileLocation.getNodeOffset();
final int nextTokenOffset= getNextTokenOffset(); final int nextTokenOffset= getNextTokenOffset();
if (nextTokenOffset < startOffset) { if (nextTokenOffset < startOffset) {
final int currentOffset= scribe.scanner.getCurrentPosition(); final int currentOffset= getCurrentPosition();
final int restLength= startOffset - currentOffset; final int restLength= startOffset - currentOffset;
if (restLength > 0) { if (restLength > 0) {
scribe.printRaw(currentOffset, restLength); scribe.printRaw(currentOffset, restLength);
@ -3939,7 +3935,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
preferences.insert_space_before_opening_brace_in_block, preferences.insert_space_before_opening_brace_in_block,
preferences.indent_statements_compare_to_block); preferences.indent_statements_compare_to_block);
} finally { } finally {
endOfNode(stmt); finishNode(stmt);
} }
} }
} else if (stmt instanceof IASTNullStatement) { } else if (stmt instanceof IASTNullStatement) {
@ -3953,7 +3949,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
// Don't insert a line break if we have already passed the start of the statement. // Don't insert a line break if we have already passed the start of the statement.
// This is possible with macro expansions. // This is possible with macro expansions.
boolean indented = false; boolean indented = false;
if (scribe.scanner.getCurrentPosition() <= stmt.getFileLocation().getNodeOffset()) { if (getCurrentPosition() <= nodeOffset(stmt)) {
scribe.printTrailingComment(); scribe.printTrailingComment();
scribe.startNewLine(); scribe.startNewLine();
scribe.indent(); scribe.indent();
@ -4010,6 +4006,10 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
return true; return true;
} }
private int getCurrentPosition() {
return scribe.scanner.getCurrentPosition();
}
/** /**
* Returns <code>true</code> if the given macro expansion is followed by a semicolon on the same * Returns <code>true</code> if the given macro expansion is followed by a semicolon on the same
* line. * line.
@ -4051,10 +4051,14 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
* expansion. * expansion.
*/ */
private static boolean doNodesHaveSameOffset(IASTNode node1, IASTNode node2) { private static boolean doNodesHaveSameOffset(IASTNode node1, IASTNode node2) {
return node1.getFileLocation().getNodeOffset() == node2.getFileLocation().getNodeOffset(); return nodeOffset(node1) == nodeOffset(node2);
} }
private static int getNodeEndPosition(IASTNode node) { private static int nodeOffset(IASTNode node) {
return node.getFileLocation().getNodeOffset();
}
private static int nodeEndOffset(IASTNode node) {
IASTFileLocation loc = node.getFileLocation(); IASTFileLocation loc = node.getFileLocation();
return loc.getNodeOffset() + loc.getNodeLength(); return loc.getNodeOffset() + loc.getNodeLength();
} }
@ -4072,7 +4076,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
scribe.startNewLine(); scribe.startNewLine();
scribe.printComment(); scribe.printComment();
} }
} else if (scribe.scanner.getCurrentPosition() <= block.getFileLocation().getNodeOffset()) { } else if (getCurrentPosition() <= nodeOffset(block)) {
formatOpeningBrace(block_brace_position, insertSpaceBeforeOpeningBrace); formatOpeningBrace(block_brace_position, insertSpaceBeforeOpeningBrace);
} }
} }
@ -4240,10 +4244,10 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
} }
private int peekNextToken(boolean ignoreSkip) { private int peekNextToken(boolean ignoreSkip) {
if (!ignoreSkip && scribe.shouldSkip(scribe.scanner.getCurrentPosition())) { if (!ignoreSkip && scribe.shouldSkip(getCurrentPosition())) {
return Token.tBADCHAR; return Token.tBADCHAR;
} }
localScanner.resetTo(scribe.scanner.getCurrentPosition(), scribe.scannerEndPosition); localScanner.resetTo(getCurrentPosition(), scribe.scannerEndPosition);
int token = localScanner.getNextToken(); int token = localScanner.getNextToken();
while (token == Token.tBLOCKCOMMENT || token == Token.tLINECOMMENT) { while (token == Token.tBLOCKCOMMENT || token == Token.tLINECOMMENT) {
token = localScanner.getNextToken(); token = localScanner.getNextToken();
@ -4266,7 +4270,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
if (fileLocation == null) { if (fileLocation == null) {
return false; return false;
} }
int blockStartPosition= block.getFileLocation().getNodeOffset(); int blockStartPosition= nodeOffset(block);
int blockLength= block.getFileLocation().getNodeLength(); int blockLength= block.getFileLocation().getNodeLength();
if (commentStartsBlock(blockStartPosition, blockLength)) return false; if (commentStartsBlock(blockStartPosition, blockLength)) return false;
final int statementsLength = statements.size(); final int statementsLength = statements.size();