Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarco Stornelli2019-04-12 12:15:16 -0400
committerMarco Stornelli2019-05-12 09:42:21 -0400
commitf893b77dba991ba1b3348d72267573540e9ce8bd (patch)
tree91b5f2cbe30fb629af8007f444b31b138c077039 /core/org.eclipse.cdt.core
parent826a0b2c0effc6a8d1479ddbff8d5b4bbd31a6b6 (diff)
downloadorg.eclipse.cdt-f893b77dba991ba1b3348d72267573540e9ce8bd.tar.gz
org.eclipse.cdt-f893b77dba991ba1b3348d72267573540e9ce8bd.tar.xz
org.eclipse.cdt-f893b77dba991ba1b3348d72267573540e9ce8bd.zip
Bug 461435 - Added options to format pointers in methods
Rules used: 1) Default is right alignment to be compliant with built-in formatter rules; 2) Pointer with declarator without name will be always left alignment unless there's a nested declarator; 3) Return value of methods/functions is always left alignment; 4) If left alignment is selected, if space after comma is selected too for a declaration list, space is always forced after the comma. Change-Id: I82d5ad130e665e5357644cc59655a558f3fb732f Signed-off-by: Marco Stornelli <marco.stornelli@gmail.com>
Diffstat (limited to 'core/org.eclipse.cdt.core')
-rw-r--r--core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/formatter/DefaultCodeFormatterConstants.java59
-rw-r--r--core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/formatter/DefaultCodeFormatterOptions.java41
-rw-r--r--core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java76
3 files changed, 163 insertions, 13 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 8f34e1811c..c9a7ab8ca9 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
@@ -543,7 +543,7 @@ public class DefaultCodeFormatterConstants {
* @see #NEXT_LINE
* @see #NEXT_LINE_SHIFTED
* @see #NEXT_LINE_ON_WRAP
- * @since 6.7
+ * @since 6.8
*/
public static final String FORMATTER_BRACE_POSITION_FOR_LINKAGE_DECLARATION = CCorePlugin.PLUGIN_ID
+ ".formatter.brace_position_for_linkage_declaration"; //$NON-NLS-1$
@@ -760,7 +760,7 @@ public class DefaultCodeFormatterConstants {
* </pre>
* @see #TRUE
* @see #FALSE
- * @since 6.7
+ * @since 6.8
*/
public static final String FORMATTER_INDENT_BODY_DECLARATIONS_COMPARE_TO_LINKAGE = CCorePlugin.PLUGIN_ID
+ ".formatter.indent_body_declarations_compare_to_linkage"; //$NON-NLS-1$
@@ -941,6 +941,7 @@ public class DefaultCodeFormatterConstants {
* </pre>
* @see CCorePlugin#INSERT
* @see CCorePlugin#DO_NOT_INSERT
+ * @since 6.8
*/
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$
@@ -2006,6 +2007,58 @@ public class DefaultCodeFormatterConstants {
+ ".formatter.insert_space_before_opening_brace_in_method_declaration"; //$NON-NLS-1$
/**
* <pre>
+ * FORMATTER / Option to insert a space before a pointer in a method declaration
+ * - option id: "org.eclipse.cdt.core.formatter.insert_space_before_pointer_in_method_declaration"
+ * - possible values: { INSERT, DO_NOT_INSERT }
+ * - default: INSERT
+ * </pre>
+ * @see CCorePlugin#INSERT
+ * @see CCorePlugin#DO_NOT_INSERT
+ * @since 6.8
+ */
+ public static final String FORMATTER_INSERT_SPACE_BEFORE_POINTER_IN_METHOD_DECLARATION = CCorePlugin.PLUGIN_ID
+ + ".formatter.insert_space_before_pointer_in_method_declaration"; //$NON-NLS-1$
+ /**
+ * <pre>
+ * FORMATTER / Option to insert a space after a pointer in a method declaration
+ * - option id: "org.eclipse.cdt.core.formatter.insert_space_after_pointer_in_method_declaration"
+ * - possible values: { INSERT, DO_NOT_INSERT }
+ * - default: INSERT
+ * </pre>
+ * @see CCorePlugin#INSERT
+ * @see CCorePlugin#DO_NOT_INSERT
+ * @since 6.8
+ */
+ public static final String FORMATTER_INSERT_SPACE_AFTER_POINTER_IN_METHOD_DECLARATION = CCorePlugin.PLUGIN_ID
+ + ".formatter.insert_space_after_pointer_in_method_declaration"; //$NON-NLS-1$
+ /**
+ * <pre>
+ * FORMATTER / Option to insert a space after a pointer in a declarator list
+ * - option id: "org.eclipse.cdt.core.formatter.insert_space_after_pointer_in_declarator_list"
+ * - possible values: { INSERT, DO_NOT_INSERT }
+ * - default: INSERT
+ * </pre>
+ * @see CCorePlugin#INSERT
+ * @see CCorePlugin#DO_NOT_INSERT
+ * @since 6.8
+ */
+ public static final String FORMATTER_INSERT_SPACE_AFTER_POINTER_IN_DECLARATOR_LIST = CCorePlugin.PLUGIN_ID
+ + ".formatter.insert_space_after_pointer_in_declarator_list"; //$NON-NLS-1$
+ /**
+ * <pre>
+ * FORMATTER / Option to insert a space before a pointer in a declarator list
+ * - option id: "org.eclipse.cdt.core.formatter.insert_space_before_pointer_in_declarator_list"
+ * - possible values: { INSERT, DO_NOT_INSERT }
+ * - default: INSERT
+ * </pre>
+ * @see CCorePlugin#INSERT
+ * @see CCorePlugin#DO_NOT_INSERT
+ * @since 6.8
+ */
+ public static final String FORMATTER_INSERT_SPACE_BEFORE_POINTER_IN_DECLARATOR_LIST = CCorePlugin.PLUGIN_ID
+ + ".formatter.insert_space_before_pointer_in_declarator_list"; //$NON-NLS-1$
+ /**
+ * <pre>
* FORMATTER / Option to insert a space before the opening brace in a switch statement
* - option id: "org.eclipse.cdt.core.formatter.insert_space_before_opening_brace_in_switch"
* - possible values: { INSERT, DO_NOT_INSERT }
@@ -2049,7 +2102,7 @@ public class DefaultCodeFormatterConstants {
* </pre>
* @see CCorePlugin#INSERT
* @see CCorePlugin#DO_NOT_INSERT
- * @since 6.7
+ * @since 6.8
*/
public static final String FORMATTER_INSERT_SPACE_BEFORE_OPENING_BRACE_IN_LINKAGE_DECLARATION = CCorePlugin.PLUGIN_ID
+ ".formatter.insert_space_before_opening_brace_in_linkage_declaration"; //$NON-NLS-1$
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 8a58c083d9..8aacee760d 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
@@ -168,6 +168,8 @@ public class DefaultCodeFormatterOptions {
public boolean insert_space_after_comma_in_method_invocation_arguments;
public boolean insert_space_after_comma_in_method_declaration_parameters;
public boolean insert_space_after_comma_in_method_declaration_throws;
+ public boolean insert_space_after_pointer_in_declarator_list;
+ public boolean insert_space_before_pointer_in_declarator_list;
public boolean insert_space_after_comma_in_declarator_list;
public boolean insert_space_after_comma_in_expression_list;
public boolean insert_space_after_comma_in_base_types;
@@ -234,6 +236,8 @@ public class DefaultCodeFormatterOptions {
public boolean insert_space_before_opening_brace_in_initializer_list;
public boolean insert_space_before_opening_brace_in_block;
public boolean insert_space_before_opening_brace_in_method_declaration;
+ public boolean insert_space_before_pointer_in_method_declaration;
+ public boolean insert_space_after_pointer_in_method_declaration;
public boolean insert_space_before_opening_brace_in_type_declaration;
public boolean insert_space_before_opening_brace_in_namespace_declaration;
/**
@@ -646,6 +650,15 @@ public class DefaultCodeFormatterOptions {
options.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_OPENING_BRACE_IN_METHOD_DECLARATION,
this.insert_space_before_opening_brace_in_method_declaration ? CCorePlugin.INSERT
: CCorePlugin.DO_NOT_INSERT);
+ options.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_POINTER_IN_METHOD_DECLARATION,
+ this.insert_space_before_pointer_in_method_declaration ? CCorePlugin.INSERT
+ : CCorePlugin.DO_NOT_INSERT);
+ options.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_AFTER_POINTER_IN_METHOD_DECLARATION,
+ this.insert_space_after_pointer_in_method_declaration ? CCorePlugin.INSERT : CCorePlugin.DO_NOT_INSERT);
+ options.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_AFTER_POINTER_IN_DECLARATOR_LIST,
+ this.insert_space_after_pointer_in_declarator_list ? CCorePlugin.INSERT : CCorePlugin.DO_NOT_INSERT);
+ options.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_POINTER_IN_DECLARATOR_LIST,
+ this.insert_space_before_pointer_in_declarator_list ? CCorePlugin.INSERT : CCorePlugin.DO_NOT_INSERT);
options.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_OPENING_BRACE_IN_TYPE_DECLARATION,
this.insert_space_before_opening_brace_in_type_declaration ? CCorePlugin.INSERT
: CCorePlugin.DO_NOT_INSERT);
@@ -1847,6 +1860,30 @@ public class DefaultCodeFormatterOptions {
this.insert_space_before_opening_brace_in_method_declaration = CCorePlugin.INSERT
.equals(insertSpaceBeforeOpeningBraceInMethodDeclarationOption);
}
+ final Object insertSpaceBeforePointerInMethodDeclarationOption = settings
+ .get(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_POINTER_IN_METHOD_DECLARATION);
+ if (insertSpaceBeforePointerInMethodDeclarationOption != null) {
+ this.insert_space_before_pointer_in_method_declaration = CCorePlugin.INSERT
+ .equals(insertSpaceBeforePointerInMethodDeclarationOption);
+ }
+ final Object insertSpaceAfterPointerInMethodDeclarationOption = settings
+ .get(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_AFTER_POINTER_IN_METHOD_DECLARATION);
+ if (insertSpaceAfterPointerInMethodDeclarationOption != null) {
+ this.insert_space_after_pointer_in_method_declaration = CCorePlugin.INSERT
+ .equals(insertSpaceAfterPointerInMethodDeclarationOption);
+ }
+ final Object insertSpaceAfterPointerInDeclaratorListOption = settings
+ .get(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_AFTER_POINTER_IN_DECLARATOR_LIST);
+ if (insertSpaceAfterPointerInDeclaratorListOption != null) {
+ this.insert_space_after_pointer_in_declarator_list = CCorePlugin.INSERT
+ .equals(insertSpaceAfterPointerInDeclaratorListOption);
+ }
+ final Object insertSpaceBeforePointerInDeclaratorListOption = settings
+ .get(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_POINTER_IN_DECLARATOR_LIST);
+ if (insertSpaceBeforePointerInDeclaratorListOption != null) {
+ this.insert_space_before_pointer_in_declarator_list = CCorePlugin.INSERT
+ .equals(insertSpaceBeforePointerInDeclaratorListOption);
+ }
final Object insertSpaceBeforeOpeningBraceInTypeDeclarationOption = settings
.get(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_OPENING_BRACE_IN_TYPE_DECLARATION);
if (insertSpaceBeforeOpeningBraceInTypeDeclarationOption != null) {
@@ -2259,6 +2296,10 @@ public class DefaultCodeFormatterOptions {
this.insert_space_before_opening_brace_in_initializer_list = true;
this.insert_space_before_opening_brace_in_block = true;
this.insert_space_before_opening_brace_in_method_declaration = true;
+ this.insert_space_before_pointer_in_method_declaration = true;
+ this.insert_space_after_pointer_in_method_declaration = false;
+ this.insert_space_before_pointer_in_declarator_list = true;
+ this.insert_space_after_pointer_in_declarator_list = false;
this.insert_space_before_opening_brace_in_switch = true;
this.insert_space_before_opening_brace_in_type_declaration = true;
this.insert_space_before_opening_brace_in_namespace_declaration = true;
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 ec20bed2b7..2914d8bd5f 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
@@ -303,7 +303,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
@Override
public void run() {
- boolean needSpace = skipConstVolatileRestrict();
+ boolean needSpace = skipConstVolatileRestrict(true);
int token = peekNextToken();
// Ref-qualifier.
if (token == Token.tAMPER || token == Token.tAND) {
@@ -830,9 +830,10 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
if (name != null && name.getSimpleID().length != 0 || nestedDecl != null) {
if (node.getPropertyInParent() != IASTDeclarator.NESTED_DECLARATOR && isFirstDeclarator(node)) {
// Preserve non-space between pointer operator and name or nested declarator.
- if (pointerOperators.length == 0 || scribe.printComment()) {
+ if (pointerOperators.length == 0) {
scribe.space();
- }
+ } else
+ scribe.printComment();
}
if (name != null)
name.accept(this);
@@ -1544,11 +1545,11 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
}
}
- private boolean skipConstVolatileRestrict() {
+ private boolean skipConstVolatileRestrict(boolean spaceBefore) {
boolean skipped = false;
int token = peekNextToken();
while (token == Token.t_const || token == Token.t_volatile || token == Token.t_restrict) {
- scribe.printNextToken(token, true);
+ scribe.printNextToken(token, spaceBefore);
token = peekNextToken();
skipped = true;
}
@@ -1642,6 +1643,61 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
}
}
+ /**
+ * Align pointers according to user formatter rule. Pointers (or references) can be
+ * left, center or right alignment. Pointers with implicit name will be always left
+ * aligned unless they have nested declarators.
+ * @param pointers The list of all pointers
+ * @param pointer The pointer to be formatted
+ * @param token The token to be used: and, amper, star.
+ */
+ private boolean alignPointer(IASTPointerOperator[] pointers, IASTPointerOperator pointer, int token) {
+ boolean firstPtr = pointer == pointers[0];
+ boolean lastPtr = pointers.length == 1 || pointer == pointers[pointers.length - 1];
+ TrailingTokenFormatter tailFormatter = null;
+ IASTNode parent = pointer.getParent();
+ boolean needSpace = false;
+ if (parent instanceof IASTFunctionDeclarator) {
+ tailFormatter = new TrailingTokenFormatter(token, pointer.getParent(), false, true);
+ tailFormatter.run();
+ } else {
+ if (parent instanceof IASTDeclarator) {
+ char[] simpleId = ((IASTDeclarator) parent).getName().getSimpleID();
+ IASTDeclarator nested = ((IASTDeclarator) parent).getNestedDeclarator();
+ if ((simpleId == null || simpleId.length == 0) && nested == null) {
+ needSpace = true;
+ tailFormatter = new TrailingTokenFormatter(token, pointer.getParent(), false, false);
+ tailFormatter.run();
+ return needSpace;
+ }
+ }
+ if (parent != null && parent.getParent() instanceof IASTParameterDeclaration) {
+ needSpace = this.preferences.insert_space_after_pointer_in_method_declaration && lastPtr;
+ tailFormatter = new TrailingTokenFormatter(token, pointer.getParent(),
+ this.preferences.insert_space_before_pointer_in_method_declaration && firstPtr,
+ this.preferences.insert_space_after_pointer_in_method_declaration && lastPtr);
+ tailFormatter.run();
+ } else if (parent != null && parent.getParent() instanceof IASTSimpleDeclaration) {
+ needSpace = this.preferences.insert_space_after_pointer_in_declarator_list && lastPtr;
+ IASTSimpleDeclaration simple = (IASTSimpleDeclaration) parent.getParent();
+ IASTDeclarator[] declarators = simple.getDeclarators();
+ boolean first = declarators.length == 0 || declarators[0].getPointerOperators() == pointers;
+ tailFormatter = new TrailingTokenFormatter(token, pointer.getParent(),
+ first ? this.preferences.insert_space_before_pointer_in_declarator_list && firstPtr
+ : (this.preferences.insert_space_before_pointer_in_declarator_list
+ || this.preferences.insert_space_after_comma_in_declarator_list) && firstPtr,
+ this.preferences.insert_space_after_pointer_in_declarator_list && lastPtr);
+ tailFormatter.run();
+ } else
+ scribe.printNextToken(token, false);
+ }
+ return needSpace;
+ }
+
+ /**
+ * Format pointers operators
+ * @param pointers The list of pointers
+ */
private void formatPointers(IASTPointerOperator[] pointers) {
for (IASTPointerOperator pointer : pointers) {
if (scribe.printComment()) {
@@ -1652,9 +1708,9 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
}
if (pointer instanceof ICPPASTReferenceOperator) {
if (((ICPPASTReferenceOperator) pointer).isRValueReference()) {
- scribe.printNextToken(Token.tAND, false);
+ alignPointer(pointers, pointer, Token.tAND);
} else {
- scribe.printNextToken(Token.tAMPER, false);
+ alignPointer(pointers, pointer, Token.tAMPER);
}
} else if (pointer instanceof ICPPASTPointerToMember) {
final ICPPASTPointerToMember ptrToMember = (ICPPASTPointerToMember) pointer;
@@ -1663,12 +1719,12 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
name.accept(this);
}
scribe.printNextToken(Token.tSTAR, false);
- if (skipConstVolatileRestrict()) {
+ if (skipConstVolatileRestrict(false)) {
scribe.space();
}
} else {
- scribe.printNextToken(Token.tSTAR, false);
- if (skipConstVolatileRestrict()) {
+ boolean needSpace = alignPointer(pointers, pointer, Token.tSTAR);
+ if (skipConstVolatileRestrict(needSpace)) {
scribe.space();
}
}

Back to the top