From d70900e5adcdbbfab15560d140ec6d58ea3f6ed5 Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Thu, 17 Dec 2015 12:01:24 +0100 Subject: [PATCH] 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. --- app/src/processing/app/EditorHeader.java | 7 ++--- app/src/processing/app/SketchController.java | 16 +++--------- arduino-core/src/cc/arduino/Compiler.java | 2 +- .../src/processing/app/SketchCode.java | 26 ++++++++++++------- 4 files changed, 24 insertions(+), 27 deletions(-) diff --git a/app/src/processing/app/EditorHeader.java b/app/src/processing/app/EditorHeader.java index 22895e7e6..9f5e2a0fe 100644 --- a/app/src/processing/app/EditorHeader.java +++ b/app/src/processing/app/EditorHeader.java @@ -245,9 +245,7 @@ public class EditorHeader extends JComponent { int i = 0; for (EditorTab tab : tabs) { SketchCode code = tab.getSketchCode(); - - String codeName = code.isExtension(sketch.getHiddenExtensions()) ? - code.getPrettyName() : code.getFileName(); + String codeName = code.getPrettyName(); // if modified, add the li'l glyph next to the name String text = " " + codeName + (code.isModified() ? " \u00A7" : " "); @@ -329,8 +327,7 @@ public class EditorHeader extends JComponent { for (EditorTab tab : editor.getTabs()) { SketchCode code = tab.getSketchCode(); final int index = i++; - item = new JMenuItem(code.isExtension(sketch.getDefaultExtension()) ? - code.getPrettyName() : code.getFileName()); + item = new JMenuItem(code.getPrettyName()); item.addActionListener((ActionEvent e) -> { editor.selectTab(index); }); diff --git a/app/src/processing/app/SketchController.java b/app/src/processing/app/SketchController.java index 97df7b8d1..f9c18a892 100644 --- a/app/src/processing/app/SketchController.java +++ b/app/src/processing/app/SketchController.java @@ -41,7 +41,6 @@ import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; -import java.util.Arrays; import java.util.LinkedList; import java.util.List; import java.util.Optional; @@ -120,8 +119,7 @@ public class SketchController { renamingCode = true; String prompt = current.isPrimary() ? "New name for sketch:" : "New name for file:"; - String oldName = (current.isExtension("ino")) ? current.getPrettyName() - : current.getFileName(); + String oldName = current.getPrettyName(); editor.status.edit(prompt, oldName); } @@ -226,7 +224,7 @@ public class SketchController { if (renamingCode && current.isPrimary()) { for (SketchCode code : sketch.getCodes()) { - if (sanitaryName.equalsIgnoreCase(code.getPrettyName()) && + if (sanitaryName.equalsIgnoreCase(code.getBaseName()) && code.isExtension("cpp")) { Base.showMessage(tr("Error"), I18n.format(tr("You can't rename the sketch to \"{0}\"\n" @@ -400,7 +398,7 @@ public class SketchController { String prompt = current.isPrimary() ? tr("Are you sure you want to delete this sketch?") : I18n.format(tr("Are you sure you want to delete \"{0}\"?"), - current.getFileNameWithExtensionIfNotIno()); + current.getPrettyName()); int result = JOptionPane.showOptionDialog(editor, prompt, tr("Delete"), @@ -571,7 +569,7 @@ public class SketchController { // but ignore this situation for the first tab, since it's probably being // resaved (with the same name) to another location/folder. for (SketchCode code : sketch.getCodes()) { - if (!code.isPrimary() && newName.equalsIgnoreCase(code.getPrettyName())) { + if (!code.isPrimary() && newName.equalsIgnoreCase(code.getBaseName())) { Base.showMessage(tr("Error"), I18n.format(tr("You can't save the sketch as \"{0}\"\n" + "because the sketch already has a file with that name."), newName @@ -1081,12 +1079,6 @@ public class SketchController { return Sketch.EXTENSIONS.contains(what); } - static private final List hiddenExtensions = Arrays.asList("ino", "pde"); - - public List getHiddenExtensions() { - return hiddenExtensions; - } - /** * Create the data folder if it does not exist already. As a convenience, * it also returns the data folder, since it's likely about to be used. diff --git a/arduino-core/src/cc/arduino/Compiler.java b/arduino-core/src/cc/arduino/Compiler.java index 451d2c1ec..f800f32c8 100644 --- a/arduino-core/src/cc/arduino/Compiler.java +++ b/arduino-core/src/cc/arduino/Compiler.java @@ -585,7 +585,7 @@ public class Compiler implements MessageConsumer { if (exception != null) { SketchCode code = sketch.getCode(exception.getCodeIndex()); - String fileName = (code.isExtension("ino") || code.isExtension("pde")) ? code.getPrettyName() : code.getFileName(); + String fileName = code.getPrettyName(); int lineNum = exception.getCodeLine() + 1; s = fileName + ":" + lineNum + ": error: " + error + msg; } diff --git a/arduino-core/src/processing/app/SketchCode.java b/arduino-core/src/processing/app/SketchCode.java index dac194568..576ccbac2 100644 --- a/arduino-core/src/processing/app/SketchCode.java +++ b/arduino-core/src/processing/app/SketchCode.java @@ -161,22 +161,30 @@ public class SketchCode { } + /* + * Returns the filename include extension. + */ public String getFileName() { return file.getName(); } - + /** + * Returns the filename without extension for normal sketch files + * (Sketch.SKETCH_EXTENSIONS) and the filename with extension for all + * others. + */ public String getPrettyName() { - String prettyName = getFileName(); - int dot = prettyName.lastIndexOf('.'); - return prettyName.substring(0, dot); + if (isExtension(Sketch.SKETCH_EXTENSIONS)) + return getBaseName(); + else + return getFileName(); } - public String getFileNameWithExtensionIfNotIno() { - if (getFileName().endsWith(".ino")) { - return getPrettyName(); - } - return getFileName(); + /** + * Returns the filename without extension + */ + public String getBaseName() { + return FileUtils.splitFilename(file).basename; } public boolean isExtension(String... extensions) {