From 2a00a3219cf4fd104b24a392fb1a108fd5c3286f Mon Sep 17 00:00:00 2001 From: Sergey Prigogin Date: Tue, 15 Mar 2011 17:06:31 +0000 Subject: [PATCH] Formatting of multi-line string literals in function argument lists. --- .../formatter/CodeFormatterVisitor.java | 22 ++++++++++++++----- .../cdt/internal/formatter/Scribe.java | 2 +- .../cdt/ui/tests/text/CodeFormatterTest.java | 20 +++++++++++++++++ 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java index 63c1a1f6af8..16fcb75356a 100644 --- a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java +++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java @@ -2660,8 +2660,10 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, boolean needSpace= false; final int line= scribe.line; boolean indented= false; + int indentationLevel = scribe.indentationLevel; + int numberOfIndentations = scribe.numberOfIndentations; try { - int[] stringLiterals = { Token.tSTRING, Token.tLSTRING, Token.tRSTRING }; + final int[] stringLiterals = { Token.tSTRING, Token.tLSTRING, Token.tRSTRING }; while (true) { scribe.printNextToken(stringLiterals, needSpace); token= peekNextToken(); @@ -2670,15 +2672,23 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, } scribe.printCommentPreservingNewLines(); if (!indented && line != scribe.line) { - indented= true; - scribe.indentForContinuation(); + Alignment alignment = scribe.currentAlignment; + if (alignment != null && (alignment.mode & Alignment.M_INDENT_ON_COLUMN) != 0) { + scribe.indentationLevel= alignment.breakIndentationLevel; + } else if (alignment != null && (alignment.mode & Alignment.M_INDENT_BY_ONE) != 0) { + indented = true; + scribe.indent(); + } else { + indented = true; + scribe.indentForContinuation(); + } } needSpace= true; } } finally { - if (indented) { - scribe.unIndentForContinuation(); - } + // Restore indentation. + scribe.indentationLevel = indentationLevel; + scribe.numberOfIndentations = numberOfIndentations; } } else { scribe.printNextToken(peekNextToken()); diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/Scribe.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/Scribe.java index 631b7ca9aff..4ee541ec591 100644 --- a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/Scribe.java +++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/Scribe.java @@ -1472,7 +1472,7 @@ public class Scribe { buffer.append('\t'); indentationsAsTab++; int complement= tabLength - ((column - 1) % tabLength); // amount of space - column+= complement; + column += complement; needSpace= false; } else { buffer.append(' '); diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java index b8d64d61a26..b0a1b5b7e83 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java @@ -1059,6 +1059,26 @@ public class CodeFormatterTest extends BaseUITestCase { assertFormatterResult(); } + //void function(const char* s); + // + //void test() { + //function("string literal" + //"continuation of the string literal"); + //} + + //void function(const char* s); + // + //void test() { + // function("string literal" + // "continuation of the string literal"); + //} + public void testFunctionCallWithMultilineStringLiteral() throws Exception { + fOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, CCorePlugin.SPACE); + fOptions.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ARGUMENTS_IN_METHOD_INVOCATION, + Integer.toString(Alignment.M_COMPACT_SPLIT | Alignment.M_INDENT_ON_COLUMN)); + assertFormatterResult(); + } + //#define MY_MACRO int a; \ // int b; \ // int c();