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 2022/07/28 21:59:48 UTC

[royale-compiler] 02/02: formatter: preserve one new line at most at the end of the file

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 5a0042801626de146f2a3a8ff7a3816cc5376cce
Author: Josh Tynjala <jo...@apache.org>
AuthorDate: Thu Jul 28 14:09:54 2022 -0700

    formatter: preserve one new line at most at the end of the file
    
    Other new lines are subject to maxPreserveNewLines
---
 .../main/java/org/apache/royale/formatter/FORMATTER.java   | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 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 f73dc03bb..a0f71dcb2 100644
--- a/formatter/src/main/java/org/apache/royale/formatter/FORMATTER.java
+++ b/formatter/src/main/java/org/apache/royale/formatter/FORMATTER.java
@@ -694,8 +694,9 @@ public class FORMATTER {
 					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));
+						// if the last token is whitespace, include at most one
+						// new line, but strip the rest
+						numRequiredNewLines = Math.min(1, Math.max(0, countNewLinesInExtra(token)));
 						appendNewLines(builder, numRequiredNewLines);
 						break;
 					}
@@ -1853,8 +1854,9 @@ public class FORMATTER {
 					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));
+						// if the last token is whitespace, include at most one
+						// new line, but strip the rest
+						numRequiredNewLines = Math.min(1, Math.max(0, countNewLinesInExtra(token)));
 						appendNewLines(builder, numRequiredNewLines);
 						break;
 					}
@@ -1878,7 +1880,9 @@ public class FORMATTER {
 						builder.append(token.getText());
 					}
 					if (i == (tokens.size() - 1)) {
-						// if the last token is whitespace, append new lines
+						// if the last token is whitespace, include at most one
+						// new line, but strip the rest
+						numRequiredNewLines = Math.min(1, numRequiredNewLines);
 						appendNewLines(builder, numRequiredNewLines);
 					}
 				}