From a27ec4031f44f0c5dfd31ac9c70fd2f270b2c575 Mon Sep 17 00:00:00 2001 From: Anton Leherbauer Date: Tue, 29 Apr 2008 08:36:33 +0000 Subject: [PATCH] Fix for 228997: Eclipse CDT Code style formater bug with const parameter --- .../formatter/CodeFormatterVisitor.java | 15 +++++---- .../cdt/internal/formatter/Scribe.java | 16 ++++++--- .../cdt/ui/tests/text/CodeFormatterTest.java | 33 ++++++++++++------- 3 files changed, 43 insertions(+), 21 deletions(-) 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 60a8d1116ae..13b98329e20 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 @@ -1596,7 +1596,7 @@ public class CodeFormatterVisitor extends CPPASTVisitor { ? align.fContinuationIndentation : preferences.continuation_indentation; Alignment listAlignment = scribe.createAlignment( - "listElements_"+align,//$NON-NLS-1$ + "listElements_"+align, //$NON-NLS-1$ align.fMode, elementsLength + (addEllipsis ? 1 : 0), scribe.scanner.getCurrentPosition(), @@ -2792,12 +2792,15 @@ public class CodeFormatterVisitor extends CPPASTVisitor { int nodeEndOffset= fileLocation.getNodeOffset() + fileLocation.getNodeLength(); scribe.restartAtOffset(nodeEndOffset); } + } else if (scribe.currentAlignmentException == null) { + // print rest of node if any + skipNode(node); } continueNode(node.getParent()); } /** - * Formatting of node continues after completion of a chil node. Establish next skip region. + * Formatting of node continues after completion of a child node. Establish next skip region. * * @param node */ @@ -2851,9 +2854,9 @@ public class CodeFormatterVisitor extends CPPASTVisitor { private void skipNode(IASTNode node) { final IASTNodeLocation fileLocation= node.getFileLocation(); - if (fileLocation != null) { + if (fileLocation != null && fileLocation.getNodeLength() > 0) { final int endOffset= fileLocation.getNodeOffset() + fileLocation.getNodeLength(); - final int currentOffset= scribe.scanner.getCurrentTokenEndPosition() + 1; + final int currentOffset= scribe.scanner.getCurrentPosition(); final int restLength= endOffset - currentOffset; if (restLength > 0) { scribe.printRaw(currentOffset, restLength); @@ -2865,7 +2868,7 @@ public class CodeFormatterVisitor extends CPPASTVisitor { final IASTNodeLocation fileLocation= node.getFileLocation(); if (fileLocation != null) { final int startOffset= fileLocation.getNodeOffset(); - final int currentOffset= scribe.scanner.getCurrentTokenEndPosition() + 1; + final int currentOffset= scribe.scanner.getCurrentPosition(); final int restLength= startOffset - currentOffset; if (restLength > 0) { scribe.printRaw(currentOffset, restLength); @@ -2879,7 +2882,7 @@ public class CodeFormatterVisitor extends CPPASTVisitor { final int startOffset= fileLocation.getNodeOffset(); final int nextTokenOffset= getNextTokenOffset(); if (nextTokenOffset < startOffset) { - final int currentOffset= scribe.scanner.getCurrentTokenEndPosition() + 1; + final int currentOffset= scribe.scanner.getCurrentPosition(); final int restLength= startOffset - currentOffset; if (restLength > 0) { scribe.printRaw(currentOffset, restLength); diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/Scribe.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/Scribe.java index 3166abef146..20c6b1fdfb7 100644 --- a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/Scribe.java +++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/Scribe.java @@ -47,6 +47,7 @@ public class Scribe { // Most specific alignment. public Alignment currentAlignment; public Alignment memberAlignment; + public AlignmentException currentAlignmentException; public Token currentToken; @@ -505,14 +506,14 @@ public class Scribe { relativeDepth++; } if (outerMostDepth >= 0) { - throw new AlignmentException(AlignmentException.LINE_TOO_LONG, outerMostDepth); + throwAlignmentException(AlignmentException.LINE_TOO_LONG, outerMostDepth); } // look for innermost breakable one relativeDepth= 0; targetAlignment= currentAlignment; while (targetAlignment != null) { if (targetAlignment.couldBreak()) { - throw new AlignmentException(AlignmentException.LINE_TOO_LONG, relativeDepth); + throwAlignmentException(AlignmentException.LINE_TOO_LONG, relativeDepth); } targetAlignment= targetAlignment.enclosing; relativeDepth++; @@ -520,6 +521,12 @@ public class Scribe { // did not find any breakable location - proceed } + private void throwAlignmentException(int kind, int relativeDepth) { + AlignmentException e= new AlignmentException(kind, relativeDepth); + currentAlignmentException= e; + throw e; + } + public void indent() { if (shouldSkip(scanner.getCurrentPosition())) { fSkippedIndentations++; @@ -1391,8 +1398,7 @@ public class Scribe { if (e.relativeDepth > 0) { // if exception targets a distinct context e.relativeDepth--; // record fact that current context got // traversed - currentAlignment= currentAlignment.enclosing; // pop - // currentLocation + currentAlignment= currentAlignment.enclosing; // pop currentLocation throw e; // rethrow } // reset scribe/scanner to restart at this given location @@ -1400,6 +1406,7 @@ public class Scribe { scanner.resetTo(currentAlignment.location.inputOffset, scanner.eofPosition - 1); // clean alignment chunkKind so it will think it is a new chunk again currentAlignment.chunkKind= 0; + currentAlignmentException= null; } void redoMemberAlignment(AlignmentException e) { @@ -1408,6 +1415,7 @@ public class Scribe { scanner.resetTo(memberAlignment.location.inputOffset, scanner.eofPosition - 1); // clean alignment chunkKind so it will think it is a new chunk again memberAlignment.chunkKind= 0; + currentAlignmentException= null; } public void reset() { diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java index e53d176c029..fe14f0012b9 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java @@ -37,19 +37,21 @@ import org.eclipse.cdt.internal.formatter.align.Alignment; */ public class CodeFormatterTest extends BaseUITestCase { - private Map fOptions; - private Map fDefaultOptions; + private Map fOptions; + private Map fDefaultOptions; public static TestSuite suite() { return suite(CodeFormatterTest.class, "_"); } + @Override protected void setUp() throws Exception { super.setUp(); fDefaultOptions= DefaultCodeFormatterOptions.getDefaultSettings().getMap(); - fOptions= new HashMap(fDefaultOptions); + fOptions= new HashMap(fDefaultOptions); } + @Override protected void tearDown() throws Exception { super.tearDown(); } @@ -424,7 +426,7 @@ public class CodeFormatterTest extends BaseUITestCase { //namespace X= // Y :: - // Z ; + // Z ; //namespace X = Y::Z; public void testNamespaceAlias() throws Exception { @@ -432,9 +434,9 @@ public class CodeFormatterTest extends BaseUITestCase { } //using - // typename:: T + // typename:: T //; - //using X:: + //using X:: // T ; //using typename ::T; @@ -443,8 +445,8 @@ public class CodeFormatterTest extends BaseUITestCase { assertFormatterResult(); } - //using - // namespace + //using + // namespace // X ; //using namespace X; @@ -500,7 +502,7 @@ public class CodeFormatterTest extends BaseUITestCase { // public virtual BarClass { //}; public void testAlignmentOfClassDefinitionBaseClause1_Bug192656() throws Exception { - fOptions.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_BASE_CLAUSE_IN_TYPE_DECLARATION, + fOptions.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_BASE_CLAUSE_IN_TYPE_DECLARATION, Integer.toString(Alignment.M_ONE_PER_LINE_SPLIT | Alignment.M_FORCE)); assertFormatterResult(); } @@ -511,7 +513,7 @@ public class CodeFormatterTest extends BaseUITestCase { // public virtual BarClass { //}; public void testAlignmentOfClassDefinitionBaseClause2_Bug192656() throws Exception { - fOptions.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_BASE_CLAUSE_IN_TYPE_DECLARATION, + fOptions.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_BASE_CLAUSE_IN_TYPE_DECLARATION, Integer.toString(Alignment.M_NEXT_PER_LINE_SPLIT | Alignment.M_FORCE | Alignment.M_INDENT_ON_COLUMN)); assertFormatterResult(); } @@ -527,7 +529,7 @@ public class CodeFormatterTest extends BaseUITestCase { // throw(int) { //} public void testAlignmentOfExceptionSpecificationInMethodDeclaration_Bug191980() throws Exception { - fOptions.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_THROWS_CLAUSE_IN_METHOD_DECLARATION, + fOptions.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_THROWS_CLAUSE_IN_METHOD_DECLARATION, Integer.toString(Alignment.M_ONE_PER_LINE_SPLIT | Alignment.M_FORCE | Alignment.M_INDENT_BY_ONE)); assertFormatterResult(); } @@ -625,4 +627,13 @@ public class CodeFormatterTest extends BaseUITestCase { fOptions= DefaultCodeFormatterOptions.getWhitesmithsSettings().getMap(); assertFormatterResult(); } + + //typedef int int_; + //int_ const f(int_ const i); + + //typedef int int_; + //int_ const f(int_ const i); + public void testPreserveWhitespaceInParameterDecl_Bug228997() throws Exception { + assertFormatterResult(); + } }