mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-08-04 06:45:43 +02:00
Fix formatter alignment of conditionals and initializer lists
This commit is contained in:
parent
82b9f0cd2f
commit
d358ef7a36
8 changed files with 42 additions and 22 deletions
|
@ -84,6 +84,7 @@ import org.eclipse.cdt.core.dom.ast.IASTEnumerationSpecifier.IASTEnumerator;
|
||||||
import org.eclipse.cdt.core.dom.ast.c.ICASTCompositeTypeSpecifier;
|
import org.eclipse.cdt.core.dom.ast.c.ICASTCompositeTypeSpecifier;
|
||||||
import org.eclipse.cdt.core.dom.ast.c.ICASTDesignatedInitializer;
|
import org.eclipse.cdt.core.dom.ast.c.ICASTDesignatedInitializer;
|
||||||
import org.eclipse.cdt.core.dom.ast.c.ICASTElaboratedTypeSpecifier;
|
import org.eclipse.cdt.core.dom.ast.c.ICASTElaboratedTypeSpecifier;
|
||||||
|
import org.eclipse.cdt.core.dom.ast.c.ICASTEnumerationSpecifier;
|
||||||
import org.eclipse.cdt.core.dom.ast.c.ICASTPointer;
|
import org.eclipse.cdt.core.dom.ast.c.ICASTPointer;
|
||||||
import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor;
|
import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor;
|
||||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier;
|
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier;
|
||||||
|
@ -161,6 +162,7 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
|
||||||
public boolean fSpaceAfterOpeningParen;
|
public boolean fSpaceAfterOpeningParen;
|
||||||
public boolean fSpaceBeforeClosingParen;
|
public boolean fSpaceBeforeClosingParen;
|
||||||
public boolean fSpaceBeforeOpeningParen;
|
public boolean fSpaceBeforeOpeningParen;
|
||||||
|
public int fContinuationIndentation= -1;
|
||||||
public ListAlignment(int mode) {
|
public ListAlignment(int mode) {
|
||||||
fMode= mode;
|
fMode= mode;
|
||||||
}
|
}
|
||||||
|
@ -501,7 +503,6 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
|
||||||
visit((IASTNamedTypeSpecifier)node);
|
visit((IASTNamedTypeSpecifier)node);
|
||||||
} else {
|
} else {
|
||||||
formatNode(node);
|
formatNode(node);
|
||||||
scribe.space();
|
|
||||||
}
|
}
|
||||||
return PROCESS_SKIP;
|
return PROCESS_SKIP;
|
||||||
}
|
}
|
||||||
|
@ -899,7 +900,7 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
|
||||||
declSpec.accept(this);
|
declSpec.accept(this);
|
||||||
final List declarators= Arrays.asList(node.getDeclarators());
|
final List declarators= Arrays.asList(node.getDeclarators());
|
||||||
if (declarators.size() > 0) {
|
if (declarators.size() > 0) {
|
||||||
if (scribe.printComment()) {
|
if (scribe.printComment() || isCompositeTypeDeclaration(declSpec)) {
|
||||||
scribe.space();
|
scribe.space();
|
||||||
}
|
}
|
||||||
final ListAlignment align= new ListAlignment(Alignment.M_COMPACT_SPLIT);
|
final ListAlignment align= new ListAlignment(Alignment.M_COMPACT_SPLIT);
|
||||||
|
@ -913,6 +914,15 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
|
||||||
return PROCESS_SKIP;
|
return PROCESS_SKIP;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test whether the given decl specifier is one of 'class', 'struct', 'union' or 'enum'.
|
||||||
|
* @param declSpec
|
||||||
|
* @return true if the decl specifier is one of 'class', 'struct', 'union' or 'enum'
|
||||||
|
*/
|
||||||
|
private boolean isCompositeTypeDeclaration(IASTDeclSpecifier declSpec) {
|
||||||
|
return declSpec instanceof IASTCompositeTypeSpecifier || declSpec instanceof ICASTEnumerationSpecifier;
|
||||||
|
}
|
||||||
|
|
||||||
private int visit(ICPPASTTemplateDeclaration node) {
|
private int visit(ICPPASTTemplateDeclaration node) {
|
||||||
scribe.printNextToken(Token.t_template, false);
|
scribe.printNextToken(Token.t_template, false);
|
||||||
scribe.printNextToken(Token.tLT, false);
|
scribe.printNextToken(Token.tLT, false);
|
||||||
|
@ -1111,11 +1121,17 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
|
||||||
if (align.fSpaceAfterOpeningParen) {
|
if (align.fSpaceAfterOpeningParen) {
|
||||||
scribe.space();
|
scribe.space();
|
||||||
}
|
}
|
||||||
|
final int continuationIndentation=
|
||||||
|
align.fContinuationIndentation >= 0
|
||||||
|
? align.fContinuationIndentation
|
||||||
|
: preferences.continuation_indentation;
|
||||||
Alignment listAlignment = scribe.createAlignment(
|
Alignment listAlignment = scribe.createAlignment(
|
||||||
"listElements_"+align,//$NON-NLS-1$
|
"listElements_"+align,//$NON-NLS-1$
|
||||||
align.fMode,
|
align.fMode,
|
||||||
elementsLength + (addEllipsis ? 1 : 0),
|
elementsLength + (addEllipsis ? 1 : 0),
|
||||||
scribe.scanner.getCurrentPosition());
|
scribe.scanner.getCurrentPosition(),
|
||||||
|
continuationIndentation,
|
||||||
|
false);
|
||||||
scribe.enterAlignment(listAlignment);
|
scribe.enterAlignment(listAlignment);
|
||||||
boolean ok = false;
|
boolean ok = false;
|
||||||
do {
|
do {
|
||||||
|
@ -1293,20 +1309,13 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
|
||||||
if (preferences.insert_space_after_opening_brace_in_array_initializer) {
|
if (preferences.insert_space_after_opening_brace_in_array_initializer) {
|
||||||
scribe.space();
|
scribe.space();
|
||||||
}
|
}
|
||||||
int indents= preferences.continuation_indentation_for_array_initializer;
|
|
||||||
while (indents-- > 0) {
|
|
||||||
scribe.indent();
|
|
||||||
}
|
|
||||||
|
|
||||||
final ListAlignment align= new ListAlignment(preferences.alignment_for_expressions_in_array_initializer);
|
final ListAlignment align= new ListAlignment(preferences.alignment_for_expressions_in_array_initializer);
|
||||||
align.fSpaceBeforeComma= preferences.insert_space_before_comma_in_array_initializer;
|
align.fSpaceBeforeComma= preferences.insert_space_before_comma_in_array_initializer;
|
||||||
align.fSpaceAfterComma= preferences.insert_space_after_comma_in_array_initializer;
|
align.fSpaceAfterComma= preferences.insert_space_after_comma_in_array_initializer;
|
||||||
|
align.fContinuationIndentation= preferences.continuation_indentation_for_array_initializer;
|
||||||
formatList(initializers, align, false, false);
|
formatList(initializers, align, false, false);
|
||||||
|
|
||||||
indents= preferences.continuation_indentation_for_array_initializer;
|
|
||||||
while (indents-- > 0) {
|
|
||||||
scribe.unIndent();
|
|
||||||
}
|
|
||||||
if (preferences.insert_new_line_before_closing_brace_in_array_initializer) {
|
if (preferences.insert_new_line_before_closing_brace_in_array_initializer) {
|
||||||
scribe.startNewLine();
|
scribe.startNewLine();
|
||||||
}
|
}
|
||||||
|
|
|
@ -744,6 +744,9 @@ public class Scribe {
|
||||||
lastNumberOfNewLines= 0;
|
lastNumberOfNewLines= 0;
|
||||||
printIndentationIfNecessary();
|
printIndentationIfNecessary();
|
||||||
if (considerSpaceIfAny) {
|
if (considerSpaceIfAny) {
|
||||||
|
if (currentAlignment != null && currentAlignment.isIndentOnColumn(column)) {
|
||||||
|
needSpace= true;
|
||||||
|
}
|
||||||
space();
|
space();
|
||||||
}
|
}
|
||||||
if (pendingSpace) {
|
if (pendingSpace) {
|
||||||
|
@ -873,6 +876,9 @@ public class Scribe {
|
||||||
}
|
}
|
||||||
lastNumberOfNewLines= isNewLine ? 1 : 0;
|
lastNumberOfNewLines= isNewLine ? 1 : 0;
|
||||||
needSpace= false;
|
needSpace= false;
|
||||||
|
if (currentAlignment != null) {
|
||||||
|
indentationLevel= currentAlignment.breakIndentationLevel;
|
||||||
|
}
|
||||||
scanner.resetTo(currentTokenEndPosition, scannerEndPosition - 1);
|
scanner.resetTo(currentTokenEndPosition, scannerEndPosition - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -349,6 +349,11 @@ public class Alignment {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// test whether this is an 'indent-on-column' type alignment and aligns on the given column
|
||||||
|
public boolean isIndentOnColumn(int column) {
|
||||||
|
return (mode & M_INDENT_ON_COLUMN) != 0 && breakIndentationLevel == column - 1;
|
||||||
|
}
|
||||||
|
|
||||||
// reset fragment indentation/break status
|
// reset fragment indentation/break status
|
||||||
public void reset() {
|
public void reset() {
|
||||||
|
|
||||||
|
|
|
@ -45,7 +45,7 @@ int foo(int bar) const {
|
||||||
* Line Wrapping
|
* Line Wrapping
|
||||||
*/
|
*/
|
||||||
int array[]= { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 1000,
|
int array[]= { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 1000,
|
||||||
2000, 3000, 4000, 5000 };
|
2000, 3000, 4000, 5000 };
|
||||||
int compare(int argument, int otherArgument) {
|
int compare(int argument, int otherArgument) {
|
||||||
return argument+otherArgument > argument*otherArgument+1000000 ? 100000+50000
|
return argument+otherArgument > argument*otherArgument+1000000 ? 100000+50000
|
||||||
: 200000-30000;
|
: 200000-30000;
|
||||||
|
|
|
@ -26,11 +26,11 @@ const SimpleStruct simpleStruct = { 1, "mySimple", 0.1232 };
|
||||||
|
|
||||||
const SimpleStruct array[] = { { SIZEOF( simpleStruct, num ),
|
const SimpleStruct array[] = { { SIZEOF( simpleStruct, num ),
|
||||||
#if FOO
|
#if FOO
|
||||||
"foo"
|
"foo"
|
||||||
# else
|
# else
|
||||||
"bar"
|
"bar"
|
||||||
#endif
|
#endif
|
||||||
, 0.5 }, { SIZEOF( simpleStruct, floatNum ), "name", 1.1 } };
|
, 0.5 }, { SIZEOF( simpleStruct, floatNum ), "name", 1.1 } };
|
||||||
|
|
||||||
// single line outside scope
|
// single line outside scope
|
||||||
|
|
||||||
|
|
|
@ -412,8 +412,8 @@ final class FormatterMessages extends NLS {
|
||||||
public static String IndentationTabPage_indent_group_title;
|
public static String IndentationTabPage_indent_group_title;
|
||||||
public static String IndentationTabPage_class_group_option_indent_access_specifiers_within_class_body;
|
public static String IndentationTabPage_class_group_option_indent_access_specifiers_within_class_body;
|
||||||
public static String IndentationTabPage_class_group_option_indent_declarations_compare_to_access_specifiers;
|
public static String IndentationTabPage_class_group_option_indent_declarations_compare_to_access_specifiers;
|
||||||
public static String IndentationTabPage_class_group_option_indent_declarations_within_enum_const;
|
// public static String IndentationTabPage_class_group_option_indent_declarations_within_enum_const;
|
||||||
public static String IndentationTabPage_class_group_option_indent_declarations_within_enum_decl;
|
// public static String IndentationTabPage_class_group_option_indent_declarations_within_enum_decl;
|
||||||
public static String IndentationTabPage_block_group_option_indent_statements_compare_to_body;
|
public static String IndentationTabPage_block_group_option_indent_statements_compare_to_body;
|
||||||
public static String IndentationTabPage_block_group_option_indent_statements_compare_to_block;
|
public static String IndentationTabPage_block_group_option_indent_statements_compare_to_block;
|
||||||
public static String IndentationTabPage_switch_group_option_indent_statements_within_switch_body;
|
public static String IndentationTabPage_switch_group_option_indent_statements_within_switch_body;
|
||||||
|
|
|
@ -317,7 +317,7 @@ LineWrappingTabPage_indentation_default=Default indentation
|
||||||
LineWrappingTabPage_indentation_on_column=Indent on column
|
LineWrappingTabPage_indentation_on_column=Indent on column
|
||||||
LineWrappingTabPage_indentation_by_one=Indent by one
|
LineWrappingTabPage_indentation_by_one=Indent by one
|
||||||
#LineWrappingTabPage_class_decls=Class Declarations
|
#LineWrappingTabPage_class_decls=Class Declarations
|
||||||
LineWrappingTabPage_method_decls=Method Declarations
|
LineWrappingTabPage_method_decls=Function Declarations
|
||||||
#LineWrappingTabPage_constructor_decls=Constructor declarations
|
#LineWrappingTabPage_constructor_decls=Constructor declarations
|
||||||
LineWrappingTabPage_function_calls=Function Calls
|
LineWrappingTabPage_function_calls=Function Calls
|
||||||
LineWrappingTabPage_expressions=Expressions
|
LineWrappingTabPage_expressions=Expressions
|
||||||
|
@ -484,8 +484,8 @@ IndentationTabPage_indent_group_title=Indent
|
||||||
|
|
||||||
IndentationTabPage_class_group_option_indent_access_specifiers_within_class_body='public', 'protected', 'private' within class &body
|
IndentationTabPage_class_group_option_indent_access_specifiers_within_class_body='public', 'protected', 'private' within class &body
|
||||||
IndentationTabPage_class_group_option_indent_declarations_compare_to_access_specifiers=De&clarations relative to 'public', 'protected', 'private'
|
IndentationTabPage_class_group_option_indent_declarations_compare_to_access_specifiers=De&clarations relative to 'public', 'protected', 'private'
|
||||||
IndentationTabPage_class_group_option_indent_declarations_within_enum_const=Declarations within en&um constants
|
#IndentationTabPage_class_group_option_indent_declarations_within_enum_const=Declarations within en&um constants
|
||||||
IndentationTabPage_class_group_option_indent_declarations_within_enum_decl=Declarations within enum declaration
|
#IndentationTabPage_class_group_option_indent_declarations_within_enum_decl=Declarations within enum declaration
|
||||||
IndentationTabPage_block_group_option_indent_statements_compare_to_body=Stat&ements within method/constructor body
|
IndentationTabPage_block_group_option_indent_statements_compare_to_body=Stat&ements within method/constructor body
|
||||||
IndentationTabPage_block_group_option_indent_statements_compare_to_block=Statements within bl&ocks
|
IndentationTabPage_block_group_option_indent_statements_compare_to_block=Statements within bl&ocks
|
||||||
IndentationTabPage_namespace_group_option_indent_declarations_within_namespace=Declarations within '&namespace' definition
|
IndentationTabPage_namespace_group_option_indent_declarations_within_namespace=Declarations within '&namespace' definition
|
||||||
|
|
|
@ -446,7 +446,7 @@ public class LineWrappingTabPage extends ModifyDialogTabPage {
|
||||||
|
|
||||||
private final Category fConditionalExpressionCategory= new Category(
|
private final Category fConditionalExpressionCategory= new Category(
|
||||||
DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_CONDITIONAL_EXPRESSION,
|
DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_CONDITIONAL_EXPRESSION,
|
||||||
"int compare(int argument, int otherArgument) {return argument > argument ? 100000 : 200000;}", //$NON-NLS-1$
|
"int compare(int argument, int argument2) {return argument > argument2 ? 100000 : 200000;}", //$NON-NLS-1$
|
||||||
FormatterMessages.LineWrappingTabPage_conditionals
|
FormatterMessages.LineWrappingTabPage_conditionals
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue