Skip to main content
summaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorMarco Stornelli2019-04-09 13:01:21 -0400
committerJonah Graham2019-04-09 16:21:20 -0400
commit290cb246b6d708e1634e3d07f0f929768ae2d9b1 (patch)
tree69df3f81bb6f24e1a736fb9f3f71969cd3bd1497 /core
parent5131b86d9a1d56141ed7d884a656335fb60b271d (diff)
downloadorg.eclipse.cdt-290cb246b6d708e1634e3d07f0f929768ae2d9b1.tar.gz
org.eclipse.cdt-290cb246b6d708e1634e3d07f0f929768ae2d9b1.tar.xz
org.eclipse.cdt-290cb246b6d708e1634e3d07f0f929768ae2d9b1.zip
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>
Diffstat (limited to 'core')
-rw-r--r--core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/formatter/DefaultCodeFormatterConstants.java12
-rw-r--r--core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/formatter/DefaultCodeFormatterOptions.java10
-rw-r--r--core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java13
-rw-r--r--core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java62
-rw-r--r--core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/formatter/FormatterMessages.java1
-rw-r--r--core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/formatter/FormatterMessages.properties1
-rw-r--r--core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/formatter/NewLinesTabPage.java4
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 3dc6712fd0..8f34e1811c 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
@@ -934,6 +934,18 @@ public class DefaultCodeFormatterConstants {
+ ".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
* - option id: "org.eclipse.cdt.core.formatter.insert_new_line_before_else_in_if_statement"
* - possible values: { INSERT, DO_NOT_INSERT }
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 cb7b71516c..8a58c083d9 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 78dca997d4..307faf2735 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 31745c52ec..bd8d67485c 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 4fd40cc12b..b32d493565 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 4887fb0a47..89adf6b4fd 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 07e139c698..0628080e6f 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
@@ -46,6 +46,10 @@ public class NewLinesTabPage extends FormatterTabPage {
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,
DO_NOT_INSERT_INSERT);

Back to the top