From 290cb246b6d708e1634e3d07f0f929768ae2d9b1 Mon Sep 17 00:00:00 2001 From: Marco Stornelli Date: Tue, 9 Apr 2019 19:01:21 +0200 Subject: [PATCH] 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 --- .../DefaultCodeFormatterConstants.java | 12 ++++ .../DefaultCodeFormatterOptions.java | 10 +++ .../formatter/CodeFormatterVisitor.java | 13 ++-- .../cdt/ui/tests/text/CodeFormatterTest.java | 62 +++++++++++++++++++ .../formatter/FormatterMessages.java | 1 + .../formatter/FormatterMessages.properties | 1 + .../formatter/NewLinesTabPage.java | 4 ++ 7 files changed, 99 insertions(+), 4 deletions(-) diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/formatter/DefaultCodeFormatterConstants.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/formatter/DefaultCodeFormatterConstants.java index 3dc6712fd06..8f34e1811c7 100644 --- a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/formatter/DefaultCodeFormatterConstants.java +++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/formatter/DefaultCodeFormatterConstants.java @@ -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$ + /** + *
+	 * 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
+	 * 
+ * @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$ /** *
 	 * FORMATTER / Option to insert a new line before the else keyword in if statement
diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/formatter/DefaultCodeFormatterOptions.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/formatter/DefaultCodeFormatterOptions.java
index cb7b71516c3..8a58c083d98 100644
--- a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/formatter/DefaultCodeFormatterOptions.java
+++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/formatter/DefaultCodeFormatterOptions.java
@@ -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) {
diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java
index 78dca997d49..307faf27359 100644
--- a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java
+++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java
@@ -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()) {
diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java
index 31745c52ec3..bd8d67485c5 100644
--- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java
+++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java
@@ -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)
diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/formatter/FormatterMessages.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/formatter/FormatterMessages.java
index 4fd40cc12b2..b32d4935651 100644
--- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/formatter/FormatterMessages.java
+++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/formatter/FormatterMessages.java
@@ -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;
diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/formatter/FormatterMessages.properties b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/formatter/FormatterMessages.properties
index 4887fb0a476..89adf6b4fdb 100644
--- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/formatter/FormatterMessages.properties
+++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/formatter/FormatterMessages.properties
@@ -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
diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/formatter/NewLinesTabPage.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/formatter/NewLinesTabPage.java
index 07e139c6988..0628080e6ff 100644
--- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/formatter/NewLinesTabPage.java
+++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/formatter/NewLinesTabPage.java
@@ -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,