1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-08 10:16:03 +02:00

Formatter: Fix whitespace issues

This commit is contained in:
Anton Leherbauer 2007-03-01 12:15:55 +00:00
parent 88b72e9a46
commit 8c10e5d244
2 changed files with 32 additions and 24 deletions

View file

@ -408,8 +408,10 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
} }
// common to all declarators // common to all declarators
scribe.printComment();
formatPointers(node.getPointerOperators()); formatPointers(node.getPointerOperators());
if (scribe.printComment()) {
scribe.space();
}
IASTName name= node.getName(); IASTName name= node.getName();
if (name != null) { if (name != null) {
name.accept(this); name.accept(this);
@ -793,6 +795,9 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
private void formatPointers(IASTPointerOperator[] pointers) { private void formatPointers(IASTPointerOperator[] pointers) {
for (int i = 0; i < pointers.length; i++) { for (int i = 0; i < pointers.length; i++) {
IASTPointerOperator pointer= pointers[i]; IASTPointerOperator pointer= pointers[i];
if (scribe.printComment()) {
scribe.space();
}
scribe.printModifiers(); scribe.printModifiers();
if (pointer instanceof ICPPASTReferenceOperator) { if (pointer instanceof ICPPASTReferenceOperator) {
scribe.printNextToken(Token.tAMPER, false); scribe.printNextToken(Token.tAMPER, false);
@ -806,7 +811,7 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
} }
scribe.printNextToken(Token.tSTAR, false); scribe.printNextToken(Token.tSTAR, false);
} else { } else {
skipNode(pointer); formatNode(pointer);
} }
} }
} }
@ -1732,9 +1737,6 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
} }
private void formatNode(IASTNode node) { private void formatNode(IASTNode node) {
if (scribe.printComment()) {
scribe.space();
}
final IASTNodeLocation[] locations= node.getNodeLocations(); final IASTNodeLocation[] locations= node.getNodeLocations();
final IASTNodeLocation minLocation= getMinFileLocation(locations); final IASTNodeLocation minLocation= getMinFileLocation(locations);
if (minLocation != null) { if (minLocation != null) {

View file

@ -90,8 +90,6 @@ public class Scribe {
/** indent empty lines */ /** indent empty lines */
private final boolean indentEmptyLines; private final boolean indentEmptyLines;
private boolean preserveWhitespace;
private boolean preserveNewlines; private boolean preserveNewlines;
private List fSkipPositions= Collections.EMPTY_LIST; private List fSkipPositions= Collections.EMPTY_LIST;
@ -610,21 +608,31 @@ public class Scribe {
} }
public void printRaw(int startOffset, int length) { public void printRaw(int startOffset, int length) {
if (startOffset + length < scanner.getCurrentPosition()) { assert length >= 0;
// safeguard: don't move backwards if (length == 0) {
return;
}
if (startOffset > scanner.getCurrentPosition()) {
printComment();
}
if (pendingSpace) {
addInsertEdit(scanner.getCurrentPosition(), " "); //$NON-NLS-1$
pendingSpace= false;
needSpace= false;
}
if (startOffset + length < scanner.getCurrentPosition()) {
// don't move backwards
return; return;
} }
boolean savedPreserveWS= preserveWhitespace;
boolean savedPreserveNL= preserveNewlines; boolean savedPreserveNL= preserveNewlines;
boolean savedSkipOverInactive= skipOverInactive; boolean savedSkipOverInactive= skipOverInactive;
int savedScannerEndPos= scannerEndPosition; int savedScannerEndPos= scannerEndPosition;
preserveWhitespace= false;
preserveNewlines= true; preserveNewlines= true;
skipOverInactive= false; skipOverInactive= false;
scannerEndPosition= startOffset + length; scannerEndPosition= startOffset + length;
int parenLevel= 0;
try { try {
scanner.resetTo(startOffset, startOffset + length - 1); scanner.resetTo(Math.max(startOffset, scanner.getCurrentPosition()), startOffset + length - 1);
int parenLevel= 0;
while (true) { while (true) {
boolean hasWhitespace= printComment(); boolean hasWhitespace= printComment();
currentToken= scanner.nextToken(); currentToken= scanner.nextToken();
@ -634,6 +642,11 @@ public class Scribe {
} }
break; break;
} }
if (pendingSpace) {
addInsertEdit(scanner.getCurrentTokenStartPosition(), " "); //$NON-NLS-1$
pendingSpace= false;
needSpace= false;
}
switch (currentToken.type) { switch (currentToken.type) {
case Token.tLBRACE: case Token.tLBRACE:
scanner.resetTo(scanner.getCurrentTokenStartPosition(), scannerEndPosition-1); scanner.resetTo(scanner.getCurrentTokenStartPosition(), scannerEndPosition-1);
@ -695,12 +708,12 @@ public class Scribe {
print(currentToken.getLength(), hasWhitespace); print(currentToken.getLength(), hasWhitespace);
} }
} }
hasWhitespace= false;
} }
} finally { } finally {
scannerEndPosition= savedScannerEndPos; scannerEndPosition= savedScannerEndPos;
scanner.resetTo(startOffset + length, scannerEndPosition - 1); scanner.resetTo(startOffset + length, scannerEndPosition - 1);
skipOverInactive= savedSkipOverInactive; skipOverInactive= savedSkipOverInactive;
preserveWhitespace= savedPreserveWS;
preserveNewlines= savedPreserveNL; preserveNewlines= savedPreserveNL;
} }
} }
@ -725,7 +738,6 @@ public class Scribe {
} }
} }
private void print(int length, boolean considerSpaceIfAny) { private void print(int length, boolean considerSpaceIfAny) {
if (checkLineWrapping && length + column > pageWidth) { if (checkLineWrapping && length + column > pageWidth) {
handleLineTooLong(); handleLineTooLong();
@ -739,7 +751,6 @@ public class Scribe {
addInsertEdit(scanner.getCurrentTokenStartPosition(), " "); //$NON-NLS-1$ addInsertEdit(scanner.getCurrentTokenStartPosition(), " "); //$NON-NLS-1$
} }
pendingSpace= false; pendingSpace= false;
needSpace= false;
column+= length; column+= length;
needSpace= true; needSpace= true;
} }
@ -883,6 +894,7 @@ public class Scribe {
int endOffset= Math.min(scannerEndPosition, inactivePos.getOffset() + inactivePos.getLength()); int endOffset= Math.min(scannerEndPosition, inactivePos.getOffset() + inactivePos.getLength());
if (startOffset < endOffset) { if (startOffset < endOffset) {
int savedIndentLevel= indentationLevel; int savedIndentLevel= indentationLevel;
scanner.resetTo(scanner.getCurrentTokenStartPosition(), scanner.eofPosition - 1);
printRaw(startOffset, endOffset - startOffset); printRaw(startOffset, endOffset - startOffset);
while (indentationLevel > savedIndentLevel) { while (indentationLevel > savedIndentLevel) {
unIndent(); unIndent();
@ -914,14 +926,8 @@ public class Scribe {
} }
} }
if (count == 0) { if (count == 0) {
if (preserveWhitespace) { hasWhitespace= true;
addReplaceEdit(scanner.getCurrentTokenStartPosition(), scanner.getCurrentTokenEndPosition(), addDeleteEdit(scanner.getCurrentTokenStartPosition(), scanner.getCurrentTokenEndPosition());
" "); //$NON-NLS-1$
++column;
} else {
hasWhitespace= true;
addDeleteEdit(scanner.getCurrentTokenStartPosition(), scanner.getCurrentTokenEndPosition());
}
} else if (hasComment) { } else if (hasComment) {
if (count == 1) { if (count == 1) {
printNewLine(scanner.getCurrentTokenStartPosition()); printNewLine(scanner.getCurrentTokenStartPosition());