Skip to content

Commit d70900e

Browse files
matthijskooijmanfacchinm
authored andcommitted
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 5792ec2 commit d70900e

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
@@ -245,9 +245,7 @@ public void paintComponent(Graphics screen) {
245245
int i = 0;
246246
for (EditorTab tab : tabs) {
247247
SketchCode code = tab.getSketchCode();
248-
249-
String codeName = code.isExtension(sketch.getHiddenExtensions()) ?
250-
code.getPrettyName() : code.getFileName();
248+
String codeName = code.getPrettyName();
251249

252250
// if modified, add the li'l glyph next to the name
253251
String text = " " + codeName + (code.isModified() ? " \u00A7" : " ");
@@ -329,8 +327,7 @@ public void rebuildMenu() {
329327
for (EditorTab tab : editor.getTabs()) {
330328
SketchCode code = tab.getSketchCode();
331329
final int index = i++;
332-
item = new JMenuItem(code.isExtension(sketch.getDefaultExtension()) ?
333-
code.getPrettyName() : code.getFileName());
330+
item = new JMenuItem(code.getPrettyName());
334331
item.addActionListener((ActionEvent e) -> {
335332
editor.selectTab(index);
336333
});

app/src/processing/app/SketchController.java

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

@@ -226,7 +224,7 @@ protected void nameCode(String newName) {
226224

227225
if (renamingCode && current.isPrimary()) {
228226
for (SketchCode code : sketch.getCodes()) {
229-
if (sanitaryName.equalsIgnoreCase(code.getPrettyName()) &&
227+
if (sanitaryName.equalsIgnoreCase(code.getBaseName()) &&
230228
code.isExtension("cpp")) {
231229
Base.showMessage(tr("Error"),
232230
I18n.format(tr("You can't rename the sketch to \"{0}\"\n"
@@ -400,7 +398,7 @@ public void handleDeleteCode() throws IOException {
400398
String prompt = current.isPrimary() ?
401399
tr("Are you sure you want to delete this sketch?") :
402400
I18n.format(tr("Are you sure you want to delete \"{0}\"?"),
403-
current.getFileNameWithExtensionIfNotIno());
401+
current.getPrettyName());
404402
int result = JOptionPane.showOptionDialog(editor,
405403
prompt,
406404
tr("Delete"),
@@ -571,7 +569,7 @@ protected boolean saveAs() throws IOException {
571569
// but ignore this situation for the first tab, since it's probably being
572570
// resaved (with the same name) to another location/folder.
573571
for (SketchCode code : sketch.getCodes()) {
574-
if (!code.isPrimary() && newName.equalsIgnoreCase(code.getPrettyName())) {
572+
if (!code.isPrimary() && newName.equalsIgnoreCase(code.getBaseName())) {
575573
Base.showMessage(tr("Error"),
576574
I18n.format(tr("You can't save the sketch as \"{0}\"\n" +
577575
"because the sketch already has a file with that name."), newName
@@ -1081,12 +1079,6 @@ private boolean validExtension(String what) {
10811079
return Sketch.EXTENSIONS.contains(what);
10821080
}
10831081

1084-
static private final List<String> hiddenExtensions = Arrays.asList("ino", "pde");
1085-
1086-
public List<String> getHiddenExtensions() {
1087-
return hiddenExtensions;
1088-
}
1089-
10901082
/**
10911083
* Create the data folder if it does not exist already. As a convenience,
10921084
* 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
@@ -585,7 +585,7 @@ public void message(String s) {
585585

586586
if (exception != null) {
587587
SketchCode code = sketch.getCode(exception.getCodeIndex());
588-
String fileName = (code.isExtension("ino") || code.isExtension("pde")) ? code.getPrettyName() : code.getFileName();
588+
String fileName = code.getPrettyName();
589589
int lineNum = exception.getCodeLine() + 1;
590590
s = fileName + ":" + lineNum + ": error: " + error + msg;
591591
}

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)