1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-24 01:15:29 +02:00

Bug 449394 - Fix constructor init list formatting

Init list was always on new lines regardless user input about new line
policy or wrapping policy.

Change-Id: I1ac85611129c23e89d9b299ca87ced314ad542f3
Signed-off-by: Marco Stornelli <marco.stornelli@gmail.com>
This commit is contained in:
Marco Stornelli 2019-04-09 19:01:21 +02:00 committed by Jonah Graham
parent 5131b86d9a
commit 290cb246b6
7 changed files with 99 additions and 4 deletions

View file

@ -932,6 +932,18 @@ public class DefaultCodeFormatterConstants {
*/
public static final String FORMATTER_INSERT_NEW_LINE_BEFORE_COLON_IN_CONSTRUCTOR_INITIALIZER_LIST = CCorePlugin.PLUGIN_ID
+ ".formatter.insert_new_line_before_colon_in_constructor_initializer_list"; //$NON-NLS-1$
/**
* <pre>
* FORMATTER / Option to insert a new line after colon in constructor initializer list.
* - option id: "org.eclipse.cdt.core.formatter.formatter.insert_new_line_after_colon_in_constructor_initializer_list"
* - possible values: { DO_NOT_INSERT, INSERT }
* - default: DO_NOT_INSERT
* </pre>
* @see CCorePlugin#INSERT
* @see CCorePlugin#DO_NOT_INSERT
*/
public static final String FORMATTER_INSERT_NEW_LINE_AFTER_COLON_IN_CONSTRUCTOR_INITIALIZER_LIST = CCorePlugin.PLUGIN_ID
+ ".formatter.insert_new_line_after_colon_in_constructor_initializer_list"; //$NON-NLS-1$
/**
* <pre>
* FORMATTER / Option to insert a new line before the else keyword in if statement

View file

@ -143,6 +143,7 @@ public class DefaultCodeFormatterOptions {
public boolean insert_new_line_before_catch_in_try_statement;
public boolean insert_new_line_before_closing_brace_in_initializer_list;
public boolean insert_new_line_before_colon_in_constructor_initializer_list;
public boolean insert_new_line_after_colon_in_constructor_initializer_list;
public boolean insert_new_line_before_else_in_if_statement;
public boolean insert_new_line_before_while_in_do_statement;
public boolean insert_new_line_before_identifier_in_function_declaration;
@ -436,6 +437,9 @@ public class DefaultCodeFormatterOptions {
DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_BEFORE_COLON_IN_CONSTRUCTOR_INITIALIZER_LIST,
this.insert_new_line_before_colon_in_constructor_initializer_list ? CCorePlugin.INSERT
: CCorePlugin.DO_NOT_INSERT);
options.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_AFTER_COLON_IN_CONSTRUCTOR_INITIALIZER_LIST,
this.insert_new_line_after_colon_in_constructor_initializer_list ? CCorePlugin.INSERT
: CCorePlugin.INSERT);
options.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_BEFORE_ELSE_IN_IF_STATEMENT,
this.insert_new_line_before_else_in_if_statement ? CCorePlugin.INSERT : CCorePlugin.DO_NOT_INSERT);
options.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_BEFORE_WHILE_IN_DO_STATEMENT,
@ -1322,6 +1326,12 @@ public class DefaultCodeFormatterOptions {
this.insert_new_line_before_closing_brace_in_initializer_list = CCorePlugin.INSERT
.equals(insertNewLineBeforeClosingBraceInInitializerListOption);
}
final Object insertNewLineAfterColonInConstructorInitializerListOption = settings.get(
DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_AFTER_COLON_IN_CONSTRUCTOR_INITIALIZER_LIST);
if (insertNewLineAfterColonInConstructorInitializerListOption != null) {
this.insert_new_line_after_colon_in_constructor_initializer_list = CCorePlugin.INSERT
.equals(insertNewLineAfterColonInConstructorInitializerListOption);
}
final Object insertNewLineBeforeColonInConstructorInitializerListOption = settings.get(
DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_BEFORE_COLON_IN_CONSTRUCTOR_INITIALIZER_LIST);
if (insertNewLineBeforeColonInConstructorInitializerListOption != null) {

View file

@ -1399,17 +1399,22 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
}
scribe.printNextToken(Token.tCOLON,
!preferences.insert_new_line_before_colon_in_constructor_initializer_list);
if (preferences.insert_new_line_before_colon_in_constructor_initializer_list) {
if (!preferences.insert_new_line_after_colon_in_constructor_initializer_list) {
scribe.space();
} else {
}
if (preferences.insert_new_line_after_colon_in_constructor_initializer_list) {
scribe.printTrailingComment();
scribe.startNewLine();
scribe.indentForContinuation();
if (!preferences.insert_new_line_before_colon_in_constructor_initializer_list)
scribe.indentForContinuation();
}
final ListOptions options = new ListOptions(preferences.alignment_for_constructor_initializer_list);
options.fTieBreakRule = Alignment.R_OUTERMOST;
formatList(Arrays.asList(constructorChain), options, false, false, null);
scribe.unIndentForContinuation();
if (preferences.insert_new_line_after_colon_in_constructor_initializer_list
|| preferences.insert_new_line_before_colon_in_constructor_initializer_list) {
scribe.unIndentForContinuation();
}
}
if (cppFunctionDefinition.isDefaulted() || cppFunctionDefinition.isDeleted()) {

View file

@ -1769,11 +1769,73 @@ public class CodeFormatterTest extends BaseUITestCase {
fOptions.put(
DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_BEFORE_COLON_IN_CONSTRUCTOR_INITIALIZER_LIST,
CCorePlugin.INSERT);
fOptions.put(
DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_AFTER_COLON_IN_CONSTRUCTOR_INITIALIZER_LIST,
CCorePlugin.DO_NOT_INSERT);
fOptions.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_CONSTRUCTOR_INITIALIZER_LIST,
Integer.toString(Alignment.M_NEXT_PER_LINE_SPLIT | Alignment.M_INDENT_ON_COLUMN | Alignment.M_FORCE));
assertFormatterResult();
}
//class Point {
//public:
//Point(int x, int y) : x(x), y(y) {}
//
//private:
//int x;
//int y;
//};
//class Point {
//public:
// Point(int x, int y) : x(x), y(y) {
// }
//
//private:
// int x;
// int y;
//};
public void testConstructorInitializer_3() throws Exception {
fOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, CCorePlugin.SPACE);
fOptions.put(
DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_BEFORE_COLON_IN_CONSTRUCTOR_INITIALIZER_LIST,
CCorePlugin.DO_NOT_INSERT);
fOptions.put(
DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_AFTER_COLON_IN_CONSTRUCTOR_INITIALIZER_LIST,
CCorePlugin.DO_NOT_INSERT);
assertFormatterResult();
}
//class Point {
//public:
//Point(int x, int y) : x(x), y(y) {}
//
//private:
//int x;
//int y;
//};
//class Point {
//public:
// Point(int x, int y) :
// x(x), y(y) {
// }
//
//private:
// int x;
// int y;
//};
public void testConstructorInitializer_4() throws Exception {
fOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, CCorePlugin.SPACE);
fOptions.put(
DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_BEFORE_COLON_IN_CONSTRUCTOR_INITIALIZER_LIST,
CCorePlugin.DO_NOT_INSERT);
fOptions.put(
DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_AFTER_COLON_IN_CONSTRUCTOR_INITIALIZER_LIST,
CCorePlugin.INSERT);
assertFormatterResult();
}
//#define A (0)
//#define B (1)
//#define ARGS (A, B)

View file

@ -391,6 +391,7 @@ final class FormatterMessages extends NLS {
public static String NewLinesTabPage_preview_header;
public static String NewLinesTabPage_newlines_group_title;
public static String NewLinesTabPage_newlines_group_option_before_colon_in_constructor_initializer_list;
public static String NewLinesTabPage_newlines_group_option_after_colon_in_constructor_initializer_list;
public static String NewLinesTabPage_newlines_emtpy_statement_on_new_line;
// public static String NewLinesTabPage_newlines_group_option_empty_class_body;
// public static String NewLinesTabPage_newlines_group_option_empty_method_body;

View file

@ -461,6 +461,7 @@ NewLinesTabPage_preview_header=New lines
NewLinesTabPage_newlines_group_title=Insert new line
NewLinesTabPage_newlines_group_option_before_colon_in_constructor_initializer_list=before colon in constructor initializer list
NewLinesTabPage_newlines_group_option_after_colon_in_constructor_initializer_list=after colon in constructor initializer list
NewLinesTabPage_newlines_emtpy_statement_on_new_line=before empty statement
#NewLinesTabPage_newlines_group_option_empty_class_body=in empty &class body
#NewLinesTabPage_newlines_group_option_empty_method_body=in empt&y method body

View file

@ -45,6 +45,10 @@ public class NewLinesTabPage extends FormatterTabPage {
protected void doCreatePreferences(Composite composite, int numColumns) {
final Group newlinesGroup = createGroup(numColumns, composite,
FormatterMessages.NewLinesTabPage_newlines_group_title);
createPref(newlinesGroup, numColumns,
FormatterMessages.NewLinesTabPage_newlines_group_option_after_colon_in_constructor_initializer_list,
DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_AFTER_COLON_IN_CONSTRUCTOR_INITIALIZER_LIST,
DO_NOT_INSERT_INSERT);
createPref(newlinesGroup, numColumns,
FormatterMessages.NewLinesTabPage_newlines_group_option_before_colon_in_constructor_initializer_list,
DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_BEFORE_COLON_IN_CONSTRUCTOR_INITIALIZER_LIST,