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


The following commit(s) were added to refs/heads/develop by this push:
     new 427d54251 formatter: fix formatting of switch/case inside for-each or 
for-in loop
427d54251 is described below

commit 427d542518381c64eb23c1554d21dada53c5da2b
Author: Josh Tynjala <[email protected]>
AuthorDate: Thu Jul 11 13:06:17 2024 -0700

    formatter: fix formatting of switch/case inside for-each or for-in loop
    
    The variable declaration in side the for loop was still considered active 
because the in keyword wasn't treated as the end of the declaration. Added 
that, and also cleared the flag at paren end, just for extra safety.
---
 .../apache/royale/formatter/ASTokenFormatter.java  | 13 ++++++-
 .../royale/formatter/TestForEachStatement.java     | 40 ++++++++++++++++++++++
 .../royale/formatter/TestForInStatement.java       | 40 ++++++++++++++++++++++
 .../apache/royale/formatter/TestForStatement.java  | 40 ++++++++++++++++++++++
 4 files changed, 132 insertions(+), 1 deletion(-)

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 91b8504d4..4cf87a6d5 100644
--- a/formatter/src/main/java/org/apache/royale/formatter/ASTokenFormatter.java
+++ b/formatter/src/main/java/org/apache/royale/formatter/ASTokenFormatter.java
@@ -381,9 +381,14 @@ public class ASTokenFormatter extends BaseTokenFormatter {
                                                }
                                                break;
                                        }
+                                       case ASTokenTypes.TOKEN_KEYWORD_IN: {
+                                               inVarOrConstDeclaration = false;
+                                               // needs an extra space before 
the token
+                                               requiredSpace = true;
+                                               break;
+                                       }
                                        case ASTokenTypes.TOKEN_KEYWORD_AS:
                                        case ASTokenTypes.TOKEN_KEYWORD_IS:
-                                       case ASTokenTypes.TOKEN_KEYWORD_IN:
                                        case 
ASTokenTypes.TOKEN_RESERVED_WORD_EACH:
                                        case 
ASTokenTypes.TOKEN_RESERVED_WORD_EXTENDS:
                                        case 
ASTokenTypes.TOKEN_RESERVED_WORD_IMPLEMENTS:
@@ -709,6 +714,12 @@ public class ASTokenFormatter extends BaseTokenFormatter {
                                                        if 
(controlFlowParenStack <= 0) {
                                                                
endIndentedStatement();
                                                                
inControlFlowStatement = false;
+                                                               // if a 
variable was declared inside the
+                                                               // parentheses, 
such as in a for() loop, this
+                                                               // should 
already be false (if it's not, there's
+                                                               // a bug 
somewhere).
+                                                               // but this 
will add a little extra safety.
+                                                               
inVarOrConstDeclaration = false;
                                                                if 
(!blockStack.isEmpty()) {
                                                                        
BlockStackItem stackItem = blockStack.get(blockStack.size() - 1);
                                                                        
stackItem.controlFlowEnd = token.getEnd();
diff --git 
a/formatter/src/test/java/org/apache/royale/formatter/TestForEachStatement.java 
b/formatter/src/test/java/org/apache/royale/formatter/TestForEachStatement.java
index d437247dd..076d46838 100644
--- 
a/formatter/src/test/java/org/apache/royale/formatter/TestForEachStatement.java
+++ 
b/formatter/src/test/java/org/apache/royale/formatter/TestForEachStatement.java
@@ -528,4 +528,44 @@ public class TestForEachStatement extends 
BaseFormatterTests {
                                // @formatter:on
                                result);
        }
+
+       @Test
+       public void testSwitchWithMultipleCases() {
+               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" +
+                       "\tfor each (var item:Object in array) {\n" +
+                       "\t\tswitch (condition) {\n" +
+                       "\t\t\tcase 1:\n" +
+                       "\t\t\t\tbreak;\n" +
+                       "\t\t\tcase 2:\n" +
+                       "\t\t\t\tbreak;\n" +
+                       "\t\t}\n" +
+                       "\t}\n" +
+                       "}",
+                       // @formatter:on
+                       problems
+               );
+               assertEquals(
+               // @formatter:off
+                                       "{\n" +
+                                       "\tfor each (var item:Object in 
array)\n" +
+                                       "\t{\n" +
+                                       "\t\tswitch (condition)\n" +
+                                       "\t\t{\n" +
+                                       "\t\t\tcase 1:\n" +
+                                       "\t\t\t\tbreak;\n" +
+                                       "\t\t\tcase 2:\n" +
+                                       "\t\t\t\tbreak;\n" +
+                                       "\t\t}\n" + 
+                                       "\t}\n" + 
+                                       "}",
+                               // @formatter:on
+                               result);
+       }
 }
diff --git 
a/formatter/src/test/java/org/apache/royale/formatter/TestForInStatement.java 
b/formatter/src/test/java/org/apache/royale/formatter/TestForInStatement.java
index 52cc3e23f..3ffd8e0fa 100644
--- 
a/formatter/src/test/java/org/apache/royale/formatter/TestForInStatement.java
+++ 
b/formatter/src/test/java/org/apache/royale/formatter/TestForInStatement.java
@@ -528,4 +528,44 @@ public class TestForInStatement extends BaseFormatterTests 
{
                                // @formatter:on
                                result);
        }
+
+       @Test
+       public void testSwitchWithMultipleCases() {
+               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" +
+                       "\tfor (var key:String in object) {\n" +
+                       "\t\tswitch (condition) {\n" +
+                       "\t\t\tcase 1:\n" +
+                       "\t\t\t\tbreak;\n" +
+                       "\t\t\tcase 2:\n" +
+                       "\t\t\t\tbreak;\n" +
+                       "\t\t}\n" +
+                       "\t}\n" +
+                       "}",
+                       // @formatter:on
+                       problems
+               );
+               assertEquals(
+               // @formatter:off
+                                       "{\n" +
+                                       "\tfor (var key:String in object)\n" +
+                                       "\t{\n" +
+                                       "\t\tswitch (condition)\n" +
+                                       "\t\t{\n" +
+                                       "\t\t\tcase 1:\n" +
+                                       "\t\t\t\tbreak;\n" +
+                                       "\t\t\tcase 2:\n" +
+                                       "\t\t\t\tbreak;\n" +
+                                       "\t\t}\n" + 
+                                       "\t}\n" + 
+                                       "}",
+                               // @formatter:on
+                               result);
+       }
 }
diff --git 
a/formatter/src/test/java/org/apache/royale/formatter/TestForStatement.java 
b/formatter/src/test/java/org/apache/royale/formatter/TestForStatement.java
index 1f3104707..59b34edfd 100644
--- a/formatter/src/test/java/org/apache/royale/formatter/TestForStatement.java
+++ b/formatter/src/test/java/org/apache/royale/formatter/TestForStatement.java
@@ -559,4 +559,44 @@ public class TestForStatement extends BaseFormatterTests {
                                // @formatter:on
                                result);
        }
+
+       @Test
+       public void testSwitchWithMultipleCases() {
+               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" +
+                       "\tfor (var i:int = 0; i < 3; i++) {\n" +
+                       "\t\tswitch (condition) {\n" +
+                       "\t\t\tcase 1:\n" +
+                       "\t\t\t\tbreak;\n" +
+                       "\t\t\tcase 2:\n" +
+                       "\t\t\t\tbreak;\n" +
+                       "\t\t}\n" +
+                       "\t}\n" +
+                       "}",
+                       // @formatter:on
+                       problems
+               );
+               assertEquals(
+               // @formatter:off
+                                       "{\n" +
+                                       "\tfor (var i:int = 0; i < 3; i++)\n" +
+                                       "\t{\n" +
+                                       "\t\tswitch (condition)\n" +
+                                       "\t\t{\n" +
+                                       "\t\t\tcase 1:\n" +
+                                       "\t\t\t\tbreak;\n" +
+                                       "\t\t\tcase 2:\n" +
+                                       "\t\t\t\tbreak;\n" +
+                                       "\t\t}\n" + 
+                                       "\t}\n" + 
+                                       "}",
+                               // @formatter:on
+                               result);
+       }
 }

Reply via email to