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/10/06 18:00:24 UTC

[royale-compiler] 01/07: formatter: option to insert a space at the start of line comments

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 64e040c0ca0dfb0029098599de1ad895121550cf
Author: Josh Tynjala <jo...@apache.org>
AuthorDate: Wed Oct 6 10:04:40 2021 -0700

    formatter: option to insert a space at the start of line comments
---
 .../org/apache/royale/formatter/FORMATTER.java     | 10 ++++++++-
 .../royale/formatter/config/Configuration.java     | 18 +++++++++++++++
 .../royale/formatter/TestSingleLineComment.java    | 26 +++++++++++++++++++++-
 3 files changed, 52 insertions(+), 2 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 ee6ceb8..6eed027 100644
--- a/formatter/src/main/java/org/apache/royale/formatter/FORMATTER.java
+++ b/formatter/src/main/java/org/apache/royale/formatter/FORMATTER.java
@@ -104,6 +104,7 @@ class FORMATTER {
 	public boolean insertSpaceAfterFunctionKeywordForAnonymousFunctions = false;
 	public boolean insertSpaceBeforeAndAfterBinaryOperators = true;
 	public boolean insertSpaceAfterCommaDelimiter = true;
+	public boolean insertSpaceAtStartOfLineComment = true;
 	public int maxPreserveNewLines = 2;
 	public Semicolons semicolons = Semicolons.INSERT;
 	public boolean ignoreProblems = false;
@@ -293,6 +294,7 @@ class FORMATTER {
 					.getInsertSpaceAfterKeywordsInControlFlowStatements();
 			insertSpaceAfterSemicolonInForStatements = configuration.getInsertSpaceAfterSemicolonInForStatements();
 			insertSpaceBeforeAndAfterBinaryOperators = configuration.getInsertSpaceBeforeAndAfterBinaryOperators();
+			insertSpaceAtStartOfLineComment = configuration.getInsertSpaceAtStartOfLineComment();
 			insertSpaces = configuration.getInsertSpaces();
 			listChangedFiles = configuration.getListFiles();
 			maxPreserveNewLines = configuration.getMaxPreserveNewLines();
@@ -1187,7 +1189,13 @@ class FORMATTER {
 
 	private String formatSingleLineComment(String comment) {
 		comment = comment.substring(2).trim();
-		return "// " + comment;
+		StringBuilder builder = new StringBuilder();
+		builder.append("//");
+		if(insertSpaceAtStartOfLineComment) {
+			builder.append(" ");
+		}
+		builder.append(comment);
+		return builder.toString();
 	}
 
 	private String formatMultiLineComment(String comment) {
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 c66c8a9..1b6699f 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
@@ -347,6 +347,24 @@ public class Configuration {
     }
 
     //
+    // 'insert-space-line-comment' option
+    //
+
+    private boolean insertSpaceAtStartOfLineComment = true;
+
+    public boolean getInsertSpaceAtStartOfLineComment()
+    {
+        return insertSpaceAtStartOfLineComment;
+    }
+
+    @Config
+    @Mapping("insert-space-line-comment")
+    public void setInsertSpaceAtStartOfLineComment(ConfigurationValue cv, boolean b)
+    {
+        this.insertSpaceAtStartOfLineComment = b;
+    }
+
+    //
     // 'collapse-empty-blocks' option
     //
 
diff --git a/formatter/src/test/java/org/apache/royale/formatter/TestSingleLineComment.java b/formatter/src/test/java/org/apache/royale/formatter/TestSingleLineComment.java
index 1d96788..7d752be 100644
--- a/formatter/src/test/java/org/apache/royale/formatter/TestSingleLineComment.java
+++ b/formatter/src/test/java/org/apache/royale/formatter/TestSingleLineComment.java
@@ -25,11 +25,12 @@ import org.junit.Test;
 
 public class TestSingleLineComment extends BaseFormatterTests {
 	@Test
-	public void testWithoutSpace() {
+	public void testInsertSpaceAtStartOfLineComment() {
 		FORMATTER formatter = new FORMATTER();
 		formatter.insertSpaceAfterKeywordsInControlFlowStatements = true;
 		formatter.placeOpenBraceOnNewLine = true;
 		formatter.insertSpaces = false;
+		formatter.insertSpaceAtStartOfLineComment = true;
 		String result = formatter.formatText(
 		// @formatter:off
 			"//this is a comment",
@@ -42,6 +43,25 @@ public class TestSingleLineComment extends BaseFormatterTests {
 				// @formatter:on
 				result);
 	}
+	@Test
+	public void testDisableInsertSpaceAtStartOfLineComment() {
+		FORMATTER formatter = new FORMATTER();
+		formatter.insertSpaceAfterKeywordsInControlFlowStatements = true;
+		formatter.placeOpenBraceOnNewLine = true;
+		formatter.insertSpaces = false;
+		formatter.insertSpaceAtStartOfLineComment = false;
+		String result = formatter.formatText(
+		// @formatter:off
+			"// this is a comment",
+			// @formatter:on
+			problems
+		);
+		assertEquals(
+		// @formatter:off
+				"//this is a comment",
+				// @formatter:on
+				result);
+	}
 
 	@Test
 	public void testAtEndOfStatement() {
@@ -49,6 +69,7 @@ public class TestSingleLineComment extends BaseFormatterTests {
 		formatter.insertSpaceAfterKeywordsInControlFlowStatements = true;
 		formatter.placeOpenBraceOnNewLine = true;
 		formatter.insertSpaces = false;
+		formatter.insertSpaceAtStartOfLineComment = true;
 		String result = formatter.formatText(
 		// @formatter:off
 			"statement; // this is a comment",
@@ -68,6 +89,7 @@ public class TestSingleLineComment extends BaseFormatterTests {
 		formatter.insertSpaceAfterKeywordsInControlFlowStatements = true;
 		formatter.placeOpenBraceOnNewLine = true;
 		formatter.insertSpaces = false;
+		formatter.insertSpaceAtStartOfLineComment = true;
 		String result = formatter.formatText(
 		// @formatter:off
 			"// this is a comment\n" +
@@ -89,6 +111,7 @@ public class TestSingleLineComment extends BaseFormatterTests {
 		formatter.insertSpaceAfterKeywordsInControlFlowStatements = true;
 		formatter.placeOpenBraceOnNewLine = true;
 		formatter.insertSpaces = false;
+		formatter.insertSpaceAtStartOfLineComment = true;
 		String result = formatter.formatText(
 		// @formatter:off
 			"statement;\n" +
@@ -110,6 +133,7 @@ public class TestSingleLineComment extends BaseFormatterTests {
 		formatter.insertSpaceAfterKeywordsInControlFlowStatements = true;
 		formatter.placeOpenBraceOnNewLine = true;
 		formatter.insertSpaces = false;
+		formatter.insertSpaceAtStartOfLineComment = true;
 		String result = formatter.formatText(
 		// @formatter:off
 			"if (statement) // this is a comment\n" +