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 256a0456f8c95b2a039937c7913ecf8d85584b0b Author: Josh Tynjala <[email protected]> AuthorDate: Wed Oct 6 10:25:50 2021 -0700 formatter: fix missing new line after single line comment --- .../org/apache/royale/formatter/FORMATTER.java | 6 +++++- .../royale/formatter/TestSingleLineComment.java | 25 ++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/formatter/src/main/java/org/apache/royale/formatter/FORMATTER.java b/formatter/src/main/java/org/apache/royale/formatter/FORMATTER.java index 6eed027..a93fc3f 100644 --- a/formatter/src/main/java/org/apache/royale/formatter/FORMATTER.java +++ b/formatter/src/main/java/org/apache/royale/formatter/FORMATTER.java @@ -550,7 +550,11 @@ class FORMATTER { break; } if (!blockOpenPending) { - numRequiredNewLines = Math.max(numRequiredNewLines, countNewLinesInExtra(token)); + int newLinesInExtra = countNewLinesInExtra(token); + if(prevToken != null && prevToken.getType() == ASTokenTypes.HIDDEN_TOKEN_SINGLE_LINE_COMMENT) { + newLinesInExtra++; + } + numRequiredNewLines = Math.max(numRequiredNewLines, newLinesInExtra); if (!indentedStatement && numRequiredNewLines > 0 && prevTokenNotComment != null && prevTokenNotComment.getType() != ASTokenTypes.TOKEN_SEMICOLON && prevTokenNotComment.getType() != ASTokenTypes.TOKEN_BLOCK_CLOSE diff --git a/formatter/src/test/java/org/apache/royale/formatter/TestSingleLineComment.java b/formatter/src/test/java/org/apache/royale/formatter/TestSingleLineComment.java index 7d752be..98f8202 100644 --- a/formatter/src/test/java/org/apache/royale/formatter/TestSingleLineComment.java +++ b/formatter/src/test/java/org/apache/royale/formatter/TestSingleLineComment.java @@ -106,6 +106,31 @@ public class TestSingleLineComment extends BaseFormatterTests { } @Test + public void testWithExtraLineBeforeStatement() { + FORMATTER formatter = new FORMATTER(); + formatter.insertSpaceAfterKeywordsInControlFlowStatements = true; + formatter.placeOpenBraceOnNewLine = true; + formatter.insertSpaces = false; + formatter.insertSpaceAtStartOfLineComment = true; + formatter.maxPreserveNewLines = 2; + String result = formatter.formatText( + // @formatter:off + "// this is a comment\n" + + "\n" + + "statement;", + // @formatter:on + problems + ); + assertEquals( + // @formatter:off + "// this is a comment\n" + + "\n" + + "statement;", + // @formatter:on + result); + } + + @Test public void testOnLineAfterStatement() { FORMATTER formatter = new FORMATTER(); formatter.insertSpaceAfterKeywordsInControlFlowStatements = true;
