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/09/20 21:24:58 UTC

[royale-compiler] 03/05: formatter: fix new lines at end of file not being preserved

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 da1c4e56554cee487b9c57cfc6a1a831b6620839
Author: Josh Tynjala <jo...@apache.org>
AuthorDate: Mon Sep 20 11:43:42 2021 -0700

    formatter: fix new lines at end of file not being preserved
---
 .../org/apache/royale/formatter/FORMATTER.java     | 34 ++++++++++++++++++----
 1 file changed, 28 insertions(+), 6 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 d010430..d3239dd 100644
--- a/formatter/src/main/java/org/apache/royale/formatter/FORMATTER.java
+++ b/formatter/src/main/java/org/apache/royale/formatter/FORMATTER.java
@@ -408,6 +408,18 @@ class FORMATTER {
 			tokens.add(token);
 			prevToken = token;
 		}
+		if(prevToken != null) {
+			int start = prevToken.getAbsoluteEnd();
+			int end = text.length();
+			if (end > start) {
+				String tokenText = text.substring(start, end);
+				ASToken extraToken = new ASToken(TOKEN_TYPE_EXTRA, start, end, prevToken.getEndLine(),
+						prevToken.getEndColumn(), tokenText);
+				extraToken.setEndLine(prevToken.getLine());
+				extraToken.setEndLine(prevToken.getColumn());
+				tokens.add(extraToken);
+			}
+		}
 		try {
 			return parseTokens(filePath, tokens, node);
 		} catch (Exception e) {
@@ -471,6 +483,12 @@ class FORMATTER {
 		for (int i = 0; i < tokens.size(); i++) {
 			token = tokens.get(i);
 			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 (!blockOpenPending) {
 					numRequiredNewLines = Math.max(numRequiredNewLines, countNewLinesInExtra(token));
 					if (!indentedStatement && numRequiredNewLines > 0 && prevTokenNotComment != null
@@ -701,12 +719,7 @@ class FORMATTER {
 			}
 			if (prevToken != null) {
 				if (numRequiredNewLines > 0) {
-					if (maxPreserveNewLines != 0) {
-						numRequiredNewLines = Math.min(maxPreserveNewLines, numRequiredNewLines);
-					}
-					for (int j = 0; j < numRequiredNewLines; j++) {
-						builder.append('\n');
-					}
+					appendNewLines(builder, numRequiredNewLines);
 					appendIndent(builder, indent);
 				} else if (requiredSpace) {
 					builder.append(' ');
@@ -1283,6 +1296,15 @@ class FORMATTER {
 		}
 	}
 
+	private void appendNewLines(StringBuilder builder, int numRequiredNewLines) {
+		if (maxPreserveNewLines != 0) {
+			numRequiredNewLines = Math.min(maxPreserveNewLines, numRequiredNewLines);
+		}
+		for (int j = 0; j < numRequiredNewLines; j++) {
+			builder.append('\n');
+		}
+	}
+
 	private static class BlockStackItem {
 		public BlockStackItem(IASToken token) {
 			this.token = token;