1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-23 14:42:11 +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
scribe.printComment();
formatPointers(node.getPointerOperators());
if (scribe.printComment()) {
scribe.space();
}
IASTName name= node.getName();
if (name != null) {
name.accept(this);
@ -793,6 +795,9 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
private void formatPointers(IASTPointerOperator[] pointers) {
for (int i = 0; i < pointers.length; i++) {
IASTPointerOperator pointer= pointers[i];
if (scribe.printComment()) {
scribe.space();
}
scribe.printModifiers();
if (pointer instanceof ICPPASTReferenceOperator) {
scribe.printNextToken(Token.tAMPER, false);
@ -806,7 +811,7 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
}
scribe.printNextToken(Token.tSTAR, false);
} else {
skipNode(pointer);
formatNode(pointer);
}
}
}
@ -1732,9 +1737,6 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
}
private void formatNode(IASTNode node) {
if (scribe.printComment()) {
scribe.space();
}
final IASTNodeLocation[] locations= node.getNodeLocations();
final IASTNodeLocation minLocation= getMinFileLocation(locations);
if (minLocation != null) {

View file

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