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 2025/01/09 20:47:28 UTC
(royale-compiler) 05/09: formatter: new option mxml-indent-cdata
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 6d13e5c8f36a865dc08b0661771e6d48f6e705c3
Author: Josh Tynjala <jo...@apache.org>
AuthorDate: Wed Jan 8 09:32:17 2025 -0800
formatter: new option mxml-indent-cdata
A similar option existed in Flash Builder, so it makes sense to add it to asformat too
---
.../apache/royale/formatter/FormatterSettings.java | 1 +
.../apache/royale/formatter/FormatterUtils.java | 1 +
.../royale/formatter/MXMLTokenFormatter.java | 12 ++-
.../royale/formatter/config/Configuration.java | 17 ++++
.../apache/royale/formatter/TestMXMLScript.java | 104 +++++++++++++++++++--
5 files changed, 124 insertions(+), 11 deletions(-)
diff --git a/formatter/src/main/java/org/apache/royale/formatter/FormatterSettings.java b/formatter/src/main/java/org/apache/royale/formatter/FormatterSettings.java
index 908f824e3..1dacbe3e2 100644
--- a/formatter/src/main/java/org/apache/royale/formatter/FormatterSettings.java
+++ b/formatter/src/main/java/org/apache/royale/formatter/FormatterSettings.java
@@ -40,4 +40,5 @@ public class FormatterSettings {
public boolean collapseEmptyBlocks = false;
public boolean mxmlAlignAttributes = false;
public boolean mxmlInsertNewLineBetweenAttributes = false;
+ public boolean mxmlIndentCData = true;
}
diff --git a/formatter/src/main/java/org/apache/royale/formatter/FormatterUtils.java b/formatter/src/main/java/org/apache/royale/formatter/FormatterUtils.java
index 4221977d9..8dc03d8d6 100644
--- a/formatter/src/main/java/org/apache/royale/formatter/FormatterUtils.java
+++ b/formatter/src/main/java/org/apache/royale/formatter/FormatterUtils.java
@@ -43,6 +43,7 @@ public class FormatterUtils {
settings.collapseEmptyBlocks = configuration.getCollapseEmptyBlocks();
settings.mxmlAlignAttributes = configuration.getMxmlAlignAttributes();
settings.mxmlInsertNewLineBetweenAttributes = configuration.getMxmlInsertNewLineBetweenAttributes();
+ settings.mxmlIndentCData = configuration.getMxmlIndentCData();
return settings;
}
}
diff --git a/formatter/src/main/java/org/apache/royale/formatter/MXMLTokenFormatter.java b/formatter/src/main/java/org/apache/royale/formatter/MXMLTokenFormatter.java
index f514eaa99..3ec74f99f 100644
--- a/formatter/src/main/java/org/apache/royale/formatter/MXMLTokenFormatter.java
+++ b/formatter/src/main/java/org/apache/royale/formatter/MXMLTokenFormatter.java
@@ -369,7 +369,7 @@ public class MXMLTokenFormatter extends BaseTokenFormatter {
}
if (formattedScriptText.length() > 0) {
String[] formattedLines = formattedScriptText.split("\n");
- String lineIndent = requireCdata ? (indent + indent + indent) : (indent + indent);
+ String lineIndent = (requireCdata && settings.mxmlIndentCData) ? (indent + indent + indent) : (indent + indent);
for (int i = 0; i < formattedLines.length; i++) {
formattedLines[i] = lineIndent + formattedLines[i];
}
@@ -381,7 +381,10 @@ public class MXMLTokenFormatter extends BaseTokenFormatter {
builder.append(">\n");
if (requireCdata) {
builder.append(indent);
- builder.append(indent);
+ if (settings.mxmlIndentCData)
+ {
+ builder.append(indent);
+ }
builder.append("<![CDATA[\n");
}
if (formattedScriptText.length() > 0) {
@@ -390,7 +393,10 @@ public class MXMLTokenFormatter extends BaseTokenFormatter {
}
if (requireCdata) {
builder.append(indent);
- builder.append(indent);
+ if (settings.mxmlIndentCData)
+ {
+ builder.append(indent);
+ }
builder.append("]]>\n");
}
builder.append(indent);
diff --git a/formatter/src/main/java/org/apache/royale/formatter/config/Configuration.java b/formatter/src/main/java/org/apache/royale/formatter/config/Configuration.java
index aa2f69535..e548aed70 100644
--- a/formatter/src/main/java/org/apache/royale/formatter/config/Configuration.java
+++ b/formatter/src/main/java/org/apache/royale/formatter/config/Configuration.java
@@ -588,6 +588,23 @@ public class Configuration {
this.mxmlInsertNewLineBetweenAttributes = b;
}
+ //
+ // 'mxml-indent-cdata' option
+ //
+
+ private boolean mxmlIndentCData = true;
+
+ public boolean getMxmlIndentCData()
+ {
+ return mxmlIndentCData;
+ }
+
+ @Mapping("mxml-indent-cdata")
+ public void setMxmlIndentCData(ConfigurationValue cv, boolean b)
+ {
+ this.mxmlIndentCData = b;
+ }
+
/**
*
* @param path A path to resolve.
diff --git a/formatter/src/test/java/org/apache/royale/formatter/TestMXMLScript.java b/formatter/src/test/java/org/apache/royale/formatter/TestMXMLScript.java
index f5c6783e1..b3938f60f 100644
--- a/formatter/src/test/java/org/apache/royale/formatter/TestMXMLScript.java
+++ b/formatter/src/test/java/org/apache/royale/formatter/TestMXMLScript.java
@@ -25,9 +25,10 @@ import org.junit.Test;
public class TestMXMLScript extends BaseFormatterTests {
@Test
- public void testEmptyScriptNoCdata() {
+ public void testEmptyScriptNoCDataIndentCDataEnabled() {
FormatterSettings settings = new FormatterSettings();
settings.insertSpaces = false;
+ settings.mxmlIndentCData = true;
MXMLTokenFormatter formatter = new MXMLTokenFormatter(settings);
String result = formatter.format("file.mxml",
// @formatter:off
@@ -51,9 +52,37 @@ public class TestMXMLScript extends BaseFormatterTests {
}
@Test
- public void testEmptyScriptNoCdataNoWhitespace() {
+ public void testEmptyScriptNoCDataIndentCDataDisabled() {
FormatterSettings settings = new FormatterSettings();
settings.insertSpaces = false;
+ settings.mxmlIndentCData = false;
+ MXMLTokenFormatter formatter = new MXMLTokenFormatter(settings);
+ String result = formatter.format("file.mxml",
+ // @formatter:off
+ "<s:Application>\n" +
+ "<fx:Script>\n" +
+ "</fx:Script>\n" +
+ "</s:Application>",
+ // @formatter:on
+ problems
+ );
+ assertEquals(
+ // @formatter:off
+ "<s:Application>\n" +
+ "\t<fx:Script>\n" +
+ "\t<![CDATA[\n" +
+ "\t]]>\n" +
+ "\t</fx:Script>\n" +
+ "</s:Application>",
+ // @formatter:on
+ result);
+ }
+
+ @Test
+ public void testEmptyScriptNoCDataNoWhitespaceIndentCDataEnabled() {
+ FormatterSettings settings = new FormatterSettings();
+ settings.insertSpaces = false;
+ settings.mxmlIndentCData = true;
MXMLTokenFormatter formatter = new MXMLTokenFormatter(settings);
String result = formatter.format("file.mxml",
// @formatter:off
@@ -76,9 +105,36 @@ public class TestMXMLScript extends BaseFormatterTests {
}
@Test
- public void testEmptyScriptWithCdata() {
+ public void testEmptyScriptNoCDataNoWhitespaceIndentCDataDisabled() {
FormatterSettings settings = new FormatterSettings();
settings.insertSpaces = false;
+ settings.mxmlIndentCData = false;
+ MXMLTokenFormatter formatter = new MXMLTokenFormatter(settings);
+ String result = formatter.format("file.mxml",
+ // @formatter:off
+ "<s:Application>\n" +
+ "<fx:Script></fx:Script>\n" +
+ "</s:Application>",
+ // @formatter:on
+ problems
+ );
+ assertEquals(
+ // @formatter:off
+ "<s:Application>\n" +
+ "\t<fx:Script>\n" +
+ "\t<![CDATA[\n" +
+ "\t]]>\n" +
+ "\t</fx:Script>\n" +
+ "</s:Application>",
+ // @formatter:on
+ result);
+ }
+
+ @Test
+ public void testEmptyScriptWithCDataIndentCDataEnabled() {
+ FormatterSettings settings = new FormatterSettings();
+ settings.insertSpaces = false;
+ settings.mxmlIndentCData = true;
MXMLTokenFormatter formatter = new MXMLTokenFormatter(settings);
String result = formatter.format("file.mxml",
// @formatter:off
@@ -104,9 +160,10 @@ public class TestMXMLScript extends BaseFormatterTests {
}
@Test
- public void testEmptyScriptWithCdataNoWhitespace() {
+ public void testEmptyScriptWithCDataNoWhitespaceIndentCDataDisabled() {
FormatterSettings settings = new FormatterSettings();
settings.insertSpaces = false;
+ settings.mxmlIndentCData = false;
MXMLTokenFormatter formatter = new MXMLTokenFormatter(settings);
String result = formatter.format("file.mxml",
// @formatter:off
@@ -120,8 +177,8 @@ public class TestMXMLScript extends BaseFormatterTests {
// @formatter:off
"<s:Application>\n" +
"\t<fx:Script>\n" +
- "\t\t<![CDATA[\n" +
- "\t\t]]>\n" +
+ "\t<![CDATA[\n" +
+ "\t]]>\n" +
"\t</fx:Script>\n" +
"</s:Application>",
// @formatter:on
@@ -129,9 +186,10 @@ public class TestMXMLScript extends BaseFormatterTests {
}
@Test
- public void testScriptWithActionScript() {
+ public void testScriptWithActionScriptIndentCDataEnabled() {
FormatterSettings settings = new FormatterSettings();
settings.insertSpaces = false;
+ settings.mxmlIndentCData = true;
MXMLTokenFormatter formatter = new MXMLTokenFormatter(settings);
String result = formatter.format("file.mxml",
// @formatter:off
@@ -157,5 +215,35 @@ public class TestMXMLScript extends BaseFormatterTests {
// @formatter:on
result);
}
-
+
+ @Test
+ public void testScriptWithActionScriptIndentCDataDisabled() {
+ FormatterSettings settings = new FormatterSettings();
+ settings.insertSpaces = false;
+ settings.mxmlIndentCData = false;
+ MXMLTokenFormatter formatter = new MXMLTokenFormatter(settings);
+ String result = formatter.format("file.mxml",
+ // @formatter:off
+ "<s:Application>\n" +
+ "<fx:Script>\n" +
+ "<![CDATA[\n" +
+ "public var a: Number=123.4;\n" +
+ "]]>\n" +
+ "</fx:Script>\n" +
+ "</s:Application>",
+ // @formatter:on
+ problems
+ );
+ assertEquals(
+ // @formatter:off
+ "<s:Application>\n" +
+ "\t<fx:Script>\n" +
+ "\t<![CDATA[\n" +
+ "\t\tpublic var a:Number = 123.4;\n" +
+ "\t]]>\n" +
+ "\t</fx:Script>\n" +
+ "</s:Application>",
+ // @formatter:on
+ result);
+ }
}