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:35 UTC

[royale-compiler] 02/04: formatter: like square and curly brackets, parentheses are on the block stack

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 cf33da792dc6a7f63a00dd134984c8479d4ff3af
Author: Josh Tynjala <jo...@apache.org>
AuthorDate: Mon Nov 22 14:28:46 2021 -0800

    formatter: like square and curly brackets, parentheses are on the block stack
---
 .../org/apache/royale/formatter/FORMATTER.java     | 42 +++++++++++++++-------
 1 file changed, 29 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 857c6fc..775be8a 100644
--- a/formatter/src/main/java/org/apache/royale/formatter/FORMATTER.java
+++ b/formatter/src/main/java/org/apache/royale/formatter/FORMATTER.java
@@ -980,8 +980,7 @@ public class FORMATTER {
 			} else {
 				switch (token.getType()) {
 					case ASTokenTypes.TOKEN_SEMICOLON: {
-						if (inControlFlowStatement && !blockStack.isEmpty() && blockStack.get(blockStack.size() - 1).token
-								.getType() == ASTokenTypes.TOKEN_KEYWORD_FOR) {
+						if (inControlFlowStatement && isInForStatement(blockStack)) {
 							if (insertSpaceAfterSemicolonInForStatements) {
 								requiredSpace = true;
 							}
@@ -1185,15 +1184,19 @@ public class FORMATTER {
 						break;
 					}
 					case ASTokenTypes.TOKEN_PAREN_OPEN: {
+						blockStack.add(new BlockStackItem(token));
 						if (inControlFlowStatement) {
 							controlFlowParenStack++;
 						}
-						else {
-							blockStack.add(new BlockStackItem(token));
-						}
 						break;
 					}
 					case ASTokenTypes.TOKEN_PAREN_CLOSE: {
+						if (!blockStack.isEmpty()) {
+							BlockStackItem item = blockStack.get(blockStack.size() - 1);
+							if (item.token.getType() == ASTokenTypes.TOKEN_PAREN_OPEN) {
+								blockStack.remove(item);
+							}
+						}
 						if (inControlFlowStatement) {
 							controlFlowParenStack--;
 							if (controlFlowParenStack <= 0) {
@@ -1215,14 +1218,6 @@ public class FORMATTER {
 								}
 							}
 						}
-						else {
-							if (!blockStack.isEmpty()) {
-								BlockStackItem item = blockStack.get(blockStack.size() - 1);
-								if (item.token.getType() == ASTokenTypes.TOKEN_PAREN_OPEN) {
-									blockStack.remove(item);
-								}
-							}
-						}
 						break;
 					}
 					case ASTokenTypes.TOKEN_KEYWORD_CASE: {
@@ -1435,6 +1430,27 @@ public class FORMATTER {
 		return builder.toString();
 	}
 
+	private boolean isInForStatement(List<BlockStackItem> blockStack) {
+		for (int i = blockStack.size() - 1; i >= 0; i--) {
+			BlockStackItem item = blockStack.get(i);
+			switch (item.token.getType()) {
+				case ASTokenTypes.TOKEN_BLOCK_OPEN:
+				case ASTokenTypes.TOKEN_SQUARE_OPEN:
+				case ASTokenTypes.TOKEN_PAREN_OPEN: {
+					// these tokens are fine, keep searching
+					break;
+				}
+				case ASTokenTypes.TOKEN_KEYWORD_FOR: {
+					return true;
+				}
+				default: {
+					return false;
+				}
+			}
+		}
+		return false;
+	}
+
 	private boolean isInListing(String lineText, boolean alreadyInListing) {
 		int searchIndex = 0;
 		boolean inListing = alreadyInListing;