Skip to content

Commit b040160

Browse files
committed
include newlines at end of files when saving (fixes #5327)
1 parent 9eb63c1 commit b040160

2 files changed

Lines changed: 13 additions & 9 deletions

File tree

app/src/processing/app/Util.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,11 @@ static public String loadFile(File file) throws IOException {
162162
* Spew the contents of a String object out to a file. As of 3.0 beta 2,
163163
* this will replace and write \r\n for newlines on Windows.
164164
* https://github.com/processing/processing/issues/3455
165+
* As of 3.3.7, this puts a newline at the end of the file,
166+
* per good practice/POSIX: https://stackoverflow.com/a/729795
165167
*/
166-
static public void saveFile(String str, File file) throws IOException {
167-
if (Platform.isWindows()) {
168-
String[] lines = str.split("\\r?\\n");
169-
str = PApplet.join(lines, "\r\n");
170-
}
168+
static public void saveFile(String text, File file) throws IOException {
169+
String[] lines = text.split("\\r?\\n");
171170
File temp = File.createTempFile(file.getName(), null, file.getParentFile());
172171
try {
173172
// fix from cjwant to prevent symlinks from being destroyed.
@@ -178,9 +177,11 @@ static public void saveFile(String str, File file) throws IOException {
178177
throw new IOException("Could not resolve canonical representation of " +
179178
file.getAbsolutePath());
180179
}
181-
// Can't use saveStrings() here b/c Windows will add a ^M to the file
180+
// Could use saveStrings(), but the we wouldn't be able to checkError()
182181
PrintWriter writer = PApplet.createWriter(temp);
183-
writer.print(str);
182+
for (String line : lines) {
183+
writer.println(line);
184+
}
184185
boolean error = writer.checkError(); // calls flush()
185186
writer.close(); // attempt to close regardless
186187
if (error) {

todo.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ X menu bars broken in High Sierra
99
X https://github.com/processing/processing/issues/5272
1010
X no more responses, had to close
1111

12+
_ change up the newline handling
13+
_ https://github.com/processing/processing/issues/5327
14+
_ explanation: https://stackoverflow.com/a/729795
15+
_ http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_206
16+
1217
jakub
1318
X Fix scrub comments for empty block comment /**/
1419
X https://github.com/processing/processing/pull/5265
@@ -56,8 +61,6 @@ _ windows defender blocks processing 3.3.6
5661
_ https://github.com/processing/processing/issues/5329
5762
_ Rename dialog is unusable on high density screen
5863
_ https://github.com/processing/processing/issues/5368
59-
_ change up the newline handling
60-
_ https://github.com/processing/processing/issues/5327
6164
_ Manager fails to complete install of PythonMode when no windows open
6265
_ https://github.com/processing/processing/issues/5309
6366
_ update to launch4j 3.11?

0 commit comments

Comments
 (0)