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 5cc81b81fdc..1000370213b 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 @@ -170,7 +170,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, private static class ListOptions { final int fMode; - boolean fUseFallbackMode; + boolean fInsertNewLineBeforeListIfNecessary; boolean fSpaceBeforeComma; boolean fSpaceAfterComma = true; boolean fSpaceAfterOpeningParen; @@ -1457,7 +1457,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, private ListOptions createListOptionsForFunctionDeclarationParameters() { final ListOptions options= new ListOptions(preferences.alignment_for_parameters_in_method_declaration); - options.fUseFallbackMode= true; + options.fInsertNewLineBeforeListIfNecessary= true; options.fSpaceBeforeOpeningParen= preferences.insert_space_before_opening_paren_in_method_declaration; options.fSpaceAfterOpeningParen= preferences.insert_space_after_opening_paren_in_method_declaration; options.fSpaceBeforeClosingParen= preferences.insert_space_before_closing_paren_in_method_declaration; @@ -2003,38 +2003,36 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, if (options.fSpaceAfterOpeningParen) { scribe.space(); } - Alignment retryAlignment = null; + Alignment wrapperAlignment = null; - int fallbackMode = options.fUseFallbackMode ? - getFallbackAlignmentMode(options.fMode) : options.fMode; - if (fallbackMode != options.fMode) { - retryAlignment = scribe.createAlignment( - Alignment.LIST_FALLBACK_TRAP, - Alignment.M_ONE_PER_LINE_SPLIT, + final int continuationIndentation= options.fContinuationIndentation >= 0 ? + options.fContinuationIndentation : preferences.continuation_indentation; + if (options.fInsertNewLineBeforeListIfNecessary && + (options.fMode & Alignment.M_INDENT_ON_COLUMN) != 0) { + wrapperAlignment = scribe.createAlignment( + Alignment.LIST_WRAPPER, + Alignment.M_COMPACT_FIRST_BREAK_SPLIT, Alignment.R_INNERMOST, 1, scribe.scanner.getCurrentPosition(), - 0, + continuationIndentation, false); - scribe.enterAlignment(retryAlignment); + scribe.enterAlignment(wrapperAlignment); } boolean success = false; - int mode = options.fMode; do { - if (retryAlignment != null) - scribe.alignFragment(retryAlignment, 0); + if (wrapperAlignment != null) + scribe.alignFragment(wrapperAlignment, 0); try { - final int continuationIndentation= options.fContinuationIndentation >= 0 ? - options.fContinuationIndentation : preferences.continuation_indentation; Alignment alignment = scribe.createAlignment( Alignment.LIST_ELEMENTS_PREFIX + (elements.isEmpty() ? "ellipsis" : elements.get(0).getClass().getSimpleName()), //$NON-NLS-1$ - mode, + options.fMode, options.fTieBreakRule, elementsLength + (addEllipsis ? 1 : 0), scribe.scanner.getCurrentPosition(), - continuationIndentation, + wrapperAlignment == null ? continuationIndentation : 0, false); scribe.enterAlignment(alignment); boolean ok = false; @@ -2082,27 +2080,18 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, scribe.exitAlignment(alignment, true); success = true; } catch (AlignmentException e) { - if (retryAlignment == null) + if (wrapperAlignment == null) throw e; scribe.redoAlignment(e); } - mode = fallbackMode; - } while (!success); - if (retryAlignment != null) - scribe.exitAlignment(retryAlignment, true); + } while (wrapperAlignment != null && !success); + if (wrapperAlignment != null) + scribe.exitAlignment(wrapperAlignment, true); } else if (tailFormatter != null) { tailFormatter.run(); } } - private int getFallbackAlignmentMode(int alignmentMode) { - switch (alignmentMode & Alignment.SPLIT_MASK) { - case Alignment.M_COMPACT_SPLIT: - alignmentMode = Alignment.M_COMPACT_FIRST_BREAK_SPLIT | (alignmentMode & ~Alignment.SPLIT_MASK); - } - return alignmentMode & ~Alignment.M_INDENT_ON_COLUMN; - } - private int visit(ICPPASTTryBlockStatement node) { scribe.printNextToken(Token.t_try, scribe.printComment()); final IASTStatement tryBody= node.getTryBody(); @@ -2256,7 +2245,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, expressions= Collections.emptyList(); } final ListOptions options= new ListOptions(preferences.alignment_for_arguments_in_method_invocation); - options.fUseFallbackMode= true; + options.fInsertNewLineBeforeListIfNecessary= true; options.fSpaceBeforeOpeningParen= preferences.insert_space_before_opening_paren_in_method_invocation; options.fSpaceAfterOpeningParen= preferences.insert_space_after_opening_paren_in_method_invocation; options.fSpaceBeforeClosingParen= preferences.insert_space_before_closing_paren_in_method_invocation; diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/align/Alignment.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/align/Alignment.java index f3137c74c07..c0a9218b470 100644 --- a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/align/Alignment.java +++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/align/Alignment.java @@ -31,7 +31,7 @@ public class Alignment { public static final String FIELD_REFERENCE = "fieldReference"; //$NON-NLS-1$ public static final String FOR = "for"; //$NON-NLS-1$ public static final String LIST_ELEMENTS_PREFIX = "listElements_"; //$NON-NLS-1$ - public static final String LIST_FALLBACK_TRAP = "listFallbackTrap"; //$NON-NLS-1$ + public static final String LIST_WRAPPER = "listWrapper"; //$NON-NLS-1$ public static final String MACRO_ARGUMENTS = "macroArguments"; //$NON-NLS-1$ public static final String TRAILING_TEXT = "trailingText"; //$NON-NLS-1$ 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 03c402206c9..8cb7927df0a 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 @@ -1080,6 +1080,27 @@ public class CodeFormatterTest extends BaseUITestCase { assertFormatterResult(); } + //void function(int); + //int function_with_a_looooooooooooooooooooooooooooooooong_name(int); + // + //void test() { + //function(function_with_a_looooooooooooooooooooooooooooooooong_name(1000000)); + //} + + //void function(int); + //int function_with_a_looooooooooooooooooooooooooooooooong_name(int); + // + //void test() { + // function(function_with_a_looooooooooooooooooooooooooooooooong_name( + // 1000000)); + //} + public void testFunctionCall_3() 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(); + } + //void function(const char* s); // //void test() {