1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-25 18:05:33 +02:00

Fix formatter alignment of conditionals and initializer lists

This commit is contained in:
Anton Leherbauer 2007-04-06 11:56:48 +00:00
parent 82b9f0cd2f
commit d358ef7a36
8 changed files with 42 additions and 22 deletions

View file

@ -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.ICASTDesignatedInitializer;
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.cpp.CPPASTVisitor;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier;
@ -161,6 +162,7 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
public boolean fSpaceAfterOpeningParen;
public boolean fSpaceBeforeClosingParen;
public boolean fSpaceBeforeOpeningParen;
public int fContinuationIndentation= -1;
public ListAlignment(int mode) {
fMode= mode;
}
@ -501,7 +503,6 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
visit((IASTNamedTypeSpecifier)node);
} else {
formatNode(node);
scribe.space();
}
return PROCESS_SKIP;
}
@ -899,7 +900,7 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
declSpec.accept(this);
final List declarators= Arrays.asList(node.getDeclarators());
if (declarators.size() > 0) {
if (scribe.printComment()) {
if (scribe.printComment() || isCompositeTypeDeclaration(declSpec)) {
scribe.space();
}
final ListAlignment align= new ListAlignment(Alignment.M_COMPACT_SPLIT);
@ -913,6 +914,15 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
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) {
scribe.printNextToken(Token.t_template, false);
scribe.printNextToken(Token.tLT, false);
@ -1111,11 +1121,17 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
if (align.fSpaceAfterOpeningParen) {
scribe.space();
}
final int continuationIndentation=
align.fContinuationIndentation >= 0
? align.fContinuationIndentation
: preferences.continuation_indentation;
Alignment listAlignment = scribe.createAlignment(
"listElements_"+align,//$NON-NLS-1$
align.fMode,
elementsLength + (addEllipsis ? 1 : 0),
scribe.scanner.getCurrentPosition());
scribe.scanner.getCurrentPosition(),
continuationIndentation,
false);
scribe.enterAlignment(listAlignment);
boolean ok = false;
do {
@ -1293,20 +1309,13 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
if (preferences.insert_space_after_opening_brace_in_array_initializer) {
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);
align.fSpaceBeforeComma= preferences.insert_space_before_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);
indents= preferences.continuation_indentation_for_array_initializer;
while (indents-- > 0) {
scribe.unIndent();
}
if (preferences.insert_new_line_before_closing_brace_in_array_initializer) {
scribe.startNewLine();
}

View file

@ -744,6 +744,9 @@ public class Scribe {
lastNumberOfNewLines= 0;
printIndentationIfNecessary();
if (considerSpaceIfAny) {
if (currentAlignment != null && currentAlignment.isIndentOnColumn(column)) {
needSpace= true;
}
space();
}
if (pendingSpace) {
@ -873,6 +876,9 @@ public class Scribe {
}
lastNumberOfNewLines= isNewLine ? 1 : 0;
needSpace= false;
if (currentAlignment != null) {
indentationLevel= currentAlignment.breakIndentationLevel;
}
scanner.resetTo(currentTokenEndPosition, scannerEndPosition - 1);
}

View file

@ -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
public void reset() {

View file

@ -45,7 +45,7 @@ int foo(int bar) const {
* Line Wrapping
*/
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) {
return argument+otherArgument > argument*otherArgument+1000000 ? 100000+50000
: 200000-30000;

View file

@ -26,11 +26,11 @@ const SimpleStruct simpleStruct = { 1, "mySimple", 0.1232 };
const SimpleStruct array[] = { { SIZEOF( simpleStruct, num ),
#if FOO
"foo"
"foo"
# else
"bar"
"bar"
#endif
, 0.5 }, { SIZEOF( simpleStruct, floatNum ), "name", 1.1 } };
, 0.5 }, { SIZEOF( simpleStruct, floatNum ), "name", 1.1 } };
// single line outside scope

View file

@ -412,8 +412,8 @@ final class FormatterMessages extends NLS {
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_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_decl;
// 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_block_group_option_indent_statements_compare_to_body;
public static String IndentationTabPage_block_group_option_indent_statements_compare_to_block;
public static String IndentationTabPage_switch_group_option_indent_statements_within_switch_body;

View file

@ -317,7 +317,7 @@ LineWrappingTabPage_indentation_default=Default indentation
LineWrappingTabPage_indentation_on_column=Indent on column
LineWrappingTabPage_indentation_by_one=Indent by one
#LineWrappingTabPage_class_decls=Class Declarations
LineWrappingTabPage_method_decls=Method Declarations
LineWrappingTabPage_method_decls=Function Declarations
#LineWrappingTabPage_constructor_decls=Constructor declarations
LineWrappingTabPage_function_calls=Function Calls
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_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_decl=Declarations within enum declaration
#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_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_namespace_group_option_indent_declarations_within_namespace=Declarations within '&namespace' definition

View file

@ -446,7 +446,7 @@ public class LineWrappingTabPage extends ModifyDialogTabPage {
private final Category fConditionalExpressionCategory= new Category(
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
);