mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-07-31 12:55:40 +02:00
Fix formatter issues with string literal concatenation
This commit is contained in:
parent
1bc6b14d20
commit
cb14d91f76
4 changed files with 56 additions and 6 deletions
|
@ -1246,7 +1246,7 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
|
||||||
final List initializers = Arrays.asList(node.getInitializers());
|
final List initializers = Arrays.asList(node.getInitializers());
|
||||||
if (initializers.isEmpty() && preferences.keep_empty_array_initializer_on_one_line) {
|
if (initializers.isEmpty() && preferences.keep_empty_array_initializer_on_one_line) {
|
||||||
scribe.printNextToken(Token.tLBRACE, preferences.insert_space_before_opening_brace_in_array_initializer);
|
scribe.printNextToken(Token.tLBRACE, preferences.insert_space_before_opening_brace_in_array_initializer);
|
||||||
scribe.printNextToken(Token.tLBRACE, preferences.insert_space_between_empty_braces_in_array_initializer);
|
scribe.printNextToken(Token.tRBRACE, preferences.insert_space_between_empty_braces_in_array_initializer);
|
||||||
} else {
|
} else {
|
||||||
final int line= scribe.line;
|
final int line= scribe.line;
|
||||||
final String brace_position= preferences.brace_position_for_array_initializer;
|
final String brace_position= preferences.brace_position_for_array_initializer;
|
||||||
|
@ -1301,7 +1301,39 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
|
||||||
}
|
}
|
||||||
|
|
||||||
private int visit(IASTLiteralExpression node) {
|
private int visit(IASTLiteralExpression node) {
|
||||||
scribe.printNextToken(peekNextToken(), scribe.printComment());
|
if (node.getKind() == IASTLiteralExpression.lk_string_literal) {
|
||||||
|
// handle concatentation of string literals
|
||||||
|
if (node.getNodeLocations().length > 1) {
|
||||||
|
// cannot handle embedded macros
|
||||||
|
skipNode(node);
|
||||||
|
} else {
|
||||||
|
int token;
|
||||||
|
boolean needSpace= false;
|
||||||
|
final int line= scribe.line;
|
||||||
|
boolean indented= false;
|
||||||
|
try {
|
||||||
|
while (true) {
|
||||||
|
scribe.printCommentPreservingNewLines();
|
||||||
|
scribe.printNextToken(Token.tSTRING, needSpace);
|
||||||
|
token= peekNextToken();
|
||||||
|
if (token != Token.tSTRING) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
needSpace= true;
|
||||||
|
if (!indented && line != scribe.line) {
|
||||||
|
indented= true;
|
||||||
|
scribe.indent();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
if (indented) {
|
||||||
|
scribe.unIndent();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
scribe.printNextToken(peekNextToken(), scribe.printComment());
|
||||||
|
}
|
||||||
return PROCESS_SKIP;
|
return PROCESS_SKIP;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -608,8 +608,7 @@ public class Scribe {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void printRaw(int startOffset, int length) {
|
public void printRaw(int startOffset, int length) {
|
||||||
assert length >= 0;
|
if (length <= 0) {
|
||||||
if (length == 0) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (startOffset > scanner.getCurrentPosition()) {
|
if (startOffset > scanner.getCurrentPosition()) {
|
||||||
|
@ -873,8 +872,9 @@ public class Scribe {
|
||||||
|
|
||||||
public void printEndOfTranslationUnit() {
|
public void printEndOfTranslationUnit() {
|
||||||
int currentTokenStartPosition= scanner.getCurrentPosition();
|
int currentTokenStartPosition= scanner.getCurrentPosition();
|
||||||
printRaw(currentTokenStartPosition, scannerEndPosition - currentTokenStartPosition + 1);
|
if (currentTokenStartPosition <= scannerEndPosition) {
|
||||||
return;
|
printRaw(currentTokenStartPosition, scannerEndPosition - currentTokenStartPosition + 1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean printComment() {
|
public boolean printComment() {
|
||||||
|
@ -1582,4 +1582,14 @@ public class Scribe {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean printCommentPreservingNewLines() {
|
||||||
|
final boolean savedPreserveNL= this.preserveNewlines;
|
||||||
|
this.preserveNewlines= true;
|
||||||
|
try {
|
||||||
|
return printComment();
|
||||||
|
} finally {
|
||||||
|
this.preserveNewlines= savedPreserveNL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,3 +23,7 @@ AClass::AClass(int x) throw(int) :
|
||||||
// keep space between decl spec and declarator
|
// keep space between decl spec and declarator
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
}
|
}
|
||||||
|
// handling of string concat
|
||||||
|
char* s1= "this " "is " "one " "string.";
|
||||||
|
char* s2= "this " "is "
|
||||||
|
"one " "string.";
|
||||||
|
|
|
@ -11,3 +11,7 @@ AClass::AClass(int x)throw(int):ABaseClass(x){for (int i=0;i < 12;i++) {}}
|
||||||
int
|
int
|
||||||
main(int argc, char **argv) {
|
main(int argc, char **argv) {
|
||||||
}
|
}
|
||||||
|
// handling of string concat
|
||||||
|
char* s1= "this " "is " "one ""string.";
|
||||||
|
char* s2= "this " "is "
|
||||||
|
"one " "string.";
|
||||||
|
|
Loading…
Add table
Reference in a new issue