This is an automated email from the ASF dual-hosted git repository. joshtynjala pushed a commit to branch develop in repository https://gitbox.apache.org/repos/asf/royale-compiler.git
commit fe40d93d3a5ffb20651bc20ce66dadab1a3a034c Author: Josh Tynjala <[email protected]> AuthorDate: Mon Sep 18 10:45:29 2023 -0700 ASTokenFormatter: fix formatting issues with comments on same line as control flow that doesn't have curly braces --- .../apache/royale/formatter/ASTokenFormatter.java | 12 +- .../apache/royale/formatter/TestIfStatement.java | 168 +++++++++++++++++++++ 2 files changed, 177 insertions(+), 3 deletions(-) diff --git a/formatter/src/main/java/org/apache/royale/formatter/ASTokenFormatter.java b/formatter/src/main/java/org/apache/royale/formatter/ASTokenFormatter.java index 701f919eb..9676a5b6f 100644 --- a/formatter/src/main/java/org/apache/royale/formatter/ASTokenFormatter.java +++ b/formatter/src/main/java/org/apache/royale/formatter/ASTokenFormatter.java @@ -702,13 +702,18 @@ public class ASTokenFormatter extends BaseTokenFormatter { if (!skipWhitespaceBeforeSemicolon) { numRequiredNewLines = Math.max(numRequiredNewLines, 1); } - } else if (nextToken != null && nextToken.getType() != ASTokenTypes.TOKEN_BLOCK_OPEN - && nextToken.getType() != ASTokenTypes.HIDDEN_TOKEN_SINGLE_LINE_COMMENT + } else if (nextTokenNotComment != null && nextTokenNotComment.getType() != ASTokenTypes.TOKEN_BLOCK_OPEN && !skipWhitespaceBeforeSemicolon) { indent = increaseIndent(indent); BlockStackItem stackItem = blockStack.get(blockStack.size() - 1); stackItem.braces = false; - numRequiredNewLines = Math.max(numRequiredNewLines, 1); + // if a comment is on the same line + // don't add a line break yet + if (nextToken.getType() != ASTokenTypes.HIDDEN_TOKEN_SINGLE_LINE_COMMENT + && nextToken.getType() != ASTokenTypes.HIDDEN_TOKEN_MULTI_LINE_COMMENT + && nextToken.getType() != ASTokenTypes.HIDDEN_TOKEN_COMMENT) { + numRequiredNewLines = Math.max(numRequiredNewLines, 1); + } } } } @@ -1000,6 +1005,7 @@ public class ASTokenFormatter extends BaseTokenFormatter { case ASTokenTypes.HIDDEN_TOKEN_MULTI_LINE_COMMENT: { if (!skipWhitespaceBeforeSemicolon) { if (nextTokenOrExtra != null && nextTokenOrExtra.getType() == TOKEN_TYPE_EXTRA) { + numRequiredNewLines = Math.max(0, countNewLinesInExtra(nextTokenOrExtra)); requiredSpace = true; } } diff --git a/formatter/src/test/java/org/apache/royale/formatter/TestIfStatement.java b/formatter/src/test/java/org/apache/royale/formatter/TestIfStatement.java index e1c957385..470bae98c 100644 --- a/formatter/src/test/java/org/apache/royale/formatter/TestIfStatement.java +++ b/formatter/src/test/java/org/apache/royale/formatter/TestIfStatement.java @@ -535,6 +535,174 @@ public class TestIfStatement extends BaseFormatterTests { result); } + @Test + public void testNestedBodiesWithoutParenthesesWithSingleLineComment1() { + FormatterSettings settings = new FormatterSettings(); + settings.insertSpaceAfterKeywordsInControlFlowStatements = true; + settings.placeOpenBraceOnNewLine = true; + settings.insertSpaces = false; + ASTokenFormatter formatter = new ASTokenFormatter(settings); + String result = formatter.format("file.as", + // @formatter:off + "{\n" + + "\tif (condition1) // comment\n" + + "\t\tif (condition2)\n" + + "\t\t\tstatement;\n" + + "}", + // @formatter:on + problems + ); + assertEquals( + // @formatter:off + "{\n" + + "\tif (condition1) // comment\n" + + "\t\tif (condition2)\n" + + "\t\t\tstatement;\n" + + "}", + // @formatter:on + result); + } + + @Test + public void testNestedBodiesWithoutParenthesesWithSingleLineComment2() { + FormatterSettings settings = new FormatterSettings(); + settings.insertSpaceAfterKeywordsInControlFlowStatements = true; + settings.placeOpenBraceOnNewLine = true; + settings.insertSpaces = false; + ASTokenFormatter formatter = new ASTokenFormatter(settings); + String result = formatter.format("file.as", + // @formatter:off + "{\n" + + "\tif (condition1)\n" + + "\t\tif (condition2) // comment\n" + + "\t\t\tstatement;\n" + + "}", + // @formatter:on + problems + ); + assertEquals( + // @formatter:off + "{\n" + + "\tif (condition1)\n" + + "\t\tif (condition2) // comment\n" + + "\t\t\tstatement;\n" + + "}", + // @formatter:on + result); + } + + @Test + public void testNestedBodiesWithoutParenthesesWithSingleLineComment3() { + FormatterSettings settings = new FormatterSettings(); + settings.insertSpaceAfterKeywordsInControlFlowStatements = true; + settings.placeOpenBraceOnNewLine = true; + settings.insertSpaces = false; + ASTokenFormatter formatter = new ASTokenFormatter(settings); + String result = formatter.format("file.as", + // @formatter:off + "{\n" + + "\tif (condition1) // comment\n" + + "\t\tif (condition2) // comment\n" + + "\t\t\tstatement;\n" + + "}", + // @formatter:on + problems + ); + assertEquals( + // @formatter:off + "{\n" + + "\tif (condition1) // comment\n" + + "\t\tif (condition2) // comment\n" + + "\t\t\tstatement;\n" + + "}", + // @formatter:on + result); + } + + @Test + public void testNestedBodiesWithoutParenthesesWithMultiLineComment1() { + FormatterSettings settings = new FormatterSettings(); + settings.insertSpaceAfterKeywordsInControlFlowStatements = true; + settings.placeOpenBraceOnNewLine = true; + settings.insertSpaces = false; + ASTokenFormatter formatter = new ASTokenFormatter(settings); + String result = formatter.format("file.as", + // @formatter:off + "{\n" + + "\tif (condition1) /* comment */\n" + + "\t\tif (condition2)\n" + + "\t\t\tstatement;\n" + + "}", + // @formatter:on + problems + ); + assertEquals( + // @formatter:off + "{\n" + + "\tif (condition1) /* comment */\n" + + "\t\tif (condition2)\n" + + "\t\t\tstatement;\n" + + "}", + // @formatter:on + result); + } + + @Test + public void testNestedBodiesWithoutParenthesesWithMultiLineComment2() { + FormatterSettings settings = new FormatterSettings(); + settings.insertSpaceAfterKeywordsInControlFlowStatements = true; + settings.placeOpenBraceOnNewLine = true; + settings.insertSpaces = false; + ASTokenFormatter formatter = new ASTokenFormatter(settings); + String result = formatter.format("file.as", + // @formatter:off + "{\n" + + "\tif (condition1)\n" + + "\t\tif (condition2) /* comment */\n" + + "\t\t\tstatement;\n" + + "}", + // @formatter:on + problems + ); + assertEquals( + // @formatter:off + "{\n" + + "\tif (condition1)\n" + + "\t\tif (condition2) /* comment */\n" + + "\t\t\tstatement;\n" + + "}", + // @formatter:on + result); + } + + @Test + public void testNestedBodiesWithoutParenthesesWithMultiLineComment3() { + FormatterSettings settings = new FormatterSettings(); + settings.insertSpaceAfterKeywordsInControlFlowStatements = true; + settings.placeOpenBraceOnNewLine = true; + settings.insertSpaces = false; + ASTokenFormatter formatter = new ASTokenFormatter(settings); + String result = formatter.format("file.as", + // @formatter:off + "{\n" + + "\tif (condition1) /* comment */\n" + + "\t\tif (condition2) /* comment */\n" + + "\t\t\tstatement;\n" + + "}", + // @formatter:on + problems + ); + assertEquals( + // @formatter:off + "{\n" + + "\tif (condition1) /* comment */\n" + + "\t\tif (condition2) /* comment */\n" + + "\t\t\tstatement;\n" + + "}", + // @formatter:on + result); + } + @Test public void testCollapseEmptyBlock1() { FormatterSettings settings = new FormatterSettings();
