Skip to content

Commit 968c8ee

Browse files
Clean up SketchController.nameCode a bit
This lets it use FileUtils.splitFilename and reference Sketch.EXTENSIONS and the new Sketch.DEFAULT_SKETCH_EXTENSION directly, allowing to remove a few helper functions.
1 parent 46fe93b commit 968c8ee

File tree

2 files changed

+12
-33
lines changed

2 files changed

+12
-33
lines changed

app/src/processing/app/SketchController.java

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ protected void nameCode(String newName) {
141141

142142
// Add the extension here, this simplifies some of the logic below.
143143
if (newName.indexOf('.') == -1) {
144-
newName += "." + sketch.getDefaultExtension();
144+
newName += "." + Sketch.DEFAULT_SKETCH_EXTENSION;
145145
}
146146

147147
// if renaming to the same thing as before, just ignore.
@@ -168,17 +168,17 @@ protected void nameCode(String newName) {
168168
return;
169169
}
170170

171-
String newExtension = newName.substring(dot+1).toLowerCase();
172-
if (!validExtension(newExtension)) {
171+
FileUtils.SplitFile split = FileUtils.splitFilename(newName);
172+
if (!Sketch.EXTENSIONS.contains(split.extension)) {
173173
Base.showWarning(tr("Problem with rename"),
174-
I18n.format(
175-
tr("\".{0}\" is not a valid extension."), newExtension
176-
), null);
174+
I18n.format(tr("\".{0}\" is not a valid extension."),
175+
split.extension),
176+
null);
177177
return;
178178
}
179179

180180
// Don't let the user create the main tab as a .java file instead of .pde
181-
if (!isDefaultExtension(newExtension)) {
181+
if (!split.extension.equals(Sketch.DEFAULT_SKETCH_EXTENSION)) {
182182
if (renamingCode) { // If creating a new tab, don't show this error
183183
if (current.isPrimary()) { // If this is the main tab, disallow
184184
Base.showWarning(tr("Problem with rename"),
@@ -190,14 +190,9 @@ protected void nameCode(String newName) {
190190
}
191191
}
192192

193-
// dots are allowed for the .pde and .java, but not in the name
194-
// make sure the user didn't name things poo.time.pde
195-
// or something like that (nothing against poo time)
196-
String shortName = newName.substring(0, dot);
197-
String sanitaryName = BaseNoGui.sanitizeName(shortName);
198-
if (!shortName.equals(sanitaryName)) {
199-
newName = sanitaryName + "." + newExtension;
200-
}
193+
// Sanitize name
194+
String sanitaryName = BaseNoGui.sanitizeName(split.basename);
195+
newName = sanitaryName + "." + split.extension;
201196

202197
// In Arduino, we want to allow files with the same name but different
203198
// extensions, so compare the full names (including extensions). This
@@ -1050,23 +1045,6 @@ private boolean hasDefaultExtension(SketchCode code) {
10501045
return code.isExtension(sketch.getDefaultExtension());
10511046
}
10521047

1053-
1054-
/**
1055-
* True if the specified extension is the default file extension.
1056-
*/
1057-
private boolean isDefaultExtension(String what) {
1058-
return what.equals(sketch.getDefaultExtension());
1059-
}
1060-
1061-
1062-
/**
1063-
* Check this extension (no dots, please) against the list of valid
1064-
* extensions.
1065-
*/
1066-
private boolean validExtension(String what) {
1067-
return Sketch.EXTENSIONS.contains(what);
1068-
}
1069-
10701048
/**
10711049
* Create the data folder if it does not exist already. As a convenience,
10721050
* it also returns the data folder, since it's likely about to be used.

arduino-core/src/processing/app/Sketch.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
*/
1616
public class Sketch {
1717

18-
public static final List<String> SKETCH_EXTENSIONS = Arrays.asList("ino", "pde");
18+
public static final String DEFAULT_SKETCH_EXTENSION = "ino";
19+
public static final List<String> SKETCH_EXTENSIONS = Arrays.asList(DEFAULT_SKETCH_EXTENSION, "pde");
1920
public static final List<String> OTHER_ALLOWED_EXTENSIONS = Arrays.asList("c", "cpp", "h", "hh", "hpp", "s");
2021
public static final List<String> EXTENSIONS = Stream.concat(SKETCH_EXTENSIONS.stream(), OTHER_ALLOWED_EXTENSIONS.stream()).collect(Collectors.toList());
2122

0 commit comments

Comments
 (0)