1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

Bug 280990 - Code formatter refuses to wrap member access expressions

This commit is contained in:
Sergey Prigogin 2011-01-30 21:29:56 +00:00
parent 9465ae8064
commit d387df2ecd
8 changed files with 147 additions and 51 deletions

View file

@ -151,16 +151,6 @@ public class DefaultCodeFormatterConstants {
* @see #createAlignmentValue(boolean, int, int)
*/
public static final String FORMATTER_ALIGNMENT_FOR_CONDITIONAL_EXPRESSION = CCorePlugin.PLUGIN_ID + ".formatter.alignment_for_conditional_expression"; //$NON-NLS-1$
/**
* <pre>
* FORMATTER / Option for alignment of expressions in initializer list
* - option id: "org.eclipse.cdt.core.formatter.alignment_for_expressions_in_array_initializer"
* - possible values: values returned by <code>createAlignmentValue(boolean, int, int)</code> call
* - default: createAlignmentValue(false, WRAP_COMPACT, INDENT_DEFAULT)
* </pre>
* @see #createAlignmentValue(boolean, int, int)
*/
public static final String FORMATTER_ALIGNMENT_FOR_EXPRESSIONS_IN_INITIALIZER_LIST = CCorePlugin.PLUGIN_ID + ".formatter.alignment_for_expressions_in_array_initializer"; //$NON-NLS-1$
/**
* <pre>
* FORMATTER / Option for alignment of a declarator list
@ -191,6 +181,27 @@ public class DefaultCodeFormatterConstants {
* @see #createAlignmentValue(boolean, int, int)
*/
public static final String FORMATTER_ALIGNMENT_FOR_EXPRESSION_LIST = CCorePlugin.PLUGIN_ID + ".formatter.alignment_for_expression_list"; //$NON-NLS-1$
/**
* <pre>
* FORMATTER / Option for alignment of expressions in initializer list
* - option id: "org.eclipse.cdt.core.formatter.alignment_for_expressions_in_array_initializer"
* - possible values: values returned by <code>createAlignmentValue(boolean, int, int)</code> call
* - default: createAlignmentValue(false, WRAP_COMPACT, INDENT_DEFAULT)
* </pre>
* @see #createAlignmentValue(boolean, int, int)
*/
public static final String FORMATTER_ALIGNMENT_FOR_EXPRESSIONS_IN_INITIALIZER_LIST = CCorePlugin.PLUGIN_ID + ".formatter.alignment_for_expressions_in_array_initializer"; //$NON-NLS-1$
/**
* <pre>
* FORMATTER / Option for alignment of member access
* - option id: "org.eclipse.cdt.core.formatter.alignment_for_member_access"
* - possible values: values returned by <code>createAlignmentValue(boolean, int, int)</code> call
* - default: createAlignmentValue(false, M_NO_ALIGNMENT, INDENT_DEFAULT)
* </pre>
* @see #createAlignmentValue(boolean, int, int)
* @since 5.3
*/
public static final String FORMATTER_ALIGNMENT_FOR_MEMBER_ACCESS = CCorePlugin.PLUGIN_ID + ".formatter.alignment_for_member_access"; //$NON-NLS-1$
/**
* <pre>
* FORMATTER / Option for alignment of parameters in method declaration

View file

@ -1984,6 +1984,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
Alignment expressionAlignment= scribe.createAlignment(
"declarationInitializer", //$NON-NLS-1$
preferences.alignment_for_assignment,
Alignment.R_OUTERMOST,
1,
scribe.scanner.getCurrentPosition());
@ -2234,6 +2235,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
Alignment expressionAlignment= scribe.createAlignment(
"assignmentExpression", //$NON-NLS-1$
preferences.alignment_for_assignment,
Alignment.R_OUTERMOST,
1,
scribe.scanner.getCurrentPosition());
@ -2314,18 +2316,36 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
}
final IASTName fieldName= node.getFieldName();
if (fieldName != null) {
final int operatorToken= node.isPointerDereference() ? Token.tARROW : Token.tDOT;
scribe.printNextToken(operatorToken, scribe.printComment());
if (scribe.printComment()) {
scribe.space();
}
if (node instanceof ICPPASTFieldReference) {
if (((ICPPASTFieldReference) node).isTemplate()) {
scribe.printNextToken(Token.t_template);
scribe.space();
}
}
fieldName.accept(this);
Alignment alignment= scribe.createAlignment(
"fieldReference", //$NON-NLS-1$
preferences.alignment_for_member_access,
Alignment.R_OUTERMOST,
1,
scribe.scanner.getCurrentPosition());
scribe.enterAlignment(alignment);
boolean ok = false;
do {
try {
scribe.alignFragment(alignment, 0);
final int operatorToken= node.isPointerDereference() ? Token.tARROW : Token.tDOT;
scribe.printComment();
scribe.printNextToken(operatorToken, false);
scribe.printComment();
if (node instanceof ICPPASTFieldReference) {
if (((ICPPASTFieldReference) node).isTemplate()) {
scribe.printNextToken(Token.t_template);
scribe.space();
}
}
fieldName.accept(this);
ok = true;
} catch (AlignmentException e) {
scribe.redoAlignment(e);
}
} while (!ok);
scribe.exitAlignment(alignment, true);
}
return PROCESS_SKIP;
}
@ -2524,9 +2544,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
Alignment.M_COMPACT_SPLIT,
Alignment.R_OUTERMOST,
2,
scribe.scanner.getCurrentPosition(),
preferences.continuation_indentation,
false);
scribe.scanner.getCurrentPosition());
scribe.enterAlignment(alignment);
boolean ok = false;

View file

@ -24,7 +24,7 @@ public class DefaultCodeFormatterOptions {
public static final int TAB = 1;
public static final int SPACE = 2;
public static final int MIXED = 4;
public static DefaultCodeFormatterOptions getDefaultSettings() {
DefaultCodeFormatterOptions options = new DefaultCodeFormatterOptions();
options.setDefaultSettings();
@ -57,27 +57,28 @@ public class DefaultCodeFormatterOptions {
public int alignment_for_arguments_in_method_invocation;
public int alignment_for_assignment;
public int alignment_for_base_clause_in_type_declaration;
public int alignment_for_binary_expression;
public int alignment_for_compact_if;
public int alignment_for_conditional_expression;
public int alignment_for_expressions_in_initializer_list;
public int alignment_for_declarator_list;
public int alignment_for_enumerator_list;
public int alignment_for_expression_list;
public int alignment_for_expressions_in_initializer_list;
public int alignment_for_member_access;
public int alignment_for_parameters_in_method_declaration;
public int alignment_for_base_clause_in_type_declaration;
public int alignment_for_throws_clause_in_method_declaration;
// public boolean align_type_members_on_columns;
public String brace_position_for_initializer_list;
public String brace_position_for_block;
public String brace_position_for_block_in_case;
// public String brace_position_for_enum_declaration;
public String brace_position_for_initializer_list;
public String brace_position_for_method_declaration;
public String brace_position_for_type_declaration;
public String brace_position_for_namespace_declaration;
public String brace_position_for_switch;
public String brace_position_for_type_declaration;
public int continuation_indentation;
public int continuation_indentation_for_initializer_list;
@ -269,10 +270,11 @@ public class DefaultCodeFormatterOptions {
options.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_BINARY_EXPRESSION, getAlignment(this.alignment_for_binary_expression));
options.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_COMPACT_IF, getAlignment(this.alignment_for_compact_if));
options.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_CONDITIONAL_EXPRESSION, getAlignment(this.alignment_for_conditional_expression));
options.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_EXPRESSIONS_IN_INITIALIZER_LIST, getAlignment(this.alignment_for_expressions_in_initializer_list));
options.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_DECLARATOR_LIST, getAlignment(this.alignment_for_declarator_list));
options.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ENUMERATOR_LIST, getAlignment(this.alignment_for_enumerator_list));
options.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_EXPRESSIONS_IN_INITIALIZER_LIST, getAlignment(this.alignment_for_expressions_in_initializer_list));
options.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_EXPRESSION_LIST, getAlignment(this.alignment_for_expression_list));
options.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_MEMBER_ACCESS, getAlignment(this.alignment_for_member_access));
options.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_PARAMETERS_IN_METHOD_DECLARATION, getAlignment(this.alignment_for_parameters_in_method_declaration));
// options.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_SELECTOR_IN_METHOD_INVOCATION, getAlignment(this.alignment_for_selector_in_method_invocation));
options.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_BASE_CLAUSE_IN_TYPE_DECLARATION, getAlignment(this.alignment_for_base_clause_in_type_declaration));
@ -521,16 +523,6 @@ public class DefaultCodeFormatterOptions {
this.alignment_for_conditional_expression = Alignment.M_ONE_PER_LINE_SPLIT;
}
}
final Object alignmentForExpressionsInInitializerListOption = settings.get(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_EXPRESSIONS_IN_INITIALIZER_LIST);
if (alignmentForExpressionsInInitializerListOption != null) {
try {
this.alignment_for_expressions_in_initializer_list = Integer.parseInt((String) alignmentForExpressionsInInitializerListOption);
} catch (NumberFormatException e) {
this.alignment_for_expressions_in_initializer_list = Alignment.M_COMPACT_SPLIT;
} catch (ClassCastException e) {
this.alignment_for_expressions_in_initializer_list = Alignment.M_COMPACT_SPLIT;
}
}
final Object alignmentForDeclaratorListOption = settings.get(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_DECLARATOR_LIST);
if (alignmentForDeclaratorListOption != null) {
try {
@ -551,6 +543,16 @@ public class DefaultCodeFormatterOptions {
this.alignment_for_enumerator_list = Alignment.M_ONE_PER_LINE_SPLIT;
}
}
final Object alignmentForExpressionsInInitializerListOption = settings.get(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_EXPRESSIONS_IN_INITIALIZER_LIST);
if (alignmentForExpressionsInInitializerListOption != null) {
try {
this.alignment_for_expressions_in_initializer_list = Integer.parseInt((String) alignmentForExpressionsInInitializerListOption);
} catch (NumberFormatException e) {
this.alignment_for_expressions_in_initializer_list = Alignment.M_COMPACT_SPLIT;
} catch (ClassCastException e) {
this.alignment_for_expressions_in_initializer_list = Alignment.M_COMPACT_SPLIT;
}
}
final Object alignmentForExpressionListOption = settings.get(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_EXPRESSION_LIST);
if (alignmentForExpressionListOption != null) {
try {
@ -561,6 +563,16 @@ public class DefaultCodeFormatterOptions {
this.alignment_for_expression_list = Alignment.M_COMPACT_SPLIT;
}
}
final Object alignmentForMemberAccessOption = settings.get(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_MEMBER_ACCESS);
if (alignmentForMemberAccessOption != null) {
try {
this.alignment_for_member_access = Integer.parseInt((String) alignmentForMemberAccessOption);
} catch (NumberFormatException e) {
this.alignment_for_member_access = Alignment.M_ONE_PER_LINE_SPLIT;
} catch (ClassCastException e) {
this.alignment_for_member_access = Alignment.M_ONE_PER_LINE_SPLIT;
}
}
final Object alignmentForParametersInMethodDeclarationOption = settings.get(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_PARAMETERS_IN_METHOD_DECLARATION);
if (alignmentForParametersInMethodDeclarationOption != null) {
try {
@ -1421,25 +1433,26 @@ public class DefaultCodeFormatterOptions {
// 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_assignment = Alignment.M_NO_ALIGNMENT;
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_compact_if = Alignment.M_COMPACT_SPLIT;
this.alignment_for_conditional_expression = Alignment.M_NEXT_PER_LINE_SPLIT;
this.alignment_for_expressions_in_initializer_list = Alignment.M_COMPACT_SPLIT;
this.alignment_for_declarator_list = Alignment.M_COMPACT_SPLIT;
this.alignment_for_enumerator_list = Alignment.M_ONE_PER_LINE_SPLIT;
this.alignment_for_expressions_in_initializer_list = Alignment.M_COMPACT_SPLIT;
this.alignment_for_member_access = Alignment.M_NO_ALIGNMENT;
this.alignment_for_parameters_in_method_declaration = Alignment.M_COMPACT_SPLIT;
// this.alignment_for_selector_in_method_invocation = Alignment.M_COMPACT_SPLIT;
this.alignment_for_base_clause_in_type_declaration = Alignment.M_NEXT_PER_LINE_SPLIT;
this.alignment_for_throws_clause_in_method_declaration = Alignment.M_COMPACT_SPLIT;
// this.align_type_members_on_columns = false;
this.brace_position_for_initializer_list = DefaultCodeFormatterConstants.END_OF_LINE;
this.brace_position_for_block = DefaultCodeFormatterConstants.END_OF_LINE;
this.brace_position_for_block_in_case = DefaultCodeFormatterConstants.END_OF_LINE;
// this.brace_position_for_enum_declaration = DefaultCodeFormatterConstants.END_OF_LINE;
this.brace_position_for_initializer_list = DefaultCodeFormatterConstants.END_OF_LINE;
this.brace_position_for_method_declaration = DefaultCodeFormatterConstants.END_OF_LINE;
this.brace_position_for_type_declaration = DefaultCodeFormatterConstants.END_OF_LINE;
this.brace_position_for_namespace_declaration = DefaultCodeFormatterConstants.END_OF_LINE;
this.brace_position_for_switch = DefaultCodeFormatterConstants.END_OF_LINE;
this.brace_position_for_type_declaration = DefaultCodeFormatterConstants.END_OF_LINE;
// this.comment_clear_blank_lines = false;
// this.comment_format = true;
// this.comment_format_header = false;

View file

@ -917,6 +917,11 @@ public class Scribe {
}
}
/**
* Prints comment at the current position.
*
* @return {@code true} if a writespace character was encountered preceding the next token,
*/
public boolean printComment() {
// if we have a space between two tokens we ensure it will be dumped in
// the formatted string

View file

@ -608,6 +608,38 @@ public class CodeFormatterTest extends BaseUITestCase {
assertFormatterResult();
}
//class ClassWithALongName {
//public:
//ClassWithALongName* methodWithALongName();
//ClassWithALongName* anotherMethodWithALongName();
//};
//
//void test() {
//ClassWithALongName* variable_with_a_long_name;
//ClassWithALongName* another_variable = variable_with_a_long_name->methodWithALongName()->anotherMethodWithALongName();
//}
//class ClassWithALongName {
//public:
// ClassWithALongName* methodWithALongName();
// ClassWithALongName* anotherMethodWithALongName();
//};
//
//void test() {
// ClassWithALongName* variable_with_a_long_name;
// ClassWithALongName* another_variable =
// variable_with_a_long_name->methodWithALongName()
// ->anotherMethodWithALongName();
//}
public void testMemberAccess() throws Exception {
fOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, CCorePlugin.SPACE);
fOptions.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ASSIGNMENT,
Integer.toString(Alignment.M_COMPACT_SPLIT));
fOptions.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_MEMBER_ACCESS,
Integer.toString(Alignment.M_COMPACT_SPLIT));
assertFormatterResult();
}
//int foo(){try{}catch(...){}}
//float* bar();
//template<typename _CharT, typename _Traits>class basic_ios : public ios_base{public:

View file

@ -206,6 +206,7 @@ final class FormatterMessages extends NLS {
public static String LineWrappingTabPage_initializer_list;
public static String LineWrappingTabPage_conditionals;
public static String LineWrappingTabPage_binary_exprs;
public static String LineWrappingTabPage_member_access;
public static String LineWrappingTabPage_indentation_default;
public static String LineWrappingTabPage_indentation_on_column;
public static String LineWrappingTabPage_indentation_by_one;
@ -226,6 +227,7 @@ final class FormatterMessages extends NLS {
public static String LineWrappingTabPage_initializer_list_lowercase;
public static String LineWrappingTabPage_conditionals_lowercase;
public static String LineWrappingTabPage_binary_exprs_lowercase;
public static String LineWrappingTabPage_member_access_lowercase;
public static String LineWrappingTabPage_indentation_default_lowercase;
public static String LineWrappingTabPage_indentation_on_column_lowercase;
public static String LineWrappingTabPage_indentation_by_one_lowercase;

View file

@ -221,7 +221,6 @@ WhiteSpaceOptions_after_question_mark=After question mark
WhiteSpaceTabPage_insert_space=&Insert space:
LineWrappingTabPage_base_clause=Base-clause
#LineWrappingTabPage_compact_if_else=Compact 'if else'
LineWrappingTabPage_parameters=Parameters
@ -233,6 +232,7 @@ LineWrappingTabPage_enumerator_list=Enumerator list
LineWrappingTabPage_initializer_list=Initializer list
LineWrappingTabPage_conditionals=Conditionals
LineWrappingTabPage_binary_exprs=Binary expressions
LineWrappingTabPage_member_access=Member access
LineWrappingTabPage_indentation_default=Default indentation
LineWrappingTabPage_indentation_on_column=Indent on column
LineWrappingTabPage_indentation_by_one=Indent by one
@ -255,6 +255,7 @@ LineWrappingTabPage_enumerator_list_lowercase=enumerator list
LineWrappingTabPage_initializer_list_lowercase=initializer list
LineWrappingTabPage_conditionals_lowercase=conditionals
LineWrappingTabPage_binary_exprs_lowercase=binary expressions
LineWrappingTabPage_member_access_lowercase=member access
LineWrappingTabPage_indentation_default_lowercase=default indentation
LineWrappingTabPage_indentation_on_column_lowercase=indent on column
LineWrappingTabPage_indentation_by_one_lowercase=indent by one

View file

@ -49,7 +49,6 @@ import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.internal.corext.util.Messages;
/**
* The line wrapping tab page.
*/
@ -64,7 +63,7 @@ public class LineWrappingTabPage extends FormatterTabPage {
public final String previewText;
public final String description; //bug 235453: for categories' labels
public final List<Category> children;
public int index;
public Category(String _key, String _previewText, String _name, String _description) {
@ -455,6 +454,20 @@ public class LineWrappingTabPage extends FormatterTabPage {
FormatterMessages.LineWrappingTabPage_binary_exprs_lowercase
);
private final Category fMemberAccessExpressionCategory= new Category(
DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_MEMBER_ACCESS,
"class TreeNode {" + //$NON-NLS-1$
"public:" + //$NON-NLS-1$
"TreeNode* getParent();" + //$NON-NLS-1$
"TreeNode* getFirstChild();" + //$NON-NLS-1$
"};\n\n" + //$NON-NLS-1$
"TreeNode* firstUncle(TreeNode& node) {" + //$NON-NLS-1$
"return node.getParent()->getParent()->getFirstChild();" + //$NON-NLS-1$
"}", //$NON-NLS-1$
FormatterMessages.LineWrappingTabPage_member_access,
FormatterMessages.LineWrappingTabPage_member_access_lowercase
);
// private final Category fEnumConstArgumentsCategory= new Category(
// DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ARGUMENTS_IN_ENUM_CONSTANT,
// "enum Example {" + //$NON-NLS-1$
@ -481,12 +494,12 @@ public class LineWrappingTabPage extends FormatterTabPage {
private final Category fAssignmentCategory= new Category(
DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ASSIGNMENT,
"static char* string = \"TextTextText\";" + //$NON-NLS-1$
"static char* string = \"text text text text\";" + //$NON-NLS-1$
"class Example {" + //$NON-NLS-1$
"void foo() {" + //$NON-NLS-1$
"for (int i = 0; i < 10; i++) {}" + //$NON-NLS-1$
"char* s;" + //$NON-NLS-1$
"s = \"TextTextText\";}}", //$NON-NLS-1$
"const char* character_string;" + //$NON-NLS-1$
"character_string = \"text text text text\";}}", //$NON-NLS-1$
FormatterMessages.LineWrappingTabPage_assignment_alignment,
FormatterMessages.LineWrappingTabPage_assignment_alignment_lowercase
);
@ -600,6 +613,7 @@ public class LineWrappingTabPage extends FormatterTabPage {
expressions.children.add(fConditionalExpressionCategory);
expressions.children.add(fInitializerListExpressionsCategory);
expressions.children.add(fAssignmentCategory);
expressions.children.add(fMemberAccessExpressionCategory);
// final Category statements= new Category(FormatterMessages.LineWrappingTabPage_statements);
// statements.children.add(fCompactIfCategory);