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 db4bc58ec388cf2ec017d48ed5931f69a2910220 Author: Josh Tynjala <[email protected]> AuthorDate: Thu Jul 14 10:54:41 2022 -0700 formatter: handle disable place brace on new line for case or default that contains only a block does not increase indent of block --- .../org/apache/royale/formatter/FORMATTER.java | 15 +- .../royale/formatter/TestSwitchStatement.java | 202 ++++++++++++++++++++- 2 files changed, 204 insertions(+), 13 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 8c6c75328..35c0d04a0 100644 --- a/formatter/src/main/java/org/apache/royale/formatter/FORMATTER.java +++ b/formatter/src/main/java/org/apache/royale/formatter/FORMATTER.java @@ -1250,19 +1250,20 @@ public class FORMATTER { || afterBlockClose.getType() == ASTokenTypes.TOKEN_KEYWORD_CASE || afterBlockClose.getType() == ASTokenTypes.TOKEN_KEYWORD_DEFAULT) { blockOpenPending = true; + requiredSpace = true; blockStack.remove(blockStack.size() - 1); } } } if (!nextIsBlock || !blockOpenPending) { indent = increaseIndent(indent); - } - 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); + 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--; 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 d7cd956e7..2615958f7 100644 --- a/formatter/src/test/java/org/apache/royale/formatter/TestSwitchStatement.java +++ b/formatter/src/test/java/org/apache/royale/formatter/TestSwitchStatement.java @@ -125,7 +125,7 @@ public class TestSwitchStatement extends BaseFormatterTests { } @Test - public void testWithCaseClauseAndEmptyBlock() { + public void testWithCaseClauseAndEmptyBlockEnablePlaceOpenBraceOnNewLine() { FORMATTER formatter = new FORMATTER(); formatter.insertSpaceAfterKeywordsInControlFlowStatements = true; formatter.placeOpenBraceOnNewLine = true; @@ -154,7 +154,34 @@ public class TestSwitchStatement extends BaseFormatterTests { } @Test - public void testWithCaseClauseAndBlockWithStatement() { + public void testWithCaseClauseAndEmptyBlockDisablePlaceOpenBraceOnNewLine() { + FORMATTER formatter = new FORMATTER(); + formatter.insertSpaceAfterKeywordsInControlFlowStatements = true; + formatter.placeOpenBraceOnNewLine = false; + formatter.insertSpaces = false; + String result = formatter.formatActionScriptText( + // @formatter:off + "switch (condition)\n" + + "{\n" + + "\tcase condition:\n" + + "\t{\n" + + "\t}\n" + + "}", + // @formatter:on + problems + ); + assertEquals( + // @formatter:off + "switch (condition) {\n" + + "\tcase condition: {\n" + + "\t}\n" + + "}", + // @formatter:on + result); + } + + @Test + public void testWithCaseClauseAndBlockWithStatementEnablePlaceOpenBraceOnNewLine() { FORMATTER formatter = new FORMATTER(); formatter.insertSpaceAfterKeywordsInControlFlowStatements = true; formatter.placeOpenBraceOnNewLine = true; @@ -185,7 +212,36 @@ public class TestSwitchStatement extends BaseFormatterTests { } @Test - public void testWithCaseClauseAndStatementAfterBlock() { + public void testWithCaseClauseAndBlockWithStatementDisablePlaceOpenBraceOnNewLine() { + FORMATTER formatter = new FORMATTER(); + formatter.insertSpaceAfterKeywordsInControlFlowStatements = true; + formatter.placeOpenBraceOnNewLine = false; + formatter.insertSpaces = false; + String result = formatter.formatActionScriptText( + // @formatter:off + "switch (condition)\n" + + "{\n" + + "\tcase condition:\n" + + "\t{\n" + + "\t\tbreak;\n" + + "\t}\n" + + "}", + // @formatter:on + problems + ); + assertEquals( + // @formatter:off + "switch (condition) {\n" + + "\tcase condition: {\n" + + "\t\tbreak;\n" + + "\t}\n" + + "}", + // @formatter:on + result); + } + + @Test + public void testWithCaseClauseAndStatementAfterBlockEnablePlaceOpenBraceOnNewLine() { FORMATTER formatter = new FORMATTER(); formatter.insertSpaceAfterKeywordsInControlFlowStatements = true; formatter.placeOpenBraceOnNewLine = true; @@ -205,6 +261,9 @@ public class TestSwitchStatement extends BaseFormatterTests { ); assertEquals( // @formatter:off + // notice that the break statement is outside the curly braces, + // so the curly braces aren't considered a "body" for the case + // clause and they get indented an extra level. "switch (condition)\n" + "{\n" + "\tcase condition:\n" + @@ -217,6 +276,42 @@ public class TestSwitchStatement extends BaseFormatterTests { result); } + @Test + public void testWithCaseClauseAndStatementAfterBlockDisablePlaceOpenBraceOnNewLine() { + FORMATTER formatter = new FORMATTER(); + formatter.insertSpaceAfterKeywordsInControlFlowStatements = true; + formatter.placeOpenBraceOnNewLine = false; + formatter.insertSpaces = false; + String result = formatter.formatActionScriptText( + // @formatter:off + "switch (condition)\n" + + "{\n" + + "\tcase condition:\n" + + "\t{\n" + + "\t\tstatement;\n" + + "\t}\n" + + "\tbreak;\n" + + "}", + // @formatter:on + problems + ); + assertEquals( + // @formatter:off + // notice that the break statement is outside the curly braces, + // so the curly braces aren't considered a "body" for the case + // clause, and they get indented an extra level. the opening + // brace also doesn't appear on the same line as the case. + "switch (condition) {\n" + + "\tcase condition:\n" + + "\t\t{\n" + + "\t\t\tstatement;\n" + + "\t\t}\n" + + "\t\tbreak;\n" + + "}", + // @formatter:on + result); + } + @Test public void testWithDefaultClauseAndStatement() { FORMATTER formatter = new FORMATTER(); @@ -274,7 +369,7 @@ public class TestSwitchStatement extends BaseFormatterTests { } @Test - public void testWithDefaultClauseAndEmptyBlock() { + public void testWithDefaultClauseAndEmptyBlockEnablePlaceOpenBraceOnNewLine() { FORMATTER formatter = new FORMATTER(); formatter.insertSpaceAfterKeywordsInControlFlowStatements = true; formatter.placeOpenBraceOnNewLine = true; @@ -303,7 +398,34 @@ public class TestSwitchStatement extends BaseFormatterTests { } @Test - public void testWithDefaultClauseAndBlockWithStatement() { + public void testWithDefaultClauseAndEmptyBlockDisablePlaceOpenBraceOnNewLine() { + FORMATTER formatter = new FORMATTER(); + formatter.insertSpaceAfterKeywordsInControlFlowStatements = true; + formatter.placeOpenBraceOnNewLine = false; + formatter.insertSpaces = false; + String result = formatter.formatActionScriptText( + // @formatter:off + "switch (condition)\n" + + "{\n" + + "\tdefault:\n" + + "\t{\n" + + "\t}\n" + + "}", + // @formatter:on + problems + ); + assertEquals( + // @formatter:off + "switch (condition) {\n" + + "\tdefault: {\n" + + "\t}\n" + + "}", + // @formatter:on + result); + } + + @Test + public void testWithDefaultClauseAndBlockWithStatementEnablePlaceOpenBraceOnNewLine() { FORMATTER formatter = new FORMATTER(); formatter.insertSpaceAfterKeywordsInControlFlowStatements = true; formatter.placeOpenBraceOnNewLine = true; @@ -334,7 +456,36 @@ public class TestSwitchStatement extends BaseFormatterTests { } @Test - public void testWithDefaultClauseAndStatementAfterBlock() { + public void testWithDefaultClauseAndBlockWithStatementDisablePlaceOpenBraceOnNewLine() { + FORMATTER formatter = new FORMATTER(); + formatter.insertSpaceAfterKeywordsInControlFlowStatements = true; + formatter.placeOpenBraceOnNewLine = false; + formatter.insertSpaces = false; + String result = formatter.formatActionScriptText( + // @formatter:off + "switch (condition)\n" + + "{\n" + + "\tdefault:\n" + + "\t{\n" + + "\t\tbreak;\n" + + "\t}\n" + + "}", + // @formatter:on + problems + ); + assertEquals( + // @formatter:off + "switch (condition) {\n" + + "\tdefault: {\n" + + "\t\tbreak;\n" + + "\t}\n" + + "}", + // @formatter:on + result); + } + + @Test + public void testWithDefaultClauseAndStatementAfterBlockEnablePlaceOpenBraceOnNewLine() { FORMATTER formatter = new FORMATTER(); formatter.insertSpaceAfterKeywordsInControlFlowStatements = true; formatter.placeOpenBraceOnNewLine = true; @@ -354,6 +505,9 @@ public class TestSwitchStatement extends BaseFormatterTests { ); assertEquals( // @formatter:off + // notice that the break statement is outside the curly braces, + // so the curly braces aren't considered a "body" for the + // default clause and they get indented an extra level. "switch (condition)\n" + "{\n" + "\tdefault:\n" + @@ -366,6 +520,42 @@ public class TestSwitchStatement extends BaseFormatterTests { result); } + @Test + public void testWithDefaultClauseAndStatementAfterBlockDisablePlaceOpenBraceOnNewLine() { + FORMATTER formatter = new FORMATTER(); + formatter.insertSpaceAfterKeywordsInControlFlowStatements = true; + formatter.placeOpenBraceOnNewLine = false; + formatter.insertSpaces = false; + String result = formatter.formatActionScriptText( + // @formatter:off + "switch (condition)\n" + + "{\n" + + "\tdefault:\n" + + "\t{\n" + + "\t\tstatement;\n" + + "\t}\n" + + "\tbreak;\n" + + "}", + // @formatter:on + problems + ); + assertEquals( + // @formatter:off + // notice that the break statement is outside the curly braces, + // so the curly braces aren't considered a "body" for the + // default clause and they get indented an extra level. the + // opening brace also doesn't appear on the same line as default. + "switch (condition) {\n" + + "\tdefault:\n" + + "\t\t{\n" + + "\t\t\tstatement;\n" + + "\t\t}\n" + + "\t\tbreak;\n" + + "}", + // @formatter:on + result); + } + @Test public void testWithMultipleCaseClauses() { FORMATTER formatter = new FORMATTER();
