mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-08-10 17:55:39 +02:00
Fallback formatting of function declarations and calls.
This commit is contained in:
parent
8ed19fb591
commit
1502e06ab7
7 changed files with 205 additions and 127 deletions
|
@ -114,7 +114,7 @@ public class DefaultCodeFormatterConstants {
|
||||||
* FORMATTER / Option for alignment of assignment
|
* FORMATTER / Option for alignment of assignment
|
||||||
* - option id: "org.eclipse.cdt.core.formatter.alignment_for_assignment"
|
* - option id: "org.eclipse.cdt.core.formatter.alignment_for_assignment"
|
||||||
* - possible values: values returned by <code>createAlignmentValue(boolean, int, int)</code> call
|
* - possible values: values returned by <code>createAlignmentValue(boolean, int, int)</code> call
|
||||||
* - default: createAlignmentValue(false, M_NO_ALIGNMENT, INDENT_DEFAULT)
|
* - default: createAlignmentValue(false, WRAP_COMPACT, INDENT_DEFAULT)
|
||||||
* </pre>
|
* </pre>
|
||||||
* @see #createAlignmentValue(boolean, int, int)
|
* @see #createAlignmentValue(boolean, int, int)
|
||||||
* @since 5.3
|
* @since 5.3
|
||||||
|
|
|
@ -169,18 +169,19 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class ListOptions {
|
private static class ListOptions {
|
||||||
public final int fMode;
|
final int fMode;
|
||||||
public boolean fSpaceBeforeComma;
|
boolean fUseFallbackMode;
|
||||||
public boolean fSpaceAfterComma= true;
|
boolean fSpaceBeforeComma;
|
||||||
public boolean fSpaceAfterOpeningParen;
|
boolean fSpaceAfterComma = true;
|
||||||
public boolean fSpaceBeforeClosingParen;
|
boolean fSpaceAfterOpeningParen;
|
||||||
public boolean fSpaceBetweenEmptyParen;
|
boolean fSpaceBeforeClosingParen;
|
||||||
public boolean fSpaceBeforeOpeningParen;
|
boolean fSpaceBetweenEmptyParen;
|
||||||
public int fContinuationIndentation= -1;
|
boolean fSpaceBeforeOpeningParen;
|
||||||
public int fTieBreakRule = Alignment.R_INNERMOST;
|
int fContinuationIndentation = -1;
|
||||||
|
int fTieBreakRule = Alignment.R_INNERMOST;
|
||||||
|
|
||||||
public ListOptions(int mode) {
|
ListOptions(int mode) {
|
||||||
fMode= mode;
|
this.fMode = mode;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1339,7 +1340,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
|
||||||
|
|
||||||
private int visit(ICPPASTFunctionDeclarator node) {
|
private int visit(ICPPASTFunctionDeclarator node) {
|
||||||
final List<ICPPASTParameterDeclaration> parameters = Arrays.asList(node.getParameters());
|
final List<ICPPASTParameterDeclaration> parameters = Arrays.asList(node.getParameters());
|
||||||
final ListOptions options = createListOptionsForFunctionParameters();
|
final ListOptions options = createListOptionsForFunctionDeclarationParameters();
|
||||||
formatList(parameters, options, true, node.takesVarArgs(),
|
formatList(parameters, options, true, node.takesVarArgs(),
|
||||||
new CPPFunctionDeclaratorTailFormatter(node, scribe.getTailFormatter()));
|
new CPPFunctionDeclaratorTailFormatter(node, scribe.getTailFormatter()));
|
||||||
|
|
||||||
|
@ -1416,13 +1417,14 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
|
||||||
|
|
||||||
private int visit(IASTStandardFunctionDeclarator node) {
|
private int visit(IASTStandardFunctionDeclarator node) {
|
||||||
final List<IASTParameterDeclaration> parameters = Arrays.asList(node.getParameters());
|
final List<IASTParameterDeclaration> parameters = Arrays.asList(node.getParameters());
|
||||||
final ListOptions options = createListOptionsForFunctionParameters();
|
final ListOptions options = createListOptionsForFunctionDeclarationParameters();
|
||||||
formatList(parameters, options, true, node.takesVarArgs(), new TrailingSemicolonFormatter(node));
|
formatList(parameters, options, true, node.takesVarArgs(), new TrailingSemicolonFormatter(node));
|
||||||
return PROCESS_SKIP;
|
return PROCESS_SKIP;
|
||||||
}
|
}
|
||||||
|
|
||||||
private ListOptions createListOptionsForFunctionParameters() {
|
private ListOptions createListOptionsForFunctionDeclarationParameters() {
|
||||||
final ListOptions options= new ListOptions(preferences.alignment_for_parameters_in_method_declaration);
|
final ListOptions options= new ListOptions(preferences.alignment_for_parameters_in_method_declaration);
|
||||||
|
options.fUseFallbackMode= true;
|
||||||
options.fSpaceBeforeOpeningParen= preferences.insert_space_before_opening_paren_in_method_declaration;
|
options.fSpaceBeforeOpeningParen= preferences.insert_space_before_opening_paren_in_method_declaration;
|
||||||
options.fSpaceAfterOpeningParen= preferences.insert_space_after_opening_paren_in_method_declaration;
|
options.fSpaceAfterOpeningParen= preferences.insert_space_after_opening_paren_in_method_declaration;
|
||||||
options.fSpaceBeforeClosingParen= preferences.insert_space_before_closing_paren_in_method_declaration;
|
options.fSpaceBeforeClosingParen= preferences.insert_space_before_closing_paren_in_method_declaration;
|
||||||
|
@ -1484,12 +1486,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
|
||||||
|
|
||||||
private int visit(ICASTKnRFunctionDeclarator node) {
|
private int visit(ICASTKnRFunctionDeclarator node) {
|
||||||
final List<IASTName> parameters= Arrays.asList(node.getParameterNames());
|
final List<IASTName> parameters= Arrays.asList(node.getParameterNames());
|
||||||
ListOptions options= new ListOptions(preferences.alignment_for_parameters_in_method_declaration);
|
ListOptions options= createListOptionsForFunctionDeclarationParameters();
|
||||||
options.fSpaceAfterOpeningParen= preferences.insert_space_after_opening_paren_in_method_declaration;
|
|
||||||
options.fSpaceBeforeClosingParen= preferences.insert_space_before_closing_paren_in_method_declaration;
|
|
||||||
options.fSpaceBetweenEmptyParen= preferences.insert_space_between_empty_parens_in_method_declaration;
|
|
||||||
options.fSpaceBeforeComma= preferences.insert_space_before_comma_in_method_declaration_parameters;
|
|
||||||
options.fSpaceAfterComma= preferences.insert_space_after_comma_in_method_declaration_parameters;
|
|
||||||
formatList(parameters, options, true, false, null);
|
formatList(parameters, options, true, false, null);
|
||||||
|
|
||||||
IASTDeclaration[] parameterDecls= node.getParameterDeclarations();
|
IASTDeclaration[] parameterDecls= node.getParameterDeclarations();
|
||||||
|
@ -1567,12 +1564,22 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
|
||||||
scribe.space();
|
scribe.space();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
final ListOptions options= new ListOptions(preferences.alignment_for_declarator_list);
|
|
||||||
options.fSpaceAfterComma= preferences.insert_space_after_comma_in_declarator_list;
|
|
||||||
options.fSpaceBeforeComma= preferences.insert_space_before_comma_in_declarator_list;
|
|
||||||
Runnable tailFormatter = fExpectSemicolonAfterDeclaration ?
|
Runnable tailFormatter = fExpectSemicolonAfterDeclaration ?
|
||||||
new TrailingSemicolonFormatter(node) : null;
|
new TrailingSemicolonFormatter(node) : null;
|
||||||
formatList(declarators, options, false, false, tailFormatter);
|
if (declarators.size() == 1) {
|
||||||
|
scribe.setTailFormatter(tailFormatter);
|
||||||
|
try {
|
||||||
|
visit(declarators.get(0));
|
||||||
|
scribe.runTailFormatter();
|
||||||
|
} finally {
|
||||||
|
scribe.setTailFormatter(null);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
final ListOptions options= new ListOptions(preferences.alignment_for_declarator_list);
|
||||||
|
options.fSpaceAfterComma= preferences.insert_space_after_comma_in_declarator_list;
|
||||||
|
options.fSpaceBeforeComma= preferences.insert_space_before_comma_in_declarator_list;
|
||||||
|
formatList(declarators, options, false, false, tailFormatter);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return PROCESS_SKIP;
|
return PROCESS_SKIP;
|
||||||
}
|
}
|
||||||
|
@ -1963,66 +1970,106 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
|
||||||
if (options.fSpaceAfterOpeningParen) {
|
if (options.fSpaceAfterOpeningParen) {
|
||||||
scribe.space();
|
scribe.space();
|
||||||
}
|
}
|
||||||
final int continuationIndentation= options.fContinuationIndentation >= 0 ?
|
Alignment retryAlignment = null;
|
||||||
options.fContinuationIndentation : preferences.continuation_indentation;
|
|
||||||
Alignment alignment = scribe.createAlignment(
|
int fallbackMode = options.fUseFallbackMode ?
|
||||||
Alignment.LIST_ELEMENTS_PREFIX +
|
getFallbackAlignmentMode(options.fMode) : options.fMode;
|
||||||
(elements.isEmpty() ? "ellipsis" : elements.get(0).getClass().getSimpleName()), //$NON-NLS-1$
|
if (fallbackMode != options.fMode) {
|
||||||
options.fMode,
|
retryAlignment = scribe.createAlignment(
|
||||||
options.fTieBreakRule,
|
Alignment.LIST_FALLBACK_TRAP,
|
||||||
elementsLength + (addEllipsis ? 1 : 0),
|
Alignment.M_ONE_PER_LINE_SPLIT,
|
||||||
scribe.scanner.getCurrentPosition(),
|
Alignment.R_INNERMOST,
|
||||||
continuationIndentation,
|
1,
|
||||||
false);
|
scribe.scanner.getCurrentPosition(),
|
||||||
scribe.enterAlignment(alignment);
|
0,
|
||||||
boolean ok = false;
|
false);
|
||||||
|
scribe.enterAlignment(retryAlignment);
|
||||||
|
}
|
||||||
|
boolean success = false;
|
||||||
|
int mode = options.fMode;
|
||||||
do {
|
do {
|
||||||
|
if (retryAlignment != null)
|
||||||
|
scribe.alignFragment(retryAlignment, 0);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
int i;
|
final int continuationIndentation= options.fContinuationIndentation >= 0 ?
|
||||||
for (i = 0; i < elementsLength; i++) {
|
options.fContinuationIndentation : preferences.continuation_indentation;
|
||||||
final IASTNode node= elements.get(i);
|
Alignment alignment = scribe.createAlignment(
|
||||||
if (i < alignment.fragmentCount - 1) {
|
Alignment.LIST_ELEMENTS_PREFIX +
|
||||||
scribe.setTailFormatter(
|
(elements.isEmpty() ? "ellipsis" : elements.get(0).getClass().getSimpleName()), //$NON-NLS-1$
|
||||||
new TrailingCommaFormatter(options.fSpaceBeforeComma,
|
mode,
|
||||||
options.fSpaceAfterComma));
|
options.fTieBreakRule,
|
||||||
} else {
|
elementsLength + (addEllipsis ? 1 : 0),
|
||||||
scribe.setTailFormatter(tailFormatter);
|
scribe.scanner.getCurrentPosition(),
|
||||||
}
|
continuationIndentation,
|
||||||
scribe.alignFragment(alignment, i);
|
false);
|
||||||
if (node instanceof ICPPASTConstructorChainInitializer) {
|
scribe.enterAlignment(alignment);
|
||||||
// Constructor chain initializer is a special case.
|
boolean ok = false;
|
||||||
visit((ICPPASTConstructorChainInitializer) node);
|
do {
|
||||||
} else {
|
try {
|
||||||
node.accept(this);
|
int i;
|
||||||
}
|
for (i = 0; i < elementsLength; i++) {
|
||||||
if (i < alignment.fragmentCount - 1) {
|
final IASTNode node= elements.get(i);
|
||||||
|
if (i < alignment.fragmentCount - 1) {
|
||||||
|
scribe.setTailFormatter(
|
||||||
|
new TrailingCommaFormatter(options.fSpaceBeforeComma,
|
||||||
|
options.fSpaceAfterComma));
|
||||||
|
} else {
|
||||||
|
scribe.setTailFormatter(tailFormatter);
|
||||||
|
}
|
||||||
|
scribe.alignFragment(alignment, i);
|
||||||
|
if (node instanceof ICPPASTConstructorChainInitializer) {
|
||||||
|
// Constructor chain initializer is a special case.
|
||||||
|
visit((ICPPASTConstructorChainInitializer) node);
|
||||||
|
} else {
|
||||||
|
node.accept(this);
|
||||||
|
}
|
||||||
|
if (i < alignment.fragmentCount - 1) {
|
||||||
|
scribe.runTailFormatter();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (addEllipsis) {
|
||||||
|
if (i > 0) {
|
||||||
|
scribe.printNextToken(Token.tCOMMA, options.fSpaceBeforeComma);
|
||||||
|
scribe.printTrailingComment();
|
||||||
|
}
|
||||||
|
scribe.alignFragment(alignment, i);
|
||||||
|
if (i > 0 && options.fSpaceAfterComma) {
|
||||||
|
scribe.space();
|
||||||
|
}
|
||||||
|
scribe.printNextToken(Token.tELIPSE);
|
||||||
|
}
|
||||||
scribe.runTailFormatter();
|
scribe.runTailFormatter();
|
||||||
|
ok = true;
|
||||||
|
} catch (AlignmentException e) {
|
||||||
|
scribe.redoAlignment(e);
|
||||||
|
} catch (ASTProblemException e) {
|
||||||
}
|
}
|
||||||
}
|
} while (!ok);
|
||||||
if (addEllipsis) {
|
scribe.exitAlignment(alignment, true);
|
||||||
if (i > 0) {
|
success = true;
|
||||||
scribe.printNextToken(Token.tCOMMA, options.fSpaceBeforeComma);
|
|
||||||
scribe.printTrailingComment();
|
|
||||||
}
|
|
||||||
scribe.alignFragment(alignment, i);
|
|
||||||
if (i > 0 && options.fSpaceAfterComma) {
|
|
||||||
scribe.space();
|
|
||||||
}
|
|
||||||
scribe.printNextToken(Token.tELIPSE);
|
|
||||||
}
|
|
||||||
scribe.runTailFormatter();
|
|
||||||
ok = true;
|
|
||||||
} catch (AlignmentException e) {
|
} catch (AlignmentException e) {
|
||||||
|
if (retryAlignment == null)
|
||||||
|
throw e;
|
||||||
scribe.redoAlignment(e);
|
scribe.redoAlignment(e);
|
||||||
} catch (ASTProblemException e) {
|
|
||||||
}
|
}
|
||||||
} while (!ok);
|
mode = fallbackMode;
|
||||||
scribe.exitAlignment(alignment, true);
|
} while (!success);
|
||||||
|
if (retryAlignment != null)
|
||||||
|
scribe.exitAlignment(retryAlignment, true);
|
||||||
} else if (tailFormatter != null) {
|
} else if (tailFormatter != null) {
|
||||||
tailFormatter.run();
|
tailFormatter.run();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private int getFallbackAlignmentMode(int alignmentMode) {
|
||||||
|
switch (alignmentMode & Alignment.SPLIT_MASK) {
|
||||||
|
case Alignment.M_COMPACT_SPLIT:
|
||||||
|
alignmentMode = Alignment.M_COMPACT_FIRST_BREAK_SPLIT | (alignmentMode & ~Alignment.SPLIT_MASK);
|
||||||
|
}
|
||||||
|
return alignmentMode & ~Alignment.M_INDENT_ON_COLUMN;
|
||||||
|
}
|
||||||
|
|
||||||
private int visit(ICPPASTTryBlockStatement node) {
|
private int visit(ICPPASTTryBlockStatement node) {
|
||||||
scribe.printNextToken(Token.t_try, scribe.printComment());
|
scribe.printNextToken(Token.t_try, scribe.printComment());
|
||||||
final IASTStatement tryBody= node.getTryBody();
|
final IASTStatement tryBody= node.getTryBody();
|
||||||
|
@ -2176,6 +2223,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
|
||||||
expressions= Collections.emptyList();
|
expressions= Collections.emptyList();
|
||||||
}
|
}
|
||||||
final ListOptions options= new ListOptions(preferences.alignment_for_arguments_in_method_invocation);
|
final ListOptions options= new ListOptions(preferences.alignment_for_arguments_in_method_invocation);
|
||||||
|
options.fUseFallbackMode= true;
|
||||||
options.fSpaceBeforeOpeningParen= preferences.insert_space_before_opening_paren_in_method_invocation;
|
options.fSpaceBeforeOpeningParen= preferences.insert_space_before_opening_paren_in_method_invocation;
|
||||||
options.fSpaceAfterOpeningParen= preferences.insert_space_after_opening_paren_in_method_invocation;
|
options.fSpaceAfterOpeningParen= preferences.insert_space_after_opening_paren_in_method_invocation;
|
||||||
options.fSpaceBeforeClosingParen= preferences.insert_space_before_closing_paren_in_method_invocation;
|
options.fSpaceBeforeClosingParen= preferences.insert_space_before_closing_paren_in_method_invocation;
|
||||||
|
@ -2273,7 +2321,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
|
||||||
Alignment expressionAlignment= scribe.createAlignment(
|
Alignment expressionAlignment= scribe.createAlignment(
|
||||||
Alignment.DECLARATION_INITIALIZER,
|
Alignment.DECLARATION_INITIALIZER,
|
||||||
preferences.alignment_for_assignment,
|
preferences.alignment_for_assignment,
|
||||||
Alignment.R_OUTERMOST,
|
Alignment.R_INNERMOST,
|
||||||
1,
|
1,
|
||||||
scribe.scanner.getCurrentPosition());
|
scribe.scanner.getCurrentPosition());
|
||||||
|
|
||||||
|
@ -2851,6 +2899,15 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
|
||||||
final int line = scribe.line;
|
final int line = scribe.line;
|
||||||
scribe.printNextToken(Token.tLPAREN, preferences.insert_space_before_opening_paren_in_for);
|
scribe.printNextToken(Token.tLPAREN, preferences.insert_space_before_opening_paren_in_for);
|
||||||
fInsideFor= true;
|
fInsideFor= true;
|
||||||
|
if (preferences.insert_space_after_opening_paren_in_for) {
|
||||||
|
scribe.space();
|
||||||
|
}
|
||||||
|
IASTStatement initializerStmt= node.getInitializerStatement();
|
||||||
|
initializerStmt.accept(this);
|
||||||
|
if (peekNextToken() == Token.tSEMI) {
|
||||||
|
scribe.printNextToken(Token.tSEMI, preferences.insert_space_before_semicolon_in_for);
|
||||||
|
}
|
||||||
|
|
||||||
Alignment alignment = scribe.createAlignment(
|
Alignment alignment = scribe.createAlignment(
|
||||||
Alignment.FOR,
|
Alignment.FOR,
|
||||||
Alignment.M_COMPACT_SPLIT,
|
Alignment.M_COMPACT_SPLIT,
|
||||||
|
@ -2863,15 +2920,6 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
|
||||||
do {
|
do {
|
||||||
try {
|
try {
|
||||||
try {
|
try {
|
||||||
if (preferences.insert_space_after_opening_paren_in_for) {
|
|
||||||
scribe.space();
|
|
||||||
}
|
|
||||||
IASTStatement initializerStmt= node.getInitializerStatement();
|
|
||||||
initializerStmt.accept(this);
|
|
||||||
if (peekNextToken() == Token.tSEMI) {
|
|
||||||
scribe.printNextToken(Token.tSEMI, preferences.insert_space_before_semicolon_in_for);
|
|
||||||
}
|
|
||||||
|
|
||||||
scribe.alignFragment(alignment, 0);
|
scribe.alignFragment(alignment, 0);
|
||||||
final IASTExpression condition = node.getConditionExpression();
|
final IASTExpression condition = node.getConditionExpression();
|
||||||
if (condition != null) {
|
if (condition != null) {
|
||||||
|
|
|
@ -496,9 +496,9 @@ public class DefaultCodeFormatterOptions {
|
||||||
try {
|
try {
|
||||||
this.alignment_for_assignment = Integer.parseInt((String) alignmentForAssignmentOption);
|
this.alignment_for_assignment = Integer.parseInt((String) alignmentForAssignmentOption);
|
||||||
} catch (NumberFormatException e) {
|
} catch (NumberFormatException e) {
|
||||||
this.alignment_for_assignment = Alignment.M_ONE_PER_LINE_SPLIT;
|
this.alignment_for_assignment = Alignment.M_COMPACT_SPLIT;
|
||||||
} catch (ClassCastException e) {
|
} catch (ClassCastException e) {
|
||||||
this.alignment_for_assignment = Alignment.M_ONE_PER_LINE_SPLIT;
|
this.alignment_for_assignment = Alignment.M_COMPACT_SPLIT;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
final Object alignmentForBinaryExpressionOption = settings.get(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_BINARY_EXPRESSION);
|
final Object alignmentForBinaryExpressionOption = settings.get(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_BINARY_EXPRESSION);
|
||||||
|
@ -1468,7 +1468,7 @@ public class DefaultCodeFormatterOptions {
|
||||||
public void setDefaultSettings() {
|
public void setDefaultSettings() {
|
||||||
// this.alignment_for_arguments_in_allocation_expression = Alignment.M_COMPACT_SPLIT;
|
// this.alignment_for_arguments_in_allocation_expression = Alignment.M_COMPACT_SPLIT;
|
||||||
this.alignment_for_arguments_in_method_invocation = Alignment.M_COMPACT_SPLIT;
|
this.alignment_for_arguments_in_method_invocation = Alignment.M_COMPACT_SPLIT;
|
||||||
this.alignment_for_assignment = Alignment.M_NO_ALIGNMENT;
|
this.alignment_for_assignment = Alignment.M_COMPACT_SPLIT;
|
||||||
this.alignment_for_base_clause_in_type_declaration = Alignment.M_NEXT_PER_LINE_SPLIT;
|
this.alignment_for_base_clause_in_type_declaration = Alignment.M_NEXT_PER_LINE_SPLIT;
|
||||||
this.alignment_for_binary_expression = Alignment.M_COMPACT_SPLIT;
|
this.alignment_for_binary_expression = Alignment.M_COMPACT_SPLIT;
|
||||||
this.alignment_for_compact_if = Alignment.M_COMPACT_SPLIT;
|
this.alignment_for_compact_if = Alignment.M_COMPACT_SPLIT;
|
||||||
|
|
|
@ -242,23 +242,27 @@ public class Scribe {
|
||||||
return createAlignment(name, mode, Alignment.R_INNERMOST, count, sourceRestart);
|
return createAlignment(name, mode, Alignment.R_INNERMOST, count, sourceRestart);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Alignment createAlignment(String name, int mode, int count, int sourceRestart, boolean adjust) {
|
public Alignment createAlignment(String name, int mode, int count, int sourceRestart,
|
||||||
|
boolean adjust) {
|
||||||
return createAlignment(name, mode, Alignment.R_INNERMOST, count, sourceRestart, adjust);
|
return createAlignment(name, mode, Alignment.R_INNERMOST, count, sourceRestart, adjust);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Alignment createAlignment(String name, int mode, int tieBreakRule, int count, int sourceRestart) {
|
public Alignment createAlignment(String name, int mode, int tieBreakRule, int count,
|
||||||
|
int sourceRestart) {
|
||||||
return createAlignment(name, mode, tieBreakRule, count, sourceRestart,
|
return createAlignment(name, mode, tieBreakRule, count, sourceRestart,
|
||||||
preferences.continuation_indentation, false);
|
preferences.continuation_indentation, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Alignment createAlignment(String name, int mode, int count, int sourceRestart, int continuationIndent,
|
public Alignment createAlignment(String name, int mode, int count, int sourceRestart,
|
||||||
boolean adjust) {
|
int continuationIndent, boolean adjust) {
|
||||||
return createAlignment(name, mode, Alignment.R_INNERMOST, count, sourceRestart, continuationIndent, adjust);
|
return createAlignment(name, mode, Alignment.R_INNERMOST, count, sourceRestart,
|
||||||
|
continuationIndent, adjust);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Alignment createAlignment(String name, int mode, int tieBreakRule, int count, int sourceRestart,
|
public Alignment createAlignment(String name, int mode, int tieBreakRule, int count,
|
||||||
int continuationIndent, boolean adjust) {
|
int sourceRestart, int continuationIndent, boolean adjust) {
|
||||||
Alignment alignment= new Alignment(name, mode, tieBreakRule, this, count, sourceRestart, continuationIndent);
|
Alignment alignment= new Alignment(name, mode, tieBreakRule, this, count, sourceRestart,
|
||||||
|
continuationIndent);
|
||||||
// adjust break indentation
|
// adjust break indentation
|
||||||
if (adjust && memberAlignment != null) {
|
if (adjust && memberAlignment != null) {
|
||||||
Alignment current= memberAlignment;
|
Alignment current= memberAlignment;
|
||||||
|
@ -273,7 +277,8 @@ public class Scribe {
|
||||||
if ((mode & Alignment.M_INDENT_BY_ONE) != 0) {
|
if ((mode & Alignment.M_INDENT_BY_ONE) != 0) {
|
||||||
alignment.breakIndentationLevel= indentationLevel + indentSize;
|
alignment.breakIndentationLevel= indentationLevel + indentSize;
|
||||||
} else {
|
} else {
|
||||||
alignment.breakIndentationLevel= indentationLevel + continuationIndent * indentSize;
|
alignment.breakIndentationLevel= indentationLevel +
|
||||||
|
continuationIndent * indentSize;
|
||||||
}
|
}
|
||||||
alignment.update();
|
alignment.update();
|
||||||
break;
|
break;
|
||||||
|
@ -281,8 +286,8 @@ public class Scribe {
|
||||||
if ((mode & Alignment.M_INDENT_BY_ONE) != 0) {
|
if ((mode & Alignment.M_INDENT_BY_ONE) != 0) {
|
||||||
alignment.breakIndentationLevel= current.originalIndentationLevel + indentSize;
|
alignment.breakIndentationLevel= current.originalIndentationLevel + indentSize;
|
||||||
} else {
|
} else {
|
||||||
alignment.breakIndentationLevel= current.originalIndentationLevel + continuationIndent
|
alignment.breakIndentationLevel= current.originalIndentationLevel +
|
||||||
* indentSize;
|
continuationIndent * indentSize;
|
||||||
}
|
}
|
||||||
alignment.update();
|
alignment.update();
|
||||||
break;
|
break;
|
||||||
|
@ -301,16 +306,18 @@ public class Scribe {
|
||||||
if ((mode & Alignment.M_INDENT_BY_ONE) != 0) {
|
if ((mode & Alignment.M_INDENT_BY_ONE) != 0) {
|
||||||
alignment.breakIndentationLevel= indentationLevel + indentSize;
|
alignment.breakIndentationLevel= indentationLevel + indentSize;
|
||||||
} else {
|
} else {
|
||||||
alignment.breakIndentationLevel= indentationLevel + continuationIndent * indentSize;
|
alignment.breakIndentationLevel= indentationLevel +
|
||||||
|
continuationIndent * indentSize;
|
||||||
}
|
}
|
||||||
alignment.update();
|
alignment.update();
|
||||||
break;
|
break;
|
||||||
case Alignment.CHUNK_FIELD:
|
case Alignment.CHUNK_FIELD:
|
||||||
if ((mode & Alignment.M_INDENT_BY_ONE) != 0) {
|
if ((mode & Alignment.M_INDENT_BY_ONE) != 0) {
|
||||||
alignment.breakIndentationLevel= current.originalIndentationLevel + indentSize;
|
alignment.breakIndentationLevel= current.originalIndentationLevel +
|
||||||
|
indentSize;
|
||||||
} else {
|
} else {
|
||||||
alignment.breakIndentationLevel= current.originalIndentationLevel + continuationIndent
|
alignment.breakIndentationLevel= current.originalIndentationLevel +
|
||||||
* indentSize;
|
continuationIndent * indentSize;
|
||||||
}
|
}
|
||||||
alignment.update();
|
alignment.update();
|
||||||
break;
|
break;
|
||||||
|
@ -573,8 +580,8 @@ public class Scribe {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void handleLineTooLong() {
|
public void handleLineTooLong() {
|
||||||
// search for closest breakable alignment, using tiebreak rules
|
// Search for closest breakable alignment, using tie break rules
|
||||||
// look for outermost breakable one
|
// look for outermost breakable one.
|
||||||
int relativeDepth= 0;
|
int relativeDepth= 0;
|
||||||
int outerMostDepth= -1;
|
int outerMostDepth= -1;
|
||||||
Alignment targetAlignment= currentAlignment;
|
Alignment targetAlignment= currentAlignment;
|
||||||
|
@ -588,7 +595,7 @@ public class Scribe {
|
||||||
if (outerMostDepth >= 0) {
|
if (outerMostDepth >= 0) {
|
||||||
throwAlignmentException(AlignmentException.LINE_TOO_LONG, outerMostDepth);
|
throwAlignmentException(AlignmentException.LINE_TOO_LONG, outerMostDepth);
|
||||||
}
|
}
|
||||||
// look for innermost breakable one
|
// Look for innermost breakable one
|
||||||
relativeDepth= 0;
|
relativeDepth= 0;
|
||||||
targetAlignment= currentAlignment;
|
targetAlignment= currentAlignment;
|
||||||
while (targetAlignment != null) {
|
while (targetAlignment != null) {
|
||||||
|
@ -598,7 +605,7 @@ public class Scribe {
|
||||||
targetAlignment= targetAlignment.enclosing;
|
targetAlignment= targetAlignment.enclosing;
|
||||||
relativeDepth++;
|
relativeDepth++;
|
||||||
}
|
}
|
||||||
// did not find any breakable location - proceed
|
// Did not find any breakable location - proceed
|
||||||
}
|
}
|
||||||
|
|
||||||
private void throwAlignmentException(int kind, int relativeDepth) {
|
private void throwAlignmentException(int kind, int relativeDepth) {
|
||||||
|
@ -771,7 +778,8 @@ public class Scribe {
|
||||||
switch (currentToken.type) {
|
switch (currentToken.type) {
|
||||||
case Token.tLBRACE: {
|
case Token.tLBRACE: {
|
||||||
scanner.resetTo(scanner.getCurrentTokenStartPosition(), scannerEndPosition - 1);
|
scanner.resetTo(scanner.getCurrentTokenStartPosition(), scannerEndPosition - 1);
|
||||||
formatOpeningBrace(preferences.brace_position_for_block, preferences.insert_space_before_opening_brace_in_block);
|
formatOpeningBrace(preferences.brace_position_for_block,
|
||||||
|
preferences.insert_space_before_opening_brace_in_block);
|
||||||
if (preferences.indent_statements_compare_to_block) {
|
if (preferences.indent_statements_compare_to_block) {
|
||||||
indent();
|
indent();
|
||||||
}
|
}
|
||||||
|
@ -936,8 +944,9 @@ public class Scribe {
|
||||||
if (isNewLine) {
|
if (isNewLine) {
|
||||||
if (Character.isWhitespace((char) currentCharacter)) {
|
if (Character.isWhitespace((char) currentCharacter)) {
|
||||||
int previousStartPosition= scanner.getCurrentPosition();
|
int previousStartPosition= scanner.getCurrentPosition();
|
||||||
while (currentCharacter != -1 && currentCharacter != '\r' && currentCharacter != '\n'
|
while (currentCharacter != -1 && currentCharacter != '\r' &&
|
||||||
&& Character.isWhitespace((char) currentCharacter)) {
|
currentCharacter != '\n' &&
|
||||||
|
Character.isWhitespace((char) currentCharacter)) {
|
||||||
previousStart= nextCharacterStart;
|
previousStart= nextCharacterStart;
|
||||||
previousStartPosition= scanner.getCurrentPosition();
|
previousStartPosition= scanner.getCurrentPosition();
|
||||||
currentCharacter= scanner.getNextChar();
|
currentCharacter= scanner.getNextChar();
|
||||||
|
@ -984,7 +993,8 @@ public class Scribe {
|
||||||
pendingSpace= false;
|
pendingSpace= false;
|
||||||
int previousStart= currentTokenStartPosition;
|
int previousStart= currentTokenStartPosition;
|
||||||
|
|
||||||
while (nextCharacterStart <= currentTokenEndPosition && (currentCharacter= scanner.getNextChar()) != -1) {
|
while (nextCharacterStart <= currentTokenEndPosition &&
|
||||||
|
(currentCharacter= scanner.getNextChar()) != -1) {
|
||||||
nextCharacterStart= scanner.getCurrentPosition();
|
nextCharacterStart= scanner.getCurrentPosition();
|
||||||
|
|
||||||
switch (currentCharacter) {
|
switch (currentCharacter) {
|
||||||
|
@ -1050,8 +1060,10 @@ public class Scribe {
|
||||||
if (skipOverInactive) {
|
if (skipOverInactive) {
|
||||||
Position inactivePos= getInactivePosAt(scanner.getCurrentTokenStartPosition());
|
Position inactivePos= getInactivePosAt(scanner.getCurrentTokenStartPosition());
|
||||||
if (inactivePos != null) {
|
if (inactivePos != null) {
|
||||||
int startOffset= Math.min(scanner.getCurrentTokenStartPosition(), inactivePos.getOffset());
|
int startOffset= Math.min(scanner.getCurrentTokenStartPosition(),
|
||||||
int endOffset= Math.min(scannerEndPosition, inactivePos.getOffset() + inactivePos.getLength());
|
inactivePos.getOffset());
|
||||||
|
int endOffset= Math.min(scannerEndPosition,
|
||||||
|
inactivePos.getOffset() + inactivePos.getLength());
|
||||||
if (startOffset < endOffset) {
|
if (startOffset < endOffset) {
|
||||||
int savedIndentLevel= indentationLevel;
|
int savedIndentLevel= indentationLevel;
|
||||||
scanner.resetTo(scanner.getCurrentTokenStartPosition(), scanner.eofPosition - 1);
|
scanner.resetTo(scanner.getCurrentTokenStartPosition(), scanner.eofPosition - 1);
|
||||||
|
@ -1098,7 +1110,8 @@ public class Scribe {
|
||||||
// to change the trailing flag.
|
// to change the trailing flag.
|
||||||
if (trailing == BASIC_TRAILING_COMMENT && hasLineComment) {
|
if (trailing == BASIC_TRAILING_COMMENT && hasLineComment) {
|
||||||
int currentCommentIndentation = computeIndentation(whiteSpaces, 0);
|
int currentCommentIndentation = computeIndentation(whiteSpaces, 0);
|
||||||
int relativeIndentation = currentCommentIndentation - lastLineComment.currentIndentation;
|
int relativeIndentation =
|
||||||
|
currentCommentIndentation - lastLineComment.currentIndentation;
|
||||||
if (tabLength == 0) {
|
if (tabLength == 0) {
|
||||||
canChangeTrailing = relativeIndentation == 0;
|
canChangeTrailing = relativeIndentation == 0;
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -32,7 +32,8 @@ public class Alignment {
|
||||||
public static final String FOR = "for"; //$NON-NLS-1$
|
public static final String FOR = "for"; //$NON-NLS-1$
|
||||||
public static final String MACRO_ARGUMENTS = "macroArguments"; //$NON-NLS-1$
|
public static final String MACRO_ARGUMENTS = "macroArguments"; //$NON-NLS-1$
|
||||||
public static final String LIST_ELEMENTS_PREFIX = "listElements_"; //$NON-NLS-1$
|
public static final String LIST_ELEMENTS_PREFIX = "listElements_"; //$NON-NLS-1$
|
||||||
|
public static final String LIST_FALLBACK_TRAP = "listFallbackTrap"; //$NON-NLS-1$
|
||||||
|
|
||||||
/** The name of the alignment */
|
/** The name of the alignment */
|
||||||
public String name;
|
public String name;
|
||||||
|
|
||||||
|
@ -173,7 +174,7 @@ public class Alignment {
|
||||||
// indent broken fragments at next indentation level, based on current column
|
// indent broken fragments at next indentation level, based on current column
|
||||||
this.breakIndentationLevel = this.scribe.getNextIndentationLevel(currentColumn);
|
this.breakIndentationLevel = this.scribe.getNextIndentationLevel(currentColumn);
|
||||||
if (this.breakIndentationLevel == this.location.outputIndentationLevel) {
|
if (this.breakIndentationLevel == this.location.outputIndentationLevel) {
|
||||||
this.breakIndentationLevel += (continuationIndent * indentSize);
|
this.breakIndentationLevel += continuationIndent * indentSize;
|
||||||
}
|
}
|
||||||
} else if ((mode & M_INDENT_BY_ONE) != 0) {
|
} else if ((mode & M_INDENT_BY_ONE) != 0) {
|
||||||
// indent broken fragments exactly one level deeper than current indentation
|
// indent broken fragments exactly one level deeper than current indentation
|
||||||
|
@ -318,8 +319,7 @@ public class Alignment {
|
||||||
*/
|
*/
|
||||||
case M_NEXT_PER_LINE_SPLIT:
|
case M_NEXT_PER_LINE_SPLIT:
|
||||||
if (this.fragmentBreaks[0] == NONE) {
|
if (this.fragmentBreaks[0] == NONE) {
|
||||||
if (this.fragmentCount > 1
|
if (this.fragmentCount > 1 && this.fragmentBreaks[1] == NONE) {
|
||||||
&& this.fragmentBreaks[1] == NONE) {
|
|
||||||
if ((this.mode & M_INDENT_ON_COLUMN) != 0) {
|
if ((this.mode & M_INDENT_ON_COLUMN) != 0) {
|
||||||
this.fragmentIndentations[0] = this.breakIndentationLevel;
|
this.fragmentIndentations[0] = this.breakIndentationLevel;
|
||||||
}
|
}
|
||||||
|
|
|
@ -65,8 +65,8 @@ template<class Bar> void Foo::fum(int i) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// TEMPLATE_VARIABLES
|
// TEMPLATE_VARIABLES
|
||||||
template<bool threads, int inst> char
|
template<bool threads, int inst> char* default_alloc_template<threads, inst>::S_start_free =
|
||||||
* default_alloc_template<threads, inst>::S_start_free = 0;
|
0;
|
||||||
|
|
||||||
// an instantiation, not a template:
|
// an instantiation, not a template:
|
||||||
complex<float> cf(0, 0);
|
complex<float> cf(0, 0);
|
||||||
|
|
|
@ -227,8 +227,8 @@ public class CodeFormatterTest extends BaseUITestCase {
|
||||||
//
|
//
|
||||||
//void test() {
|
//void test() {
|
||||||
// ClassWithALongName* variable_with_a_long_name;
|
// ClassWithALongName* variable_with_a_long_name;
|
||||||
// for (ClassWithALongName::Iterator
|
// for (ClassWithALongName::Iterator iter_for_class_with_a_long_name =
|
||||||
// iter_for_class_with_a_long_name = variable_with_a_long_name->getIterator();
|
// variable_with_a_long_name->getIterator();
|
||||||
// !iter_for_class_with_a_long_name.isDone();
|
// !iter_for_class_with_a_long_name.isDone();
|
||||||
// iter_for_class_with_a_long_name.next()) {
|
// iter_for_class_with_a_long_name.next()) {
|
||||||
// }
|
// }
|
||||||
|
@ -627,9 +627,8 @@ public class CodeFormatterTest extends BaseUITestCase {
|
||||||
//
|
//
|
||||||
//void test() {
|
//void test() {
|
||||||
// ClassWithALongName* variable_with_a_long_name;
|
// ClassWithALongName* variable_with_a_long_name;
|
||||||
// ClassWithALongName* another_variable =
|
// ClassWithALongName* another_variable = variable_with_a_long_name
|
||||||
// variable_with_a_long_name->methodWithALongName()
|
// ->methodWithALongName()->anotherMethodWithALongName();
|
||||||
// ->anotherMethodWithALongName();
|
|
||||||
//}
|
//}
|
||||||
public void testMemberAccess() throws Exception {
|
public void testMemberAccess() throws Exception {
|
||||||
fOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, CCorePlugin.SPACE);
|
fOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, CCorePlugin.SPACE);
|
||||||
|
@ -969,8 +968,26 @@ public class CodeFormatterTest extends BaseUITestCase {
|
||||||
// int very_looong_parameter_name);
|
// int very_looong_parameter_name);
|
||||||
public void testFunctionDeclaration() throws Exception {
|
public void testFunctionDeclaration() throws Exception {
|
||||||
fOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, CCorePlugin.SPACE);
|
fOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, CCorePlugin.SPACE);
|
||||||
fOptions.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ARGUMENTS_IN_METHOD_INVOCATION,
|
fOptions.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_PARAMETERS_IN_METHOD_DECLARATION,
|
||||||
Integer.toString(Alignment.M_NEXT_PER_LINE_SPLIT | Alignment.M_INDENT_ON_COLUMN));
|
Integer.toString(Alignment.M_COMPACT_SPLIT | Alignment.M_INDENT_ON_COLUMN));
|
||||||
|
assertFormatterResult();
|
||||||
|
}
|
||||||
|
|
||||||
|
//const char* function_name1(const char* parameter_name, const char* another_parameter_name,
|
||||||
|
//int very_loooooooooooooooooooooooong_parameter_name);
|
||||||
|
//const char* function_name2(const char* parameter_name, const char* another_parameter_name,
|
||||||
|
//int very_looooooooooooooooooooooooong_parameter_name);
|
||||||
|
|
||||||
|
//const char* function_name1(const char* parameter_name,
|
||||||
|
// const char* another_parameter_name,
|
||||||
|
// int very_loooooooooooooooooooooooong_parameter_name);
|
||||||
|
//const char* function_name2(
|
||||||
|
// const char* parameter_name, const char* another_parameter_name,
|
||||||
|
// int very_looooooooooooooooooooooooong_parameter_name);
|
||||||
|
public void testFunctionDeclarationFallbackFormat() throws Exception {
|
||||||
|
fOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, CCorePlugin.SPACE);
|
||||||
|
fOptions.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_PARAMETERS_IN_METHOD_DECLARATION,
|
||||||
|
Integer.toString(Alignment.M_COMPACT_SPLIT | Alignment.M_INDENT_ON_COLUMN));
|
||||||
assertFormatterResult();
|
assertFormatterResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -984,8 +1001,8 @@ public class CodeFormatterTest extends BaseUITestCase {
|
||||||
//}
|
//}
|
||||||
public void testFunctionDefinition() throws Exception {
|
public void testFunctionDefinition() throws Exception {
|
||||||
fOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, CCorePlugin.SPACE);
|
fOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, CCorePlugin.SPACE);
|
||||||
fOptions.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ARGUMENTS_IN_METHOD_INVOCATION,
|
fOptions.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_PARAMETERS_IN_METHOD_DECLARATION,
|
||||||
Integer.toString(Alignment.M_NEXT_PER_LINE_SPLIT | Alignment.M_INDENT_ON_COLUMN));
|
Integer.toString(Alignment.M_COMPACT_SPLIT | Alignment.M_INDENT_ON_COLUMN));
|
||||||
assertFormatterResult();
|
assertFormatterResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue