Skip to content

Use user-defined tab settings in new sketch generation #8686

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're still talking about pde files? That's so last year! 😆 🤣

// 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