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 078dfca354db96e8619074d7d95a27a7bdad965a
Author: Josh Tynjala <[email protected]>
AuthorDate: Mon Nov 15 14:48:12 2021 -0800

    formatter: MXML formatter off/on comments
---
 .../org/apache/royale/formatter/FORMATTER.java     | 59 ++++++++++++++--------
 .../apache/royale/formatter/TestFormatterOff.java  | 43 +++++++++++++++-
 2 files changed, 81 insertions(+), 21 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 c379573..8fda9a1 100644
--- a/formatter/src/main/java/org/apache/royale/formatter/FORMATTER.java
+++ b/formatter/src/main/java/org/apache/royale/formatter/FORMATTER.java
@@ -1723,6 +1723,7 @@ public class FORMATTER {
                boolean requiredSpace = false;
                boolean inOpenTag = false;
                boolean inCloseTag = false;
+               boolean skipFormatting = false;
                String attributeIndent = "";
                IMXMLToken prevToken = null;
                IMXMLToken prevTokenOrExtra = null;
@@ -1737,30 +1738,38 @@ public class FORMATTER {
                                nextToken = tokens.get(i + 1);
                        }
                        if (token.getType() == TOKEN_TYPE_EXTRA) {
-                               if (i == (tokens.size() - 1)) {
-                                       // if the last token is whitespace, 
include new lines
-                                       numRequiredNewLines = Math.max(0, 
countNewLinesInExtra(token));
-                                       appendNewLines(builder, 
numRequiredNewLines);
-                                       break;
+                               if (skipFormatting) {
+                                       builder.append(token.getText());
+                               } else {
+                                       if (i == (tokens.size() - 1)) {
+                                               // if the last token is 
whitespace, include new lines
+                                               numRequiredNewLines = 
Math.max(0, countNewLinesInExtra(token));
+                                               appendNewLines(builder, 
numRequiredNewLines);
+                                               break;
+                                       }
+                                       numRequiredNewLines = 
Math.max(numRequiredNewLines, countNewLinesInExtra(token));
                                }
-                               numRequiredNewLines = 
Math.max(numRequiredNewLines, countNewLinesInExtra(token));
                                prevTokenOrExtra = token;
                                continue;
                        } else if (token.getType() == 
MXMLTokenTypes.TOKEN_WHITESPACE) {
-                               if (elementStack.isEmpty() || 
!elementStack.get(elementStack.size() - 1).containsText) {
-                                       numRequiredNewLines = 
Math.max(numRequiredNewLines, countNewLinesInExtra(token));
-                               } else {
-                                       // if the parent element contains text, 
treat whitespace
-                                       // the same as text, and don't reformat 
it
-                                       // text is never reformatted because 
some components use it
-                                       // without collapsing whitespace, and 
developers would be
-                                       // confused if whitespace that they 
deliberately added were
-                                       // to be removed
+                               if (skipFormatting) {
                                        builder.append(token.getText());
-                               }
-                               if (i == (tokens.size() - 1)) {
-                                       // if the last token is whitespace, 
append new lines
-                                       appendNewLines(builder, 
numRequiredNewLines);
+                               } else {
+                                       if (elementStack.isEmpty() || 
!elementStack.get(elementStack.size() - 1).containsText) {
+                                               numRequiredNewLines = 
Math.max(numRequiredNewLines, countNewLinesInExtra(token));
+                                       } else {
+                                               // if the parent element 
contains text, treat whitespace
+                                               // the same as text, and don't 
reformat it
+                                               // text is never reformatted 
because some components use it
+                                               // without collapsing 
whitespace, and developers would be
+                                               // confused if whitespace that 
they deliberately added were
+                                               // to be removed
+                                               builder.append(token.getText());
+                                       }
+                                       if (i == (tokens.size() - 1)) {
+                                               // if the last token is 
whitespace, append new lines
+                                               appendNewLines(builder, 
numRequiredNewLines);
+                                       }
                                }
                                continue;
                        } else if (token.getType() == 
MXMLTokenTypes.TOKEN_OPEN_TAG_START
@@ -1821,7 +1830,7 @@ public class FORMATTER {
                                }
                        }
 
-                       if (prevToken != null) {
+                       if (!skipFormatting && prevToken != null) {
                                if (numRequiredNewLines > 0) {
                                        appendNewLines(builder, 
numRequiredNewLines);
                                        appendIndent(builder, indent);
@@ -1915,6 +1924,16 @@ public class FORMATTER {
                                        }
                                        break;
                                }
+                               case MXMLTokenTypes.TOKEN_COMMENT: {
+                                       String tokenText = token.getText();
+                                       String trimmed = tokenText.substring(4, 
tokenText.length() - 3).trim();
+                                       if (!skipFormatting && 
FORMATTER_TAG_OFF.equals(trimmed)) {
+                                               skipFormatting = true;
+                                       } else if (skipFormatting && 
FORMATTER_TAG_ON.equals(trimmed)) {
+                                               skipFormatting = false;
+                                       }
+                                       break;
+                               }
                        }
 
                        prevToken = token;
diff --git 
a/formatter/src/test/java/org/apache/royale/formatter/TestFormatterOff.java 
b/formatter/src/test/java/org/apache/royale/formatter/TestFormatterOff.java
index c0c8f9b..be1fd28 100644
--- a/formatter/src/test/java/org/apache/royale/formatter/TestFormatterOff.java
+++ b/formatter/src/test/java/org/apache/royale/formatter/TestFormatterOff.java
@@ -25,7 +25,7 @@ import org.junit.Test;
 
 public class TestFormatterOff extends BaseFormatterTests {
        @Test
-       public void testFormatterOff() {
+       public void testAS3FormatterOff() {
                FORMATTER formatter = new FORMATTER();
                formatter.insertSpaceAfterKeywordsInControlFlowStatements = 
true;
                formatter.placeOpenBraceOnNewLine = true;
@@ -69,4 +69,45 @@ public class TestFormatterOff extends BaseFormatterTests {
                                // @formatter:on
                                result);
        }
+
+       @Test
+       public void testMXMLFormatterOff() {
+               FORMATTER formatter = new FORMATTER();
+               formatter.insertSpaceAfterKeywordsInControlFlowStatements = 
true;
+               formatter.placeOpenBraceOnNewLine = true;
+               formatter.insertSpaces = true;
+               formatter.tabSize = 2;
+               formatter.maxPreserveNewLines = 2;
+               String result = formatter.formatMXMLText(
+               // @formatter:off
+                       "<mx:Application>\n" +
+                       "\t<!-- @formatter:off -->\n" +
+                       "\t<mx:Button/>\n" +
+                       "\n" +
+                       "\n" +
+                       "\n" +
+                       "\t<!-- @formatter:on -->\n" +
+                       "\t<mx:Button/>\n" +
+                       "\n" +
+                       "\n" +
+                       "\n" +
+                       "</mx:Application>",
+                       // @formatter:on
+                       problems
+               );
+               assertEquals(
+               // @formatter:off
+                               "<mx:Application>\n" +
+                               "  <!-- @formatter:off -->\n" +
+                               "\t<mx:Button/>\n" +
+                               "\n" +
+                               "\n" +
+                               "\n" +
+                               "\t<!-- @formatter:on -->\n" +
+                               "  <mx:Button/>\n" +
+                               "\n" +
+                               "</mx:Application>",
+                               // @formatter:on
+                               result);
+       }
 }
\ No newline at end of file

Reply via email to