1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

Bug 314958 - Editor does not format array initializers correctly

This commit is contained in:
Anton Leherbauer 2010-08-13 10:55:44 +00:00
parent d02d896e6d
commit ca643bbb15
3 changed files with 67 additions and 15 deletions

View file

@ -93,6 +93,7 @@ import org.eclipse.cdt.core.dom.ast.IASTEnumerationSpecifier.IASTEnumerator;
import org.eclipse.cdt.core.dom.ast.c.ICASTArrayModifier; import org.eclipse.cdt.core.dom.ast.c.ICASTArrayModifier;
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.ICASTDesignator;
import org.eclipse.cdt.core.dom.ast.c.ICASTTypeIdInitializerExpression; import org.eclipse.cdt.core.dom.ast.c.ICASTTypeIdInitializerExpression;
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.ICPPASTCastExpression; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCastExpression;
@ -412,9 +413,9 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
if (node instanceof IASTEqualsInitializer) { if (node instanceof IASTEqualsInitializer) {
visit((IASTEqualsInitializer) node); visit((IASTEqualsInitializer) node);
} else if (node instanceof IASTInitializerList) { } else if (node instanceof IASTInitializerList) {
visit((IASTInitializerList)node); visit((IASTInitializerList) node);
} else if (node instanceof ICASTDesignatedInitializer) { } else if (node instanceof ICASTDesignatedInitializer) {
formatRaw(node); visit((ICASTDesignatedInitializer) node);
} else { } else {
formatRaw(node); formatRaw(node);
} }
@ -1973,6 +1974,47 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
return PROCESS_SKIP; return PROCESS_SKIP;
} }
private int visit(ICASTDesignatedInitializer node) {
scribe.printComment();
ICASTDesignator[] designators = node.getDesignators();
for (ICASTDesignator designator : designators) {
designator.accept(this);
if (scribe.printComment()) {
scribe.space();
}
}
if (peekNextToken() == Token.tASSIGN) {
scribe.printNextToken(Token.tASSIGN, preferences.insert_space_before_assignment_operator);
if (preferences.insert_space_after_assignment_operator) {
scribe.space();
}
}
Alignment expressionAlignment= scribe.createAlignment(
"designatedInitializer", //$NON-NLS-1$
Alignment.M_COMPACT_SPLIT,
1,
scribe.scanner.getCurrentPosition());
scribe.enterAlignment(expressionAlignment);
boolean ok = false;
do {
try {
scribe.alignFragment(expressionAlignment, 0);
IASTInitializerClause initializer = node.getOperand();
initializer.accept(this);
ok = true;
} catch (AlignmentException e) {
scribe.redoAlignment(e);
}
} while (!ok);
scribe.exitAlignment(expressionAlignment, true);
return PROCESS_SKIP;
}
private int visit(IASTInitializerList node) { private int visit(IASTInitializerList node) {
scribe.printComment(); scribe.printComment();
@ -2215,7 +2257,7 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
scribe.space(); scribe.space();
} }
node.getSubscriptExpression().accept(this); node.getArgument().accept(this);
scribe.printNextToken(Token.tRBRACKET, preferences.insert_space_before_closing_bracket); scribe.printNextToken(Token.tRBRACKET, preferences.insert_space_before_closing_bracket);
return PROCESS_SKIP; return PROCESS_SKIP;

View file

@ -23,6 +23,7 @@ import org.eclipse.jface.text.TextUtilities;
import org.eclipse.text.edits.TextEdit; import org.eclipse.text.edits.TextEdit;
import org.eclipse.cdt.core.CCorePlugin; import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.ast.gnu.c.GCCLanguage;
import org.eclipse.cdt.core.formatter.CodeFormatter; import org.eclipse.cdt.core.formatter.CodeFormatter;
import org.eclipse.cdt.core.formatter.DefaultCodeFormatterConstants; import org.eclipse.cdt.core.formatter.DefaultCodeFormatterConstants;
import org.eclipse.cdt.ui.tests.BaseUITestCase; import org.eclipse.cdt.ui.tests.BaseUITestCase;
@ -38,7 +39,7 @@ import org.eclipse.cdt.internal.formatter.align.Alignment;
*/ */
public class CodeFormatterTest extends BaseUITestCase { public class CodeFormatterTest extends BaseUITestCase {
private Map<String, String> fOptions; private Map<String, Object> fOptions;
private Map<String, String> fDefaultOptions; private Map<String, String> fDefaultOptions;
public static TestSuite suite() { public static TestSuite suite() {
@ -49,7 +50,7 @@ public class CodeFormatterTest extends BaseUITestCase {
protected void setUp() throws Exception { protected void setUp() throws Exception {
super.setUp(); super.setUp();
fDefaultOptions= DefaultCodeFormatterOptions.getDefaultSettings().getMap(); fDefaultOptions= DefaultCodeFormatterOptions.getDefaultSettings().getMap();
fOptions= new HashMap<String, String>(fDefaultOptions); fOptions= new HashMap<String, Object>(fDefaultOptions);
} }
@Override @Override
@ -243,7 +244,7 @@ public class CodeFormatterTest extends BaseUITestCase {
// A(); // A();
// }; // };
public void testWhiteSmithsAccessSpecifierIndentation1_Bug204575() throws Exception { public void testWhiteSmithsAccessSpecifierIndentation1_Bug204575() throws Exception {
fOptions= DefaultCodeFormatterOptions.getWhitesmithsSettings().getMap(); fOptions.putAll(DefaultCodeFormatterOptions.getWhitesmithsSettings().getMap());
fOptions.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_ACCESS_SPECIFIER_COMPARE_TO_TYPE_HEADER, DefaultCodeFormatterConstants.FALSE); fOptions.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_ACCESS_SPECIFIER_COMPARE_TO_TYPE_HEADER, DefaultCodeFormatterConstants.FALSE);
fOptions.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_BODY_DECLARATIONS_COMPARE_TO_ACCESS_SPECIFIER, DefaultCodeFormatterConstants.TRUE); fOptions.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_BODY_DECLARATIONS_COMPARE_TO_ACCESS_SPECIFIER, DefaultCodeFormatterConstants.TRUE);
assertFormatterResult(); assertFormatterResult();
@ -260,7 +261,7 @@ public class CodeFormatterTest extends BaseUITestCase {
// A(); // A();
// }; // };
public void testWhiteSmithsAccessSpecifierIndentation2_Bug204575() throws Exception { public void testWhiteSmithsAccessSpecifierIndentation2_Bug204575() throws Exception {
fOptions= DefaultCodeFormatterOptions.getWhitesmithsSettings().getMap(); fOptions.putAll(DefaultCodeFormatterOptions.getWhitesmithsSettings().getMap());
fOptions.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_ACCESS_SPECIFIER_COMPARE_TO_TYPE_HEADER, DefaultCodeFormatterConstants.TRUE); fOptions.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_ACCESS_SPECIFIER_COMPARE_TO_TYPE_HEADER, DefaultCodeFormatterConstants.TRUE);
fOptions.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_BODY_DECLARATIONS_COMPARE_TO_ACCESS_SPECIFIER, DefaultCodeFormatterConstants.FALSE); fOptions.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_BODY_DECLARATIONS_COMPARE_TO_ACCESS_SPECIFIER, DefaultCodeFormatterConstants.FALSE);
assertFormatterResult(); assertFormatterResult();
@ -277,7 +278,7 @@ public class CodeFormatterTest extends BaseUITestCase {
// A(); // A();
// }; // };
public void testWhiteSmithsAccessSpecifierIndentation3_Bug204575() throws Exception { public void testWhiteSmithsAccessSpecifierIndentation3_Bug204575() throws Exception {
fOptions= DefaultCodeFormatterOptions.getWhitesmithsSettings().getMap(); fOptions.putAll(DefaultCodeFormatterOptions.getWhitesmithsSettings().getMap());
fOptions.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_ACCESS_SPECIFIER_COMPARE_TO_TYPE_HEADER, DefaultCodeFormatterConstants.TRUE); fOptions.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_ACCESS_SPECIFIER_COMPARE_TO_TYPE_HEADER, DefaultCodeFormatterConstants.TRUE);
fOptions.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_BODY_DECLARATIONS_COMPARE_TO_ACCESS_SPECIFIER, DefaultCodeFormatterConstants.TRUE); fOptions.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_BODY_DECLARATIONS_COMPARE_TO_ACCESS_SPECIFIER, DefaultCodeFormatterConstants.TRUE);
assertFormatterResult(); assertFormatterResult();
@ -564,7 +565,7 @@ public class CodeFormatterTest extends BaseUITestCase {
// // Types: // // Types:
// }; // };
public void testGNUCodingStyleConformance_Bug192764() throws Exception { public void testGNUCodingStyleConformance_Bug192764() throws Exception {
fOptions= DefaultCodeFormatterOptions.getGNUSettings().getMap(); fOptions.putAll(DefaultCodeFormatterOptions.getGNUSettings().getMap());
assertFormatterResult(); assertFormatterResult();
} }
@ -604,7 +605,7 @@ public class CodeFormatterTest extends BaseUITestCase {
// E1 = 1 // E1 = 1
// }; // };
public void testPreserveWhitespace2_Bug225326() throws Exception { public void testPreserveWhitespace2_Bug225326() throws Exception {
fOptions= DefaultCodeFormatterOptions.getWhitesmithsSettings().getMap(); fOptions.putAll(DefaultCodeFormatterOptions.getWhitesmithsSettings().getMap());
assertFormatterResult(); assertFormatterResult();
} }
@ -628,7 +629,7 @@ public class CodeFormatterTest extends BaseUITestCase {
// //
//CActiveScheduler* scheduler = new (ELeave) CActiveScheduler(); //CActiveScheduler* scheduler = new (ELeave) CActiveScheduler();
public void testFormatterRegressions_Bug225858() throws Exception { public void testFormatterRegressions_Bug225858() throws Exception {
fOptions= DefaultCodeFormatterOptions.getWhitesmithsSettings().getMap(); fOptions.putAll(DefaultCodeFormatterOptions.getWhitesmithsSettings().getMap());
assertFormatterResult(); assertFormatterResult();
} }
@ -1001,7 +1002,7 @@ public class CodeFormatterTest extends BaseUITestCase {
// } // }
//} //}
public void testCompoundStatementAsMacroGNU_Bug244928() throws Exception { public void testCompoundStatementAsMacroGNU_Bug244928() throws Exception {
fOptions= DefaultCodeFormatterOptions.getGNUSettings().getMap(); fOptions.putAll(DefaultCodeFormatterOptions.getGNUSettings().getMap());
assertFormatterResult(); assertFormatterResult();
} }
@ -1356,4 +1357,13 @@ public class CodeFormatterTest extends BaseUITestCase {
public void testCastAsMacro_Bug285901() throws Exception { public void testCastAsMacro_Bug285901() throws Exception {
assertFormatterResult(); assertFormatterResult();
} }
//PARENT_T sample={.a=1,.b={a[2]=1,.b.c=2}};
//PARENT_T sample = { .a = 1, .b = { a[2] = 1, .b.c = 2 } };
public void testDesignatedInitializer_Bug314958() throws Exception {
fOptions.put(DefaultCodeFormatterConstants.FORMATTER_LANGUAGE, GCCLanguage.getDefault());
assertFormatterResult();
}
} }

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2000, 2008 IBM Corporation and others. * Copyright (c) 2000, 2010 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -117,7 +117,7 @@ public class CodeFormatterUtil {
* @throws IllegalArgumentException If the offset and length are not inside the string, a * @throws IllegalArgumentException If the offset and length are not inside the string, a
* IllegalArgumentException is thrown. * IllegalArgumentException is thrown.
*/ */
public static TextEdit format(int kind, String source, int offset, int length, int indentationLevel, String lineSeparator, Map<String, String> options) { public static TextEdit format(int kind, String source, int offset, int length, int indentationLevel, String lineSeparator, Map<String, ?> options) {
if (offset < 0 || length < 0 || offset + length > source.length()) { if (offset < 0 || length < 0 || offset + length > source.length()) {
throw new IllegalArgumentException("offset or length outside of string. offset: " + offset + ", length: " + length + ", string size: " + source.length()); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$ throw new IllegalArgumentException("offset or length outside of string. offset: " + offset + ", length: " + length + ", string size: " + source.length()); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
} }
@ -128,7 +128,7 @@ public class CodeFormatterUtil {
return null; return null;
} }
public static TextEdit format(int kind, String source, int indentationLevel, String lineSeparator, Map<String, String> options) { public static TextEdit format(int kind, String source, int indentationLevel, String lineSeparator, Map<String, ?> options) {
String prefix= ""; //$NON-NLS-1$ String prefix= ""; //$NON-NLS-1$
String suffix= ""; //$NON-NLS-1$ String suffix= ""; //$NON-NLS-1$
switch (kind) { switch (kind) {