Skip to content

Commit 03d88ee

Browse files
committed
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.
1 parent 74f93fe commit 03d88ee

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

app/src/processing/app/Base.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,20 @@ protected File createNewUntitled() throws IOException {
764764
if (!newbieFile.createNewFile()) {
765765
throw new IOException();
766766
}
767-
FileUtils.copyFile(new File(getContentFile("examples"), "01.Basics" + File.separator + "BareMinimum" + File.separator + "BareMinimum.ino"), newbieFile);
767+
768+
// Initialize the pde file with the BareMinimum sketch.
769+
// Apply user-defined tab settings.
770+
String sketch = FileUtils.readFileToString(
771+
new File(getContentFile("examples"), "01.Basics" + File.separator
772+
+ "BareMinimum" + File.separator + "BareMinimum.ino"));
773+
String currentTab = " ";
774+
String newTab = (PreferencesData.getBoolean("editor.tabs.expand")
775+
? StringUtils.repeat(" ",
776+
PreferencesData.getInteger("editor.tabs.size"))
777+
: "\t");
778+
sketch = sketch.replaceAll(
779+
"(?<=(^|\n)(" + currentTab + "){0,50})" + currentTab, newTab);
780+
FileUtils.writeStringToFile(newbieFile, sketch);
768781
return newbieFile;
769782
}
770783

arduino-core/src/processing/app/helpers/FileUtils.java

+30
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import org.apache.commons.compress.utils.IOUtils;
44

55
import java.io.*;
6+
import java.nio.charset.Charset;
7+
import java.nio.charset.StandardCharsets;
68
import java.nio.file.Files;
79
import java.nio.file.Paths;
810
import java.util.*;
@@ -227,6 +229,34 @@ public static List<String> readFileToListOfStrings(File file) throws IOException
227229
}
228230
}
229231

232+
/**
233+
* Writes the given data to the given file, creating the file if it does not exist.
234+
* This method is equivalent to calling {@code writeStringToFile(file, data, StandardCharsets.UTF_8)}.
235+
* @param file - The file to write to.
236+
* @param data - The string to write.
237+
* @throws IOException If an I/O error occurs.
238+
*/
239+
public static void writeStringToFile(File file, String data) throws IOException {
240+
writeStringToFile(file, data, StandardCharsets.UTF_8);
241+
}
242+
243+
/**
244+
* Writes the given data to the given file, creating the file if it does not exist.
245+
* @param file - The file to write to.
246+
* @param data - The string to write.
247+
* @param charset - The charset used to convert the string to bytes.
248+
* @throws IOException If an I/O error occurs.
249+
*/
250+
public static void writeStringToFile(File file, String data, Charset charset) throws IOException {
251+
OutputStream out = null;
252+
try {
253+
out = new FileOutputStream(file);
254+
out.write(data.getBytes(charset));
255+
} finally {
256+
IOUtils.closeQuietly(out);
257+
}
258+
}
259+
230260

231261
/**
232262
* Returns true if the given file has any of the given extensions.

arduino-core/src/processing/app/helpers/StringUtils.java

+11
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,15 @@ public static String join(String[] arr, String separator) {
4848
}
4949
return sb.substring(0, sb.length() - separator.length());
5050
}
51+
52+
/**
53+
* Repeats a string for the given amount of times.
54+
* Example: {@code repeat("12", 4)} results in "12121212".
55+
* @param str - The string to repeat.
56+
* @param amount - The amount of times to repeat the string. Negative numbers are treated as zero.
57+
* @return The repeated string or null if str is null.
58+
*/
59+
public static String repeat(String str, int amount) {
60+
return org.apache.commons.lang3.StringUtils.repeat(str, amount);
61+
}
5162
}

0 commit comments

Comments
 (0)