Skip to content

Commit 5e68b66

Browse files
matthijskooijmanfacchinm
authored andcommitted
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 b3ad637 commit 5e68b66

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
@@ -140,7 +140,7 @@ protected void nameCode(String newName) {
140140

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

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

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

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

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

201196
// In Arduino, we want to allow files with the same name but different
202197
// extensions, so compare the full names (including extensions). This
@@ -1058,23 +1053,6 @@ private boolean hasDefaultExtension(SketchCode code) {
10581053
return code.isExtension(sketch.getDefaultExtension());
10591054
}
10601055

1061-
1062-
/**
1063-
* True if the specified extension is the default file extension.
1064-
*/
1065-
private boolean isDefaultExtension(String what) {
1066-
return what.equals(sketch.getDefaultExtension());
1067-
}
1068-
1069-
1070-
/**
1071-
* Check this extension (no dots, please) against the list of valid
1072-
* extensions.
1073-
*/
1074-
private boolean validExtension(String what) {
1075-
return Sketch.EXTENSIONS.contains(what);
1076-
}
1077-
10781056
/**
10791057
* Create the data folder if it does not exist already. As a convenience,
10801058
* 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)