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 aad583419ff2c2250a9c5770149a170a4e6467e0 Author: Josh Tynjala <[email protected]> AuthorDate: Thu Jul 14 10:25:39 2022 -0700 formatter: case or default that contains only a block does not increase indent of block --- .../org/apache/royale/formatter/FORMATTER.java | 49 ++++++++++++++++++---- .../royale/formatter/TestSwitchStatement.java | 40 +++++++++--------- 2 files changed, 61 insertions(+), 28 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 473ff7cb4..fc36d0989 100644 --- a/formatter/src/main/java/org/apache/royale/formatter/FORMATTER.java +++ b/formatter/src/main/java/org/apache/royale/formatter/FORMATTER.java @@ -91,11 +91,10 @@ public class FORMATTER { } final int code; - - int getCode() - { - return code; - } + + int getCode() { + return code; + } } /** @@ -393,7 +392,8 @@ public class FORMATTER { } } - private String formatMXMLScriptElement(String filePath, int line, String text, Collection<ICompilerProblem> problems) { + private String formatMXMLScriptElement(String filePath, int line, String text, + Collection<ICompilerProblem> problems) { String indent = "\t"; if (insertSpaces) { indent = ""; @@ -761,7 +761,8 @@ public class FORMATTER { indentedStatement = false; indent = decreaseIndent(indent); } - boolean oneLineBlock = nextToken != null && nextToken.getType() == ASTokenTypes.TOKEN_BLOCK_CLOSE; + boolean oneLineBlock = nextToken != null + && nextToken.getType() == ASTokenTypes.TOKEN_BLOCK_CLOSE; boolean needsNewLine = placeOpenBraceOnNewLine && (!collapseEmptyBlocks || !oneLineBlock); if (needsNewLine) { numRequiredNewLines = Math.max(numRequiredNewLines, 1); @@ -1238,7 +1239,22 @@ public class FORMATTER { if (inCaseOrDefaultClause) { inCaseOrDefaultClause = false; caseOrDefaultBlockOpenPending = true; - indent = increaseIndent(indent); + boolean nextIsBlock = nextTokenNotComment != null + && nextTokenNotComment.getType() == ASTokenTypes.TOKEN_BLOCK_OPEN; + if (nextIsBlock) { + IASToken afterBlockClose = findTokenAfterBlock(nextTokenNotComment, tokens); + if (afterBlockClose != null) { + if (afterBlockClose.getType() == ASTokenTypes.TOKEN_BLOCK_CLOSE + || afterBlockClose.getType() == ASTokenTypes.TOKEN_KEYWORD_CASE + || afterBlockClose.getType() == ASTokenTypes.TOKEN_KEYWORD_DEFAULT) { + blockOpenPending = 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)) { @@ -1408,6 +1424,23 @@ public class FORMATTER { return builder.toString(); } + private IASToken findTokenAfterBlock(IASToken tokenBlockOpen, List<IASToken> tokens) { + List<IASToken> stack = new ArrayList<IASToken>(); + int startIndex = tokens.indexOf(tokenBlockOpen) + 1; + for (int i = startIndex; i < tokens.size(); i++) { + IASToken current = tokens.get(i); + if (current.getType() == ASTokenTypes.TOKEN_BLOCK_OPEN) { + stack.add(current); + } else if (current.getType() == ASTokenTypes.TOKEN_BLOCK_CLOSE) { + if (stack.size() == 0) { + return getNextTokenSkipExtraAndComments(tokens, i + 1); + } + stack.remove(stack.size() - 1); + } + } + return null; + } + private int countNewLinesInExtra(IASToken tokenOrExtra) { if (tokenOrExtra == null || tokenOrExtra.getType() != TOKEN_TYPE_EXTRA) { return 0; 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 dffb1167b..d7cd956e7 100644 --- a/formatter/src/test/java/org/apache/royale/formatter/TestSwitchStatement.java +++ b/formatter/src/test/java/org/apache/royale/formatter/TestSwitchStatement.java @@ -146,8 +146,8 @@ public class TestSwitchStatement extends BaseFormatterTests { "switch (condition)\n" + "{\n" + "\tcase condition:\n" + - "\t\t{\n" + - "\t\t}\n" + + "\t{\n" + + "\t}\n" + "}", // @formatter:on result); @@ -176,9 +176,9 @@ public class TestSwitchStatement extends BaseFormatterTests { "switch (condition)\n" + "{\n" + "\tcase condition:\n" + - "\t\t{\n" + - "\t\t\tbreak;\n" + - "\t\t}\n" + + "\t{\n" + + "\t\tbreak;\n" + + "\t}\n" + "}", // @formatter:on result); @@ -295,8 +295,8 @@ public class TestSwitchStatement extends BaseFormatterTests { "switch (condition)\n" + "{\n" + "\tdefault:\n" + - "\t\t{\n" + - "\t\t}\n" + + "\t{\n" + + "\t}\n" + "}", // @formatter:on result); @@ -325,9 +325,9 @@ public class TestSwitchStatement extends BaseFormatterTests { "switch (condition)\n" + "{\n" + "\tdefault:\n" + - "\t\t{\n" + - "\t\t\tbreak;\n" + - "\t\t}\n" + + "\t{\n" + + "\t\tbreak;\n" + + "\t}\n" + "}", // @formatter:on result); @@ -549,8 +549,8 @@ public class TestSwitchStatement extends BaseFormatterTests { "\tswitch (condition)\n" + "\t{\n" + "\t\tcase clause:\n" + - "\t\t\t{\n" + - "\t\t\t}\n" + + "\t\t{\n" + + "\t\t}\n" + "\t}\n" + "\tstatement;\n" + "}", @@ -585,9 +585,9 @@ public class TestSwitchStatement extends BaseFormatterTests { "\tswitch (condition)\n" + "\t{\n" + "\t\tcase clause:\n" + - "\t\t\t{\n" + - "\t\t\t\tbreak;\n" + - "\t\t\t}\n" + + "\t\t{\n" + + "\t\t\tbreak;\n" + + "\t\t}\n" + "\t}\n" + "\tstatement;\n" + "}", @@ -654,8 +654,8 @@ public class TestSwitchStatement extends BaseFormatterTests { "\tswitch (condition)\n" + "\t{\n" + "\t\tdefault:\n" + - "\t\t\t{\n" + - "\t\t\t}\n" + + "\t\t{\n" + + "\t\t}\n" + "\t}\n" + "\tstatement;\n" + "}", @@ -690,9 +690,9 @@ public class TestSwitchStatement extends BaseFormatterTests { "\tswitch (condition)\n" + "\t{\n" + "\t\tdefault:\n" + - "\t\t\t{\n" + - "\t\t\t\tbreak;\n" + - "\t\t\t}\n" + + "\t\t{\n" + + "\t\t\tbreak;\n" + + "\t\t}\n" + "\t}\n" + "\tstatement;\n" + "}",
