mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-29 19:45:01 +02:00
More code formatter heuristics and stay Eclipse 3.2 compatible.
This commit is contained in:
parent
b3495a8044
commit
67654f8f4f
3 changed files with 62 additions and 35 deletions
|
@ -211,7 +211,15 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
|
|||
|
||||
private final TextEdit failedToFormat(AbortFormatting e) {
|
||||
if (DEBUG) {
|
||||
CCorePlugin.log("Could not format: " + e.getMessage());
|
||||
String errorMessage= e.getMessage();
|
||||
if (errorMessage == null) {
|
||||
if (e.nestedException != null) {
|
||||
errorMessage= e.nestedException.getClass().getName();
|
||||
} else {
|
||||
errorMessage= "Unknown error";
|
||||
}
|
||||
}
|
||||
CCorePlugin.log(CCorePlugin.createStatus("Could not format: " + errorMessage, e.nestedException));
|
||||
System.out.println("COULD NOT FORMAT: " + e.getMessage());
|
||||
System.out.println(scribe.scanner); //$NON-NLS-1$
|
||||
System.out.println(scribe);
|
||||
|
@ -930,6 +938,15 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
|
|||
int i;
|
||||
for (i = 0; i < elementsLength; i++) {
|
||||
if (i > 0) {
|
||||
// handle missing parameter
|
||||
int token= peekNextToken();
|
||||
if (token == Token.tIDENTIFIER) {
|
||||
if (!scribe.skipToToken(Token.tCOMMA)) {
|
||||
break;
|
||||
}
|
||||
} else if (token == Token.tRPAREN) {
|
||||
break;
|
||||
}
|
||||
scribe.printNextToken(Token.tCOMMA, align.fSpaceBeforeComma);
|
||||
scribe.printTrailingComment();
|
||||
}
|
||||
|
@ -956,12 +973,13 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
|
|||
}
|
||||
} while (!ok);
|
||||
scribe.exitAlignment(listAlignment, true);
|
||||
|
||||
if (encloseInParen)
|
||||
scribe.printNextToken(Token.tRPAREN, align.fSpaceBeforeClosingParen);
|
||||
} else {
|
||||
if (encloseInParen)
|
||||
scribe.printNextToken(Token.tRPAREN, align.fSpaceBeforeClosingParen);
|
||||
}
|
||||
if (encloseInParen) {
|
||||
// handle missing parameter
|
||||
if (peekNextToken() == Token.tIDENTIFIER) {
|
||||
scribe.skipToToken(Token.tRPAREN);
|
||||
}
|
||||
scribe.printNextToken(Token.tRPAREN, align.fSpaceBeforeClosingParen);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1529,6 +1547,13 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
|
|||
}
|
||||
|
||||
private void formatBlock(IASTCompoundStatement block, String block_brace_position, boolean insertSpaceBeforeOpeningBrace, boolean indentStatements) {
|
||||
IASTNodeLocation[] locations= block.getNodeLocations();
|
||||
if (locations.length == 0) {
|
||||
return;
|
||||
} else if (locations[0] instanceof IASTMacroExpansion) {
|
||||
formatNode(block);
|
||||
return;
|
||||
}
|
||||
formatOpeningBrace(block_brace_position, insertSpaceBeforeOpeningBrace);
|
||||
IASTStatement[] statements = block.getStatements();
|
||||
final int statementsLength = statements.length;
|
||||
|
@ -1648,7 +1673,7 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
|
|||
}
|
||||
|
||||
protected int peekNextToken() {
|
||||
localScanner.resetTo(scribe.scanner.startPosition, scribe.scannerEndPosition - 1);
|
||||
localScanner.resetTo(scribe.scanner.getCurrentPosition(), scribe.scannerEndPosition - 1);
|
||||
return localScanner.getNextToken();
|
||||
}
|
||||
|
||||
|
@ -1674,6 +1699,16 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
|
|||
}
|
||||
|
||||
private boolean isGuardClause(IASTCompoundStatement block, List statements) {
|
||||
IASTNodeLocation[] locations= block.getNodeLocations();
|
||||
if (locations.length == 0) {
|
||||
return false;
|
||||
} else if (locations[0] instanceof IASTMacroExpansion) {
|
||||
return false;
|
||||
}
|
||||
IASTNodeLocation fileLocation= block.getFileLocation();
|
||||
if (fileLocation == null) {
|
||||
return false;
|
||||
}
|
||||
int blockStartPosition= block.getFileLocation().getNodeOffset();
|
||||
int blockLength= block.getFileLocation().getNodeLength();
|
||||
if (commentStartsBlock(blockStartPosition, blockLength)) return false;
|
||||
|
|
|
@ -629,9 +629,6 @@ public class Scribe {
|
|||
indent();
|
||||
}
|
||||
formatOpeningBrace(formatter.preferences.brace_position_for_block, hasWhitespace);
|
||||
// print(currentToken.getLength(), hasWhitespace);
|
||||
// printNewLine();
|
||||
// indent();
|
||||
break;
|
||||
case Token.tRBRACE:
|
||||
scanner.resetTo(scanner.getCurrentTokenStartPosition(), scannerEndPosition-1);
|
||||
|
@ -639,9 +636,6 @@ public class Scribe {
|
|||
unIndent();
|
||||
}
|
||||
formatClosingBrace(formatter.preferences.brace_position_for_block);
|
||||
// printNewLine(scanner.getCurrentTokenStartPosition());
|
||||
// unIndent();
|
||||
// print(currentToken.getLength(), hasWhitespace);
|
||||
break;
|
||||
case Token.tLPAREN:
|
||||
print(currentToken.getLength(), hasWhitespace);
|
||||
|
@ -1606,14 +1600,14 @@ public class Scribe {
|
|||
}
|
||||
|
||||
/**
|
||||
* Skip to the next occurrence one of the given token types.
|
||||
* If successful, the next token will be one of the epxected tokens,
|
||||
* Skip to the next occurrence of the given token type.
|
||||
* If successful, the next token will be the epxected token,
|
||||
* otherwise the scanner position is left unchanged.
|
||||
*
|
||||
* @param expectedTokenTypes
|
||||
* @param expectedTokenType
|
||||
* @return <code>true</code> if a matching token was skipped to
|
||||
*/
|
||||
public boolean skipToToken(int[] expectedTokenTypes) {
|
||||
public boolean skipToToken(int expectedTokenType) {
|
||||
int skipStart= scanner.getCurrentPosition();
|
||||
int braceLevel= 0;
|
||||
int parenLevel= 0;
|
||||
|
@ -1639,22 +1633,20 @@ public class Scribe {
|
|||
case Token.tPREPROCESSOR_INCLUDE:
|
||||
continue;
|
||||
}
|
||||
if (braceLevel <= 0 && parenLevel == 0) {
|
||||
for (int i= 0; i < expectedTokenTypes.length; i++) {
|
||||
if (currentToken.type == expectedTokenTypes[i]) {
|
||||
printRaw(skipStart, scanner.getCurrentTokenEndPosition() - skipStart + 1);
|
||||
scanner.resetTo(scanner.getCurrentTokenEndPosition() + 1, scannerEndPosition - 1);
|
||||
return true;
|
||||
}
|
||||
if (braceLevel <= 0 && parenLevel <= 0) {
|
||||
if (currentToken.type == expectedTokenType) {
|
||||
int tokenStart= scanner.getCurrentTokenStartPosition();
|
||||
printRaw(skipStart, tokenStart - skipStart);
|
||||
scanner.resetTo(tokenStart, scannerEndPosition - 1);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (braceLevel < 0 || parenLevel < 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
scanner.resetTo(skipStart, scannerEndPosition - 1);
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean skipToToken(int expectedTokenType) {
|
||||
return skipToToken(new int[] { expectedTokenType });
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -334,7 +334,7 @@ public abstract class ModifyDialog extends StatusDialog {
|
|||
|
||||
private void doValidate() {
|
||||
if (!hasChanges()) {
|
||||
updateStatus(new Status(IStatus.ERROR, CUIPlugin.PLUGIN_ID, "")); //$NON-NLS-1$
|
||||
updateStatus(new Status(IStatus.ERROR, CUIPlugin.PLUGIN_ID, 0, "", null)); //$NON-NLS-1$
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -346,12 +346,12 @@ public abstract class ModifyDialog extends StatusDialog {
|
|||
|
||||
String name= fProfileNameField.getText().trim();
|
||||
if (!name.equals(fProfile.getName()) && fProfileManager.containsName(name)) {
|
||||
updateStatus(new Status(IStatus.ERROR, CUIPlugin.PLUGIN_ID, FormatterMessages.ModifyDialog_Duplicate_Status));
|
||||
updateStatus(new Status(IStatus.ERROR, CUIPlugin.PLUGIN_ID, 0, FormatterMessages.ModifyDialog_Duplicate_Status, null));
|
||||
return;
|
||||
}
|
||||
|
||||
if (fProfile.isBuiltInProfile() || fProfile.isSharedProfile()) {
|
||||
updateStatus(new Status(IStatus.INFO, CUIPlugin.PLUGIN_ID, FormatterMessages.ModifyDialog_NewCreated_Status));
|
||||
updateStatus(new Status(IStatus.INFO, CUIPlugin.PLUGIN_ID, 0, FormatterMessages.ModifyDialog_NewCreated_Status, null));
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -363,18 +363,18 @@ public abstract class ModifyDialog extends StatusDialog {
|
|||
|
||||
if (fProfile.isBuiltInProfile()) {
|
||||
if (fProfile.getName().equals(name)) {
|
||||
return new Status(IStatus.ERROR, CUIPlugin.PLUGIN_ID, FormatterMessages.ModifyDialog_BuiltIn_Status);
|
||||
return new Status(IStatus.ERROR, CUIPlugin.PLUGIN_ID, 0, FormatterMessages.ModifyDialog_BuiltIn_Status, null);
|
||||
}
|
||||
}
|
||||
|
||||
if (fProfile.isSharedProfile()) {
|
||||
if (fProfile.getName().equals(name)) {
|
||||
return new Status(IStatus.ERROR, CUIPlugin.PLUGIN_ID, FormatterMessages.ModifyDialog_Shared_Status);
|
||||
return new Status(IStatus.ERROR, CUIPlugin.PLUGIN_ID, 0, FormatterMessages.ModifyDialog_Shared_Status, null);
|
||||
}
|
||||
}
|
||||
|
||||
if (name.length() == 0) {
|
||||
return new Status(IStatus.ERROR, CUIPlugin.PLUGIN_ID, FormatterMessages.ModifyDialog_EmptyName_Status);
|
||||
return new Status(IStatus.ERROR, CUIPlugin.PLUGIN_ID, 0, FormatterMessages.ModifyDialog_EmptyName_Status, null);
|
||||
}
|
||||
|
||||
return StatusInfo.OK_STATUS;
|
||||
|
|
Loading…
Add table
Reference in a new issue