You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@royale.apache.org by jo...@apache.org on 2021/11/22 23:11:36 UTC
[royale-compiler] 03/04: formatter: whitespace between block close and comments
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 0e872b57a86bbcd237df48b8ae5135ad282bcd11
Author: Josh Tynjala <jo...@apache.org>
AuthorDate: Mon Nov 22 14:50:14 2021 -0800
formatter: whitespace between block close and comments
---
.../org/apache/royale/formatter/FORMATTER.java | 17 +++++--
.../royale/formatter/TestMultiLineComment.java | 26 +++++++++++
.../royale/formatter/TestSingleLineComment.java | 26 +++++++++++
.../royale/formatter/TestSwitchStatement.java | 52 ++++++++++++++++++++++
4 files changed, 118 insertions(+), 3 deletions(-)
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 775be8a..cddc59f 100644
--- a/formatter/src/main/java/org/apache/royale/formatter/FORMATTER.java
+++ b/formatter/src/main/java/org/apache/royale/formatter/FORMATTER.java
@@ -972,7 +972,11 @@ public class FORMATTER {
&& token.getType() != ASTokenTypes.TOKEN_BLOCK_OPEN) {
blockOpenPending = false;
}
- caseOrDefaultBlockOpenPending = false;
+ if (token.getType() != ASTokenTypes.HIDDEN_TOKEN_SINGLE_LINE_COMMENT
+ && token.getType() != ASTokenTypes.HIDDEN_TOKEN_MULTI_LINE_COMMENT
+ && token.getType() != ASTokenTypes.TOKEN_ASDOC_COMMENT) {
+ caseOrDefaultBlockOpenPending = false;
+ }
requiredSpace = false;
numRequiredNewLines = 0;
if (token instanceof MetaDataPayloadToken) {
@@ -1041,7 +1045,9 @@ public class FORMATTER {
if (!(item instanceof ObjectLiteralBlockStackItem)
&& (nextToken == null || (nextToken.getType() != ASTokenTypes.TOKEN_SEMICOLON
&& nextToken.getType() != ASTokenTypes.TOKEN_PAREN_CLOSE
- && nextToken.getType() != ASTokenTypes.TOKEN_COMMA))) {
+ && nextToken.getType() != ASTokenTypes.TOKEN_COMMA
+ && nextToken.getType() != ASTokenTypes.HIDDEN_TOKEN_SINGLE_LINE_COMMENT
+ && nextToken.getType() != ASTokenTypes.HIDDEN_TOKEN_MULTI_LINE_COMMENT))) {
numRequiredNewLines = Math.max(numRequiredNewLines, 1);
}
}
@@ -1173,7 +1179,12 @@ public class FORMATTER {
inCaseOrDefaultClause = false;
caseOrDefaultBlockOpenPending = true;
indent = increaseIndent(indent);
- numRequiredNewLines = Math.max(numRequiredNewLines, 1);
+ if (nextToken != null && (nextToken.getType() == ASTokenTypes.HIDDEN_TOKEN_SINGLE_LINE_COMMENT
+ || nextToken.getType() == ASTokenTypes.HIDDEN_TOKEN_MULTI_LINE_COMMENT)) {
+ requiredSpace = true;
+ } else {
+ numRequiredNewLines = Math.max(numRequiredNewLines, 1);
+ }
} else if (ternaryStack > 0) {
ternaryStack--;
requiredSpace = true;
diff --git a/formatter/src/test/java/org/apache/royale/formatter/TestMultiLineComment.java b/formatter/src/test/java/org/apache/royale/formatter/TestMultiLineComment.java
index c55a310..6d32a02 100644
--- a/formatter/src/test/java/org/apache/royale/formatter/TestMultiLineComment.java
+++ b/formatter/src/test/java/org/apache/royale/formatter/TestMultiLineComment.java
@@ -108,4 +108,30 @@ public class TestMultiLineComment extends BaseFormatterTests {
result);
}
+ @Test
+ public void testAtEndOfBlock() {
+ FORMATTER formatter = new FORMATTER();
+ formatter.insertSpaceAfterKeywordsInControlFlowStatements = true;
+ formatter.placeOpenBraceOnNewLine = true;
+ formatter.insertSpaces = false;
+ formatter.insertSpaceAtStartOfLineComment = true;
+ String result = formatter.formatActionScriptText(
+ // @formatter:off
+ "{\n" +
+ "\tstatement;\n" +
+ "} /* this is a comment */\n" +
+ "statement;",
+ // @formatter:on
+ problems
+ );
+ assertEquals(
+ // @formatter:off
+ "{\n" +
+ "\tstatement;\n" +
+ "} /* this is a comment */\n" +
+ "statement;",
+ // @formatter:on
+ result);
+ }
+
}
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 10e26f2..4c20606 100644
--- a/formatter/src/test/java/org/apache/royale/formatter/TestSingleLineComment.java
+++ b/formatter/src/test/java/org/apache/royale/formatter/TestSingleLineComment.java
@@ -178,4 +178,30 @@ public class TestSingleLineComment extends BaseFormatterTests {
result);
}
+ @Test
+ public void testAtEndOfBlock() {
+ FORMATTER formatter = new FORMATTER();
+ formatter.insertSpaceAfterKeywordsInControlFlowStatements = true;
+ formatter.placeOpenBraceOnNewLine = true;
+ formatter.insertSpaces = false;
+ formatter.insertSpaceAtStartOfLineComment = true;
+ String result = formatter.formatActionScriptText(
+ // @formatter:off
+ "{\n" +
+ "\tstatement;\n" +
+ "} // this is a comment\n" +
+ "statement;",
+ // @formatter:on
+ problems
+ );
+ assertEquals(
+ // @formatter:off
+ "{\n" +
+ "\tstatement;\n" +
+ "} // this is a comment\n" +
+ "statement;",
+ // @formatter:on
+ result);
+ }
+
}
diff --git a/formatter/src/test/java/org/apache/royale/formatter/TestSwitchStatement.java b/formatter/src/test/java/org/apache/royale/formatter/TestSwitchStatement.java
index d5a7677..dffb116 100644
--- a/formatter/src/test/java/org/apache/royale/formatter/TestSwitchStatement.java
+++ b/formatter/src/test/java/org/apache/royale/formatter/TestSwitchStatement.java
@@ -699,4 +699,56 @@ public class TestSwitchStatement extends BaseFormatterTests {
// @formatter:on
result);
}
+
+ @Test
+ public void testCommentOnSameLineAsCaseClause() {
+ FORMATTER formatter = new FORMATTER();
+ formatter.insertSpaceAfterKeywordsInControlFlowStatements = true;
+ formatter.placeOpenBraceOnNewLine = true;
+ formatter.insertSpaces = false;
+ String result = formatter.formatActionScriptText(
+ // @formatter:off
+ "switch (condition)\n" +
+ "{\n" +
+ "\tcase clause://what\n" +
+ "\t\tbreak;\n" +
+ "}",
+ // @formatter:on
+ problems
+ );
+ assertEquals(
+ // @formatter:off
+ "switch (condition)\n" +
+ "{\n" +
+ "\tcase clause: // what\n" +
+ "\t\tbreak;\n" +
+ "}",
+ // @formatter:on
+ result);
+ }
+
+ @Test
+ public void testCommentOnSameLineAsDefaultClause() {
+ FORMATTER formatter = new FORMATTER();
+ formatter.insertSpaceAfterKeywordsInControlFlowStatements = true;
+ formatter.placeOpenBraceOnNewLine = true;
+ formatter.insertSpaces = false;
+ String result = formatter.formatActionScriptText(
+ // @formatter:off
+ "switch (condition)\n" +
+ "{\n" +
+ "\tdefault://what\n" +
+ "}",
+ // @formatter:on
+ problems
+ );
+ assertEquals(
+ // @formatter:off
+ "switch (condition)\n" +
+ "{\n" +
+ "\tdefault: // what\n" +
+ "}",
+ // @formatter:on
+ result);
+ }
}