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 8cc1eb756e9..c11b9330715 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 @@ -211,10 +211,6 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, spaceBeforeToken, spaceAfterToken); } - TrailingTokenFormatter(int tokenType, boolean spaceBeforeToken, boolean spaceAfterToken) { - this(tokenType, scribe.findToken(tokenType), spaceBeforeToken, spaceAfterToken); - } - public void run() { int offset = scribe.scanner.getCurrentPosition(); if (tokenPosition < 0 || offset > tokenPosition) @@ -2092,6 +2088,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, if (i < elementsLength - 1) { scribe.setTailFormatter( new TrailingTokenFormatter(options.fSeparatorToken, + findTokenAfterNode(options.fSeparatorToken, node), options.fSpaceBeforeSeparator, options.fSpaceAfterSeparator)); } else { @@ -4336,4 +4333,10 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, int endOffset = location.getNodeOffset() + location.getNodeLength(); return scribe.findToken(tokenType, endOffset); } + + private int findTokenAfterNode(int tokenType, IASTNode node) { + IASTFileLocation location = node.getFileLocation(); + int startOffset = location.getNodeOffset() + location.getNodeLength(); + return scribe.findToken(tokenType, startOffset, scribe.scannerEndPosition - 1); + } } 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 60b56c51a4f..eb1dd7f967c 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 @@ -1926,7 +1926,7 @@ public class Scribe { * The scanner position is left unchanged. * * @param tokenType type of the token to look for - * @return true if a matching token was found + * @return the position of the matching token, if found, otherwise -1. */ public int findToken(int tokenType) { return findToken(tokenType, scannerEndPosition - 1); @@ -1939,7 +1939,7 @@ public class Scribe { * * @param tokenType type of the token to look for * @param endPosition end position limiting the search - * @return true if a matching token was found + * @return the position of the matching token, if found, otherwise -1. */ public int findToken(int tokenType, int endPosition) { int startPosition= scanner.getCurrentPosition(); @@ -2002,6 +2002,26 @@ public class Scribe { return -1; } + /** + * Searches for the next occurrence of the given token type. + * If successful, returns the offset of the found token, otherwise -1. + * The scanner position is left unchanged. + * + * @param tokenType type of the token to look for + * @param startPosition position where to start the search + * @param endPosition end position limiting the search + * @return the position of the matching token, if found, otherwise -1. + */ + public int findToken(int tokenType, int startPosition, int endPosition) { + int currentPosition= scanner.getCurrentPosition(); + try { + scanner.resetTo(startPosition, scannerEndPosition - 1); + return findToken(tokenType, endPosition); + } finally { + scanner.resetTo(currentPosition, scannerEndPosition - 1); + } + } + public boolean printCommentPreservingNewLines() { final boolean savedPreserveNL= preserveNewLines; preserveNewLines= true; 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 35feb483ca8..5e8a10a05e7 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 @@ -1176,6 +1176,48 @@ public class CodeFormatterTest extends BaseUITestCase { assertFormatterResult(); } + //int function_with_a_long_name(int, int); + //int function_with_an_even_looooooooooooooooonger_name(int, int); + // + //void test() { + //function_with_a_long_name(function_with_an_even_looooooooooooooooonger_name(1000000,2000000),3000000); + //function_with_a_long_name(function_with_an_even_looooooooooooooooonger_name(1000000,20000000),3000000); + //} + + //int function_with_a_long_name(int, int); + //int function_with_an_even_looooooooooooooooonger_name(int, int); + // + //void test() { + // function_with_a_long_name( + // function_with_an_even_looooooooooooooooonger_name(1000000, 2000000), + // 3000000); + // function_with_a_long_name( + // function_with_an_even_looooooooooooooooonger_name(1000000, + // 20000000), 3000000); + //} + public void testFunctionCall_4() throws Exception { + fOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, CCorePlugin.SPACE); + assertFormatterResult(); + } + + //template + //struct type_with_multiple_template_parameters {}; + // + //void wrap_when_necessary(type_with_multiple_template_parameters p1, int p2, int p3) { + //} + + //template + //struct type_with_multiple_template_parameters { + //}; + // + //void wrap_when_necessary(type_with_multiple_template_parameters p1, + // int p2, int p3) { + //} + public void testFunctionCallWithTemplates_Bug357300() throws Exception { + fOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, CCorePlugin.SPACE); + assertFormatterResult(); + } + //void function(const char* s); // //void test() {