Skip to content

Commit f3e787e

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 a87024d commit f3e787e

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-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

+31
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
import java.io.FileOutputStream;
99
import java.io.IOException;
1010
import java.io.InputStreamReader;
11+
import java.io.OutputStream;
12+
import java.nio.charset.Charset;
13+
import java.nio.charset.StandardCharsets;
1114
import java.nio.file.Files;
1215
import java.nio.file.Paths;
1316
import java.util.ArrayList;
@@ -146,6 +149,34 @@ public static String readFileToString(File file, String encoding) throws IOExcep
146149
}
147150
}
148151

152+
/**
153+
* Writes the given data to the given file, creating the file if it does not exist.
154+
* This method is equivalent to calling {@code writeStringToFile(file, data, StandardCharsets.UTF_8)}.
155+
* @param file - The file to write to.
156+
* @param data - The string to write.
157+
* @throws IOException If an I/O error occurs.
158+
*/
159+
public static void writeStringToFile(File file, String data) throws IOException {
160+
writeStringToFile(file, data, StandardCharsets.UTF_8);
161+
}
162+
163+
/**
164+
* Writes the given data to the given file, creating the file if it does not exist.
165+
* @param file - The file to write to.
166+
* @param data - The string to write.
167+
* @param charset - The charset used to convert the string to bytes.
168+
* @throws IOException If an I/O error occurs.
169+
*/
170+
public static void writeStringToFile(File file, String data, Charset charset) throws IOException {
171+
OutputStream out = null;
172+
try {
173+
out = new FileOutputStream(file);
174+
out.write(data.getBytes(charset));
175+
} finally {
176+
IOUtils.closeQuietly(out);
177+
}
178+
}
179+
149180
/**
150181
* Returns true if the given file has any of the given extensions.
151182
*

0 commit comments

Comments
 (0)