Skip to content

Commit

Permalink
Use user-defined tab settings in new sketch generation
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Pieter12345 authored and facchinm committed Jul 18, 2019
1 parent 3438b86 commit 9aea65b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
15 changes: 14 additions & 1 deletion app/src/processing/app/Base.java
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,20 @@ protected File createNewUntitled() throws IOException {
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;
}

Expand Down
31 changes: 31 additions & 0 deletions arduino-core/src/processing/app/helpers/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
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;
Expand Down Expand Up @@ -146,6 +149,34 @@ public static String readFileToString(File file, String encoding) throws IOExcep
}
}

/**
* 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.
*
Expand Down

0 comments on commit 9aea65b

Please sign in to comment.