mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-29 19:45:01 +02:00
Improve formatter heuristic
This commit is contained in:
parent
d5061c1162
commit
b11cd377d4
3 changed files with 44 additions and 129 deletions
|
@ -253,6 +253,9 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
|
||||||
if (i < decls.length - 1) {
|
if (i < decls.length - 1) {
|
||||||
exitAlignments();
|
exitAlignments();
|
||||||
skipToNode(decls[i+1]);
|
skipToNode(decls[i+1]);
|
||||||
|
while (scribe.indentationLevel < indentLevel) {
|
||||||
|
scribe.indent();
|
||||||
|
}
|
||||||
while (scribe.indentationLevel > indentLevel) {
|
while (scribe.indentationLevel > indentLevel) {
|
||||||
scribe.unIndent();
|
scribe.unIndent();
|
||||||
}
|
}
|
||||||
|
|
|
@ -615,24 +615,31 @@ public class Scribe {
|
||||||
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(startOffset, startOffset + length - 1);
|
||||||
while (true) {
|
while (true) {
|
||||||
boolean hasWhitespace= printComment();
|
boolean hasWhitespace= printComment();
|
||||||
if (currentToken == null) {
|
if (currentToken == null) {
|
||||||
|
if (hasWhitespace) {
|
||||||
|
space();
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
currentToken= scanner.nextToken();
|
currentToken= scanner.nextToken();
|
||||||
if (currentToken == null) {
|
if (currentToken == null) {
|
||||||
|
if (hasWhitespace) {
|
||||||
|
space();
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
switch (currentToken.type) {
|
switch (currentToken.type) {
|
||||||
case Token.tLBRACE:
|
case Token.tLBRACE:
|
||||||
scanner.resetTo(scanner.getCurrentTokenStartPosition(), scannerEndPosition-1);
|
scanner.resetTo(scanner.getCurrentTokenStartPosition(), scannerEndPosition-1);
|
||||||
|
formatOpeningBrace(formatter.preferences.brace_position_for_block, hasWhitespace);
|
||||||
if (formatter.preferences.indent_statements_compare_to_block) {
|
if (formatter.preferences.indent_statements_compare_to_block) {
|
||||||
indent();
|
indent();
|
||||||
}
|
}
|
||||||
formatOpeningBrace(formatter.preferences.brace_position_for_block, hasWhitespace);
|
|
||||||
break;
|
break;
|
||||||
case Token.tRBRACE:
|
case Token.tRBRACE:
|
||||||
scanner.resetTo(scanner.getCurrentTokenStartPosition(), scannerEndPosition-1);
|
scanner.resetTo(scanner.getCurrentTokenStartPosition(), scannerEndPosition-1);
|
||||||
|
@ -642,22 +649,37 @@ public class Scribe {
|
||||||
formatClosingBrace(formatter.preferences.brace_position_for_block);
|
formatClosingBrace(formatter.preferences.brace_position_for_block);
|
||||||
break;
|
break;
|
||||||
case Token.tLPAREN:
|
case Token.tLPAREN:
|
||||||
|
++parenLevel;
|
||||||
print(currentToken.getLength(), hasWhitespace);
|
print(currentToken.getLength(), hasWhitespace);
|
||||||
|
if (parenLevel > 0) {
|
||||||
for (int i= 0; i < formatter.preferences.continuation_indentation; i++) {
|
for (int i= 0; i < formatter.preferences.continuation_indentation; i++) {
|
||||||
indent();
|
indent();
|
||||||
}
|
}
|
||||||
// HACK: avoid indent in same line
|
// HACK: avoid indent in same line
|
||||||
column= indentationLevel + 1;
|
column= indentationLevel + 1;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case Token.tRPAREN:
|
case Token.tRPAREN:
|
||||||
|
--parenLevel;
|
||||||
|
if (parenLevel >= 0) {
|
||||||
for (int i= 0; i < formatter.preferences.continuation_indentation; i++) {
|
for (int i= 0; i < formatter.preferences.continuation_indentation; i++) {
|
||||||
unIndent();
|
unIndent();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
print(currentToken.getLength(), hasWhitespace);
|
print(currentToken.getLength(), hasWhitespace);
|
||||||
break;
|
break;
|
||||||
case Token.tSEMI:
|
case Token.tSEMI:
|
||||||
print(currentToken.getLength(), formatter.preferences.insert_space_before_semicolon);
|
print(currentToken.getLength(), formatter.preferences.insert_space_before_semicolon);
|
||||||
break;
|
break;
|
||||||
|
case Token.t_else:
|
||||||
|
case Token.t_catch:
|
||||||
|
if (formatter.preferences.insert_new_line_before_else_in_if_statement) {
|
||||||
|
printNewLine(currentToken.offset);
|
||||||
|
} else {
|
||||||
|
hasWhitespace= true;
|
||||||
|
}
|
||||||
|
print(currentToken.getLength(), hasWhitespace);
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
if (currentToken.isVisibilityModifier()
|
if (currentToken.isVisibilityModifier()
|
||||||
&& !formatter.preferences.indent_access_specifier_compare_to_type_header) {
|
&& !formatter.preferences.indent_access_specifier_compare_to_type_header) {
|
||||||
|
@ -838,120 +860,10 @@ public class Scribe {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void printEndOfTranslationUnit() {
|
public void printEndOfTranslationUnit() {
|
||||||
// if we have a space between two tokens we ensure it will be dumped in
|
|
||||||
// the formatted string
|
|
||||||
int currentTokenStartPosition= scanner.getCurrentPosition();
|
int currentTokenStartPosition= scanner.getCurrentPosition();
|
||||||
boolean hasComment= false;
|
printRaw(currentTokenStartPosition, scannerEndPosition - currentTokenStartPosition + 1);
|
||||||
boolean hasLineComment= false;
|
|
||||||
boolean hasWhitespace= false;
|
|
||||||
int count= 0;
|
|
||||||
while (true) {
|
|
||||||
currentToken= scanner.nextToken();
|
|
||||||
if (currentToken == null) {
|
|
||||||
if (count >= 1 || formatter.preferences.insert_new_line_at_end_of_file_if_missing) {
|
|
||||||
printNewLine(scannerEndPosition);
|
|
||||||
}
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
switch (currentToken.type) {
|
|
||||||
case Token.tWHITESPACE:
|
|
||||||
char[] whiteSpaces= scanner.getCurrentTokenSource();
|
|
||||||
count= 0;
|
|
||||||
for (int i= 0, max= whiteSpaces.length; i < max; i++) {
|
|
||||||
switch (whiteSpaces[i]) {
|
|
||||||
case '\r':
|
|
||||||
if ((i + 1) < max) {
|
|
||||||
if (whiteSpaces[i + 1] == '\n') {
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
count++;
|
|
||||||
break;
|
|
||||||
case '\n':
|
|
||||||
count++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (count == 0) {
|
|
||||||
hasWhitespace= true;
|
|
||||||
addDeleteEdit(scanner.getCurrentTokenStartPosition(), scanner.getCurrentTokenEndPosition());
|
|
||||||
} else if (hasComment) {
|
|
||||||
if (count == 1) {
|
|
||||||
printNewLine(scanner.getCurrentTokenStartPosition());
|
|
||||||
} else {
|
|
||||||
preserveEmptyLines(count - 1, scanner.getCurrentTokenStartPosition());
|
|
||||||
}
|
|
||||||
addDeleteEdit(scanner.getCurrentTokenStartPosition(), scanner.getCurrentTokenEndPosition());
|
|
||||||
} else if (hasLineComment) {
|
|
||||||
preserveEmptyLines(count - 1, scanner.getCurrentTokenStartPosition());
|
|
||||||
addDeleteEdit(scanner.getCurrentTokenStartPosition(), scanner.getCurrentTokenEndPosition());
|
|
||||||
} else {
|
|
||||||
addDeleteEdit(scanner.getCurrentTokenStartPosition(), scanner.getCurrentTokenEndPosition());
|
|
||||||
}
|
|
||||||
currentTokenStartPosition= scanner.getCurrentPosition();
|
|
||||||
break;
|
|
||||||
case Token.tLINECOMMENT:
|
|
||||||
if (count >= 1) {
|
|
||||||
if (count > 1) {
|
|
||||||
preserveEmptyLines(count - 1, scanner.getCurrentTokenStartPosition());
|
|
||||||
} else if (count == 1) {
|
|
||||||
printNewLine(scanner.getCurrentTokenStartPosition());
|
|
||||||
}
|
|
||||||
} else if (hasWhitespace) {
|
|
||||||
space();
|
|
||||||
}
|
|
||||||
hasWhitespace= false;
|
|
||||||
printCommentLine();
|
|
||||||
currentTokenStartPosition= scanner.getCurrentPosition();
|
|
||||||
hasLineComment= true;
|
|
||||||
count= 0;
|
|
||||||
break;
|
|
||||||
case Token.tBLOCKCOMMENT:
|
|
||||||
if (count >= 1) {
|
|
||||||
if (count > 1) {
|
|
||||||
preserveEmptyLines(count - 1, scanner.getCurrentTokenStartPosition());
|
|
||||||
} else if (count == 1) {
|
|
||||||
printNewLine(scanner.getCurrentTokenStartPosition());
|
|
||||||
}
|
|
||||||
} else if (hasWhitespace) {
|
|
||||||
space();
|
|
||||||
}
|
|
||||||
hasWhitespace= false;
|
|
||||||
printBlockComment(false);
|
|
||||||
currentTokenStartPosition= scanner.getCurrentPosition();
|
|
||||||
hasLineComment= false;
|
|
||||||
hasComment= true;
|
|
||||||
count= 0;
|
|
||||||
break;
|
|
||||||
case Token.tPREPROCESSOR:
|
|
||||||
case Token.tPREPROCESSOR_DEFINE:
|
|
||||||
case Token.tPREPROCESSOR_INCLUDE:
|
|
||||||
if (column != 1)
|
|
||||||
printNewLine(scanner.getCurrentTokenStartPosition());
|
|
||||||
if (count >= 1) {
|
|
||||||
if (count > 1) {
|
|
||||||
preserveEmptyLines(count - 1, scanner.getCurrentTokenStartPosition());
|
|
||||||
} else if (count == 1) {
|
|
||||||
// printNewLine(scanner.getCurrentTokenStartPosition());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
hasWhitespace= false;
|
|
||||||
printPreprocessorDirective();
|
|
||||||
printNewLine();
|
|
||||||
currentTokenStartPosition= scanner.getCurrentPosition();
|
|
||||||
hasLineComment= false;
|
|
||||||
hasComment= false;
|
|
||||||
count= 0;
|
|
||||||
break;
|
|
||||||
case Token.tSEMI:
|
|
||||||
print(currentToken.getLength(), formatter.preferences.insert_space_before_semicolon);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
// step back one token
|
|
||||||
scanner.resetTo(currentTokenStartPosition, scannerEndPosition - 1);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean printComment() {
|
public boolean printComment() {
|
||||||
// if we have a space between two tokens we ensure it will be dumped in
|
// if we have a space between two tokens we ensure it will be dumped in
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
#include <Simple.h>
|
#include <Simple.h>
|
||||||
|
|
||||||
const SimpleStruct simpleStruct =
|
const SimpleStruct simpleStruct =
|
||||||
{
|
{
|
||||||
1
|
1
|
||||||
, "mySimple"
|
, "mySimple"
|
||||||
, 0.1232
|
, 0.1232
|
||||||
|
@ -16,7 +16,7 @@ const SimpleStruct simpleStruct =
|
||||||
}
|
}
|
||||||
|
|
||||||
const OtherStruct array[] =
|
const OtherStruct array[] =
|
||||||
{
|
{
|
||||||
{
|
{
|
||||||
#if FOO
|
#if FOO
|
||||||
"foo"
|
"foo"
|
||||||
|
@ -39,17 +39,17 @@ const OtherStruct array[] =
|
||||||
|
|
||||||
void SimpleStruct_construct(
|
void SimpleStruct_construct(
|
||||||
struct SimpleStruct * const this )
|
struct SimpleStruct * const this )
|
||||||
{
|
{
|
||||||
// single line
|
// single line
|
||||||
this->num = 1;
|
this->num = 1;
|
||||||
this->name = "boo";
|
this->name = "boo";
|
||||||
this->floatNum = 1.5;
|
this->floatNum = 1.5;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ConnectParams_doSomething(const struct SimpleStruct * const this )
|
int ConnectParams_doSomething( const struct SimpleStruct * const this )
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
* multiline
|
* multiline
|
||||||
*/
|
*/
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue