mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-08-04 06:45:43 +02:00
Fix for 225326: Code formatter strips whitespace when it shouldn't
This commit is contained in:
parent
97170d3d09
commit
fc2be4a64b
2 changed files with 76 additions and 15 deletions
|
@ -970,6 +970,7 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
|
|||
|
||||
// declarator
|
||||
final IASTFunctionDeclarator declarator= node.getDeclarator();
|
||||
skipNonWhitespaceToNode(declarator);
|
||||
boolean needSpace= declarator.getPointerOperators().length > 0 && scribe.printComment();
|
||||
if (needSpace) {
|
||||
scribe.space();
|
||||
|
@ -1523,6 +1524,7 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
|
|||
scribe.space();
|
||||
}
|
||||
|
||||
final int headerIndent= scribe.numberOfIndentations;
|
||||
scribe.printNextToken(Token.t_enum, true);
|
||||
final IASTName name= node.getName();
|
||||
if (name != null) {
|
||||
|
@ -1532,20 +1534,23 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
|
|||
|
||||
formatLeftCurlyBrace(line, preferences.brace_position_for_type_declaration);
|
||||
formatOpeningBrace(preferences.brace_position_for_type_declaration, preferences.insert_space_before_opening_brace_in_type_declaration);
|
||||
final int braceIndent= scribe.numberOfIndentations;
|
||||
|
||||
// if (preferences.insert_new_line_after_opening_brace_in_enumerator_list) {
|
||||
// scribe.printNewLine();
|
||||
// }
|
||||
// if (preferences.insert_space_after_opening_brace_in_enumerator_list) {
|
||||
// scribe.space();
|
||||
// }
|
||||
|
||||
if (true /* preferences.insert_new_line_after_opening_brace_in_enumerator_list */) {
|
||||
scribe.startNewLine();
|
||||
} else if (true /* preferences.insert_space_after_opening_brace_in_enumerator_list */) {
|
||||
scribe.space();
|
||||
}
|
||||
if (braceIndent == headerIndent) {
|
||||
scribe.indent();
|
||||
}
|
||||
final int enumIndent= scribe.numberOfIndentations;
|
||||
final IASTEnumerator[] enumerators= node.getEnumerators();
|
||||
|
||||
final ListAlignment align= new ListAlignment(preferences.alignment_for_enumerator_list);
|
||||
align.fSpaceBeforeComma= preferences.insert_space_before_comma_in_enum_declarations;
|
||||
align.fSpaceAfterComma= preferences.insert_space_after_comma_in_enum_declarations;
|
||||
align.fContinuationIndentation= 1;
|
||||
align.fContinuationIndentation= enumIndent == headerIndent ? 1 : 0;
|
||||
formatList(Arrays.asList(enumerators), align, false, false);
|
||||
|
||||
// handle trailing comma
|
||||
|
@ -1556,13 +1561,14 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
|
|||
}
|
||||
}
|
||||
|
||||
// if (preferences.insert_new_line_before_closing_brace_in_enumerator_list) {
|
||||
// scribe.startNewLine();
|
||||
// }
|
||||
// if (preferences.insert_space_before_closing_brace_in_enumerator_list) {
|
||||
// scribe.space();
|
||||
// }
|
||||
if (enumIndent > braceIndent) {
|
||||
scribe.unIndent();
|
||||
}
|
||||
if (true /* preferences.insert_new_line_before_closing_brace_in_enumerator_list */) {
|
||||
scribe.startNewLine();
|
||||
} else if (true /* preferences.insert_space_before_closing_brace_in_enumerator_list */) {
|
||||
scribe.space();
|
||||
}
|
||||
formatClosingBrace(preferences.brace_position_for_type_declaration);
|
||||
return PROCESS_SKIP;
|
||||
}
|
||||
|
@ -2858,6 +2864,21 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
|
|||
}
|
||||
}
|
||||
|
||||
private void skipNonWhitespaceToNode(IASTNode node) {
|
||||
final IASTNodeLocation fileLocation= node.getFileLocation();
|
||||
if (fileLocation != null) {
|
||||
final int startOffset= fileLocation.getNodeOffset();
|
||||
final int nextTokenOffset= getNextTokenOffset();
|
||||
if (nextTokenOffset < startOffset) {
|
||||
final int currentOffset= scribe.scanner.getCurrentTokenEndPosition() + 1;
|
||||
final int restLength= startOffset - currentOffset;
|
||||
if (restLength > 0) {
|
||||
scribe.printRaw(currentOffset, restLength);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Format an expression nested in parenthesis. If the operand is
|
||||
* <code>null</code>, empty parenthesis are expected.
|
||||
|
|
|
@ -561,4 +561,44 @@ public class CodeFormatterTest extends BaseUITestCase {
|
|||
assertFormatterResult();
|
||||
}
|
||||
|
||||
//NOT_DEFINED void foo()
|
||||
// {
|
||||
// }
|
||||
//
|
||||
//enum T1
|
||||
// {
|
||||
// E1 = 1
|
||||
// };
|
||||
|
||||
//NOT_DEFINED void foo() {
|
||||
//}
|
||||
//
|
||||
//enum T1 {
|
||||
// E1 = 1
|
||||
//};
|
||||
public void testPreserveWhitespace_Bug225326() throws Exception {
|
||||
assertFormatterResult();
|
||||
}
|
||||
|
||||
//NOT_DEFINED void foo()
|
||||
// {
|
||||
// }
|
||||
//
|
||||
//enum T1
|
||||
// {
|
||||
// E1 = 1
|
||||
// };
|
||||
|
||||
//NOT_DEFINED void foo()
|
||||
// {
|
||||
// }
|
||||
//
|
||||
//enum T1
|
||||
// {
|
||||
// E1 = 1
|
||||
// };
|
||||
public void testPreserveWhitespace2_Bug225326() throws Exception {
|
||||
fOptions= DefaultCodeFormatterOptions.getWhitesmithsSettings().getMap();
|
||||
assertFormatterResult();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue