Skip to content

Commit 66a6fc3

Browse files
Let Sketch.getPrettyName() hide extension for .ino and .pde only
Before, `getPrettyName()` would return the extension-less name for all files. There were a lot of places that checked for .ino and/or .pde files and and called `getPrettyName()` for those, and `getFileName()` for others. By moving this check into `getPrettyName()`, all those callers become more simple, and more consistent (there were 5 different checks to basically achieve the same thing). There are small changes in behaviour, where .pde is now also hidden but was not before. Also, the print header now shows extensions for other files, which makes it more consistent with the tab names. For cases where the old behaviour was still required, `Sketch.getBaseName()` was added. At the same time, the actual handling of the filenames is simplified by using methods from FileUtils. With this change `Sketch.getFileNameWithExtensionIfNotIno()` and `SketchController.getHiddenExtensions()` are no longer needed and are removed.
1 parent 16fbdf2 commit 66a6fc3

File tree

4 files changed

+24
-27
lines changed

4 files changed

+24
-27
lines changed

app/src/processing/app/EditorHeader.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,7 @@ public void paintComponent(Graphics screen) {
239239
int i = 0;
240240
for (EditorTab tab : tabs) {
241241
SketchCode code = tab.getSketchCode();
242-
243-
String codeName = code.isExtension(sketch.getHiddenExtensions()) ?
244-
code.getPrettyName() : code.getFileName();
242+
String codeName = code.getPrettyName();
245243

246244
// if modified, add the li'l glyph next to the name
247245
String text = " " + codeName + (code.isModified() ? " \u00A7" : " ");
@@ -324,8 +322,7 @@ public void rebuildMenu() {
324322
for (EditorTab tab : editor.getTabs()) {
325323
SketchCode code = tab.getSketchCode();
326324
final int index = i++;
327-
item = new JMenuItem(code.isExtension(sketch.getDefaultExtension()) ?
328-
code.getPrettyName() : code.getFileName());
325+
item = new JMenuItem(code.getPrettyName());
329326
item.addActionListener((ActionEvent e) -> {
330327
editor.selectTab(index);
331328
});

app/src/processing/app/SketchController.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
import java.io.IOException;
4343
import java.nio.file.Files;
4444
import java.nio.file.Paths;
45-
import java.util.Arrays;
4645
import java.util.LinkedList;
4746
import java.util.List;
4847
import java.util.Optional;
@@ -121,8 +120,7 @@ public void handleRenameCode() {
121120
renamingCode = true;
122121
String prompt = current.isPrimary() ?
123122
"New name for sketch:" : "New name for file:";
124-
String oldName = (current.isExtension("ino")) ? current.getPrettyName()
125-
: current.getFileName();
123+
String oldName = current.getPrettyName();
126124
editor.status.edit(prompt, oldName);
127125
}
128126

@@ -227,7 +225,7 @@ protected void nameCode(String newName) {
227225

228226
if (renamingCode && current.isPrimary()) {
229227
for (SketchCode code : sketch.getCodes()) {
230-
if (sanitaryName.equalsIgnoreCase(code.getPrettyName()) &&
228+
if (sanitaryName.equalsIgnoreCase(code.getBaseName()) &&
231229
code.isExtension("cpp")) {
232230
Base.showMessage(tr("Error"),
233231
I18n.format(tr("You can't rename the sketch to \"{0}\"\n"
@@ -401,7 +399,7 @@ public void handleDeleteCode() throws IOException {
401399
String prompt = current.isPrimary() ?
402400
tr("Are you sure you want to delete this sketch?") :
403401
I18n.format(tr("Are you sure you want to delete \"{0}\"?"),
404-
current.getFileNameWithExtensionIfNotIno());
402+
current.getPrettyName());
405403
int result = JOptionPane.showOptionDialog(editor,
406404
prompt,
407405
tr("Delete"),
@@ -572,7 +570,7 @@ protected boolean saveAs() throws IOException {
572570
// but ignore this situation for the first tab, since it's probably being
573571
// resaved (with the same name) to another location/folder.
574572
for (SketchCode code : sketch.getCodes()) {
575-
if (!code.isPrimary() && newName.equalsIgnoreCase(code.getPrettyName())) {
573+
if (!code.isPrimary() && newName.equalsIgnoreCase(code.getBaseName())) {
576574
Base.showMessage(tr("Error"),
577575
I18n.format(tr("You can't save the sketch as \"{0}\"\n" +
578576
"because the sketch already has a file with that name."), newName
@@ -1073,12 +1071,6 @@ private boolean validExtension(String what) {
10731071
return Sketch.EXTENSIONS.contains(what);
10741072
}
10751073

1076-
static private final List<String> hiddenExtensions = Arrays.asList("ino", "pde");
1077-
1078-
public List<String> getHiddenExtensions() {
1079-
return hiddenExtensions;
1080-
}
1081-
10821074
/**
10831075
* Create the data folder if it does not exist already. As a convenience,
10841076
* it also returns the data folder, since it's likely about to be used.

arduino-core/src/cc/arduino/Compiler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ public void message(String s) {
566566

567567
if (exception != null) {
568568
SketchCode code = sketch.getCode(exception.getCodeIndex());
569-
String fileName = (code.isExtension("ino") || code.isExtension("pde")) ? code.getPrettyName() : code.getFileName();
569+
String fileName = code.getPrettyName();
570570
int lineNum = exception.getCodeLine() + 1;
571571
s = fileName + ":" + lineNum + ": error: " + error + msg;
572572
}

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

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -161,22 +161,30 @@ protected boolean renameTo(File what) {
161161
}
162162

163163

164+
/*
165+
* Returns the filename include extension.
166+
*/
164167
public String getFileName() {
165168
return file.getName();
166169
}
167170

168-
171+
/**
172+
* Returns the filename without extension for normal sketch files
173+
* (Sketch.SKETCH_EXTENSIONS) and the filename with extension for all
174+
* others.
175+
*/
169176
public String getPrettyName() {
170-
String prettyName = getFileName();
171-
int dot = prettyName.lastIndexOf('.');
172-
return prettyName.substring(0, dot);
177+
if (isExtension(Sketch.SKETCH_EXTENSIONS))
178+
return getBaseName();
179+
else
180+
return getFileName();
173181
}
174182

175-
public String getFileNameWithExtensionIfNotIno() {
176-
if (getFileName().endsWith(".ino")) {
177-
return getPrettyName();
178-
}
179-
return getFileName();
183+
/**
184+
* Returns the filename without extension
185+
*/
186+
public String getBaseName() {
187+
return FileUtils.splitFilename(file).basename;
180188
}
181189

182190
public boolean isExtension(String... extensions) {

0 commit comments

Comments
 (0)