1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-08 18:26:01 +02:00

Bug 268962: Code Formatter removes necessary whitespaces

This commit is contained in:
Anton Leherbauer 2009-03-18 11:26:27 +00:00
parent 195a008c99
commit 7086a42dea
2 changed files with 50 additions and 27 deletions

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2000, 2008 IBM Corporation and others. * Copyright (c) 2000, 2009 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
@ -367,7 +367,7 @@ public class Scribe {
} }
public String getEmptyLines(int linesNumber) { public String getEmptyLines(int linesNumber) {
StringBuffer buffer= new StringBuffer(); StringBuilder buffer= new StringBuilder();
if (lastNumberOfNewLines == 0) { if (lastNumberOfNewLines == 0) {
linesNumber++; // add an extra line breaks linesNumber++; // add an extra line breaks
for (int i= 0; i < linesNumber; i++) { for (int i= 0; i < linesNumber; i++) {
@ -823,7 +823,7 @@ public class Scribe {
column= 1; column= 1;
line++; line++;
StringBuffer buffer= new StringBuffer(); StringBuilder buffer= new StringBuilder();
buffer.append(lineSeparator); buffer.append(lineSeparator);
printIndentationIfNecessary(buffer); printIndentationIfNecessary(buffer);
buffer.append(' '); buffer.append(' ');
@ -1115,7 +1115,7 @@ public class Scribe {
} }
void printIndentationIfNecessary() { void printIndentationIfNecessary() {
StringBuffer buffer= new StringBuffer(); StringBuilder buffer= new StringBuilder();
printIndentationIfNecessary(buffer); printIndentationIfNecessary(buffer);
if (buffer.length() > 0) { if (buffer.length() > 0) {
addInsertEdit(scanner.getCurrentTokenStartPosition(), buffer.toString()); addInsertEdit(scanner.getCurrentTokenStartPosition(), buffer.toString());
@ -1123,7 +1123,7 @@ public class Scribe {
} }
} }
private void printIndentationIfNecessary(StringBuffer buffer) { private void printIndentationIfNecessary(StringBuilder buffer) {
switch (tabChar) { switch (tabChar) {
case DefaultCodeFormatterOptions.TAB: case DefaultCodeFormatterOptions.TAB:
boolean useTabsForLeadingIndents= useTabsOnlyForLeadingIndents; boolean useTabsForLeadingIndents= useTabsOnlyForLeadingIndents;
@ -1252,6 +1252,11 @@ public class Scribe {
} }
currentToken= scanner.nextToken(); currentToken= scanner.nextToken();
if (currentToken == null || expectedTokenType != currentToken.type) { if (currentToken == null || expectedTokenType != currentToken.type) {
if (pendingSpace) {
addInsertEdit(scanner.getCurrentTokenStartPosition(), SPACE);
}
pendingSpace= false;
needSpace= true;
throw new AbortFormatting( throw new AbortFormatting(
"[" + (line+1) + "/" + column + "] unexpected token type, expecting:" + expectedTokenType + ", actual:" + currentToken);//$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ "[" + (line+1) + "/" + column + "] unexpected token type, expecting:" + expectedTokenType + ", actual:" + currentToken);//$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
} }
@ -1269,7 +1274,12 @@ public class Scribe {
} }
currentToken= scanner.nextToken(); currentToken= scanner.nextToken();
if (Arrays.binarySearch(expectedTokenTypes, currentToken.type) < 0) { if (Arrays.binarySearch(expectedTokenTypes, currentToken.type) < 0) {
StringBuffer expectations= new StringBuffer(5); if (pendingSpace) {
addInsertEdit(scanner.getCurrentTokenStartPosition(), SPACE);
}
pendingSpace= false;
needSpace= true;
StringBuilder expectations= new StringBuilder(5);
for (int i= 0; i < expectedTokenTypes.length; i++) { for (int i= 0; i < expectedTokenTypes.length; i++) {
if (i > 0) { if (i > 0) {
expectations.append(','); expectations.append(',');
@ -1282,7 +1292,7 @@ public class Scribe {
print(currentToken.getLength(), considerSpaceIfAny); print(currentToken.getLength(), considerSpaceIfAny);
} }
private void printRule(StringBuffer stringBuffer) { private void printRule(StringBuilder stringBuffer) {
for (int i= 0; i < pageWidth; i++) { for (int i= 0; i < pageWidth; i++) {
if ((i % tabLength) == 0) { if ((i % tabLength) == 0) {
stringBuffer.append('+'); stringBuffer.append('+');
@ -1450,19 +1460,19 @@ public class Scribe {
@Override @Override
public String toString() { public String toString() {
StringBuffer stringBuffer= new StringBuffer(); StringBuilder buffer= new StringBuilder();
stringBuffer.append("(page width = " + pageWidth + ") - (tabChar = ");//$NON-NLS-1$//$NON-NLS-2$ buffer.append("(page width = " + pageWidth + ") - (tabChar = ");//$NON-NLS-1$//$NON-NLS-2$
switch (tabChar) { switch (tabChar) {
case DefaultCodeFormatterOptions.TAB: case DefaultCodeFormatterOptions.TAB:
stringBuffer.append("TAB");//$NON-NLS-1$ buffer.append("TAB");//$NON-NLS-1$
break; break;
case DefaultCodeFormatterOptions.SPACE: case DefaultCodeFormatterOptions.SPACE:
stringBuffer.append("SPACE");//$NON-NLS-1$ buffer.append("SPACE");//$NON-NLS-1$
break; break;
default: default:
stringBuffer.append("MIXED");//$NON-NLS-1$ buffer.append("MIXED");//$NON-NLS-1$
} }
stringBuffer buffer
.append(") - (tabSize = " + tabLength + ")")//$NON-NLS-1$//$NON-NLS-2$ .append(") - (tabSize = " + tabLength + ")")//$NON-NLS-1$//$NON-NLS-2$
.append(lineSeparator) .append(lineSeparator)
.append( .append(
@ -1473,8 +1483,8 @@ public class Scribe {
.append(lineSeparator).append( .append(lineSeparator).append(
"==================================================================================") //$NON-NLS-1$ "==================================================================================") //$NON-NLS-1$
.append(lineSeparator); .append(lineSeparator);
printRule(stringBuffer); printRule(buffer);
return stringBuffer.toString(); return buffer.toString();
} }
public void unIndent() { public void unIndent() {

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2007, 2008 Wind River Systems, Inc. and others. * Copyright (c) 2007, 2009 Wind River Systems, Inc. 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
@ -1144,4 +1144,17 @@ public class CodeFormatterTest extends BaseUITestCase {
assertFormatterResult(); assertFormatterResult();
} }
//#define X
//
//typedef X struct {
//};
//#define X
//
//typedef X struct {
//};
public void testPreserveNecessarySpace_Bug268962() throws Exception {
assertFormatterResult();
}
} }