1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-11-29 10:24:12 +01:00

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.
This commit is contained in:
Pieter12345 2019-03-20 23:53:41 +01:00 committed by Martino Facchin
parent 3438b86699
commit 9aea65bee6
2 changed files with 45 additions and 1 deletions

View File

@ -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;
}

View File

@ -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.
*