From 9aea65bee66da25633f9813cb3b68030c5954c46 Mon Sep 17 00:00:00 2001
From: Pieter12345
Date: Wed, 20 Mar 2019 23:53:41 +0100
Subject: [PATCH] Use user-defined tab settings in new sketch generation
When creating a new sketch, it is initialized with the BareMinimum example sketch. This example sketch uses 2-width whitespace indentation, which might differ from the user-defined tab settings. This commit makes the generated example sketch consistent with the user-defined tab settings.
---
app/src/processing/app/Base.java | 15 ++++++++-
.../src/processing/app/helpers/FileUtils.java | 31 +++++++++++++++++++
2 files changed, 45 insertions(+), 1 deletion(-)
diff --git a/app/src/processing/app/Base.java b/app/src/processing/app/Base.java
index a28d29fa9..9b3fa03f8 100644
--- a/app/src/processing/app/Base.java
+++ b/app/src/processing/app/Base.java
@@ -764,7 +764,20 @@ public class Base {
if (!newbieFile.createNewFile()) {
throw new IOException();
}
- FileUtils.copyFile(new File(getContentFile("examples"), "01.Basics" + File.separator + "BareMinimum" + File.separator + "BareMinimum.ino"), newbieFile);
+
+ // Initialize the pde file with the BareMinimum sketch.
+ // Apply user-defined tab settings.
+ String sketch = FileUtils.readFileToString(
+ new File(getContentFile("examples"), "01.Basics" + File.separator
+ + "BareMinimum" + File.separator + "BareMinimum.ino"));
+ String currentTab = " ";
+ String newTab = (PreferencesData.getBoolean("editor.tabs.expand")
+ ? StringUtils.repeat(" ",
+ PreferencesData.getInteger("editor.tabs.size"))
+ : "\t");
+ sketch = sketch.replaceAll(
+ "(?<=(^|\n)(" + currentTab + "){0,50})" + currentTab, newTab);
+ FileUtils.writeStringToFile(newbieFile, sketch);
return newbieFile;
}
diff --git a/arduino-core/src/processing/app/helpers/FileUtils.java b/arduino-core/src/processing/app/helpers/FileUtils.java
index 9694a2f5b..f2a1603b6 100644
--- a/arduino-core/src/processing/app/helpers/FileUtils.java
+++ b/arduino-core/src/processing/app/helpers/FileUtils.java
@@ -8,6 +8,9 @@ import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
@@ -146,6 +149,34 @@ public class FileUtils {
}
}
+ /**
+ * Writes the given data to the given file, creating the file if it does not exist.
+ * This method is equivalent to calling {@code writeStringToFile(file, data, StandardCharsets.UTF_8)}.
+ * @param file - The file to write to.
+ * @param data - The string to write.
+ * @throws IOException If an I/O error occurs.
+ */
+ public static void writeStringToFile(File file, String data) throws IOException {
+ writeStringToFile(file, data, StandardCharsets.UTF_8);
+ }
+
+ /**
+ * Writes the given data to the given file, creating the file if it does not exist.
+ * @param file - The file to write to.
+ * @param data - The string to write.
+ * @param charset - The charset used to convert the string to bytes.
+ * @throws IOException If an I/O error occurs.
+ */
+ public static void writeStringToFile(File file, String data, Charset charset) throws IOException {
+ OutputStream out = null;
+ try {
+ out = new FileOutputStream(file);
+ out.write(data.getBytes(charset));
+ } finally {
+ IOUtils.closeQuietly(out);
+ }
+ }
+
/**
* Returns true if the given file has any of the given extensions.
*