diff --git a/app/src/processing/app/SketchController.java b/app/src/processing/app/SketchController.java index 81af6e1b0..7dae0d8f1 100644 --- a/app/src/processing/app/SketchController.java +++ b/app/src/processing/app/SketchController.java @@ -41,6 +41,7 @@ import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; +import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Optional; @@ -448,13 +449,13 @@ public class SketchController { } // rename .pde files to .ino - File mainFile = new File(sketch.getMainFilePath()); - File mainFolder = mainFile.getParentFile(); - File[] pdeFiles = mainFolder.listFiles((dir, name) -> { - return name.toLowerCase().endsWith(".pde"); - }); + List oldFiles = new ArrayList<>(); + for (SketchFile file : sketch.getFiles()) { + if (file.isExtension(Sketch.OLD_SKETCH_EXTENSIONS)) + oldFiles.add(file); + } - if (pdeFiles != null && pdeFiles.length > 0) { + if (oldFiles.size() > 0) { if (PreferencesData.get("editor.update_extension") == null) { Object[] options = {tr("OK"), tr("Cancel")}; int result = JOptionPane.showOptionDialog(editor, @@ -479,8 +480,10 @@ public class SketchController { if (PreferencesData.getBoolean("editor.update_extension")) { // Do rename of all .pde files to new .ino extension - for (File pdeFile : pdeFiles) - renameCodeToInoExtension(pdeFile); + for (SketchFile file : oldFiles) { + File newName = FileUtils.replaceExtension(file.getFile(), Sketch.DEFAULT_SKETCH_EXTENSION); + file.renameTo(newName); + } } } @@ -488,20 +491,6 @@ public class SketchController { return true; } - - private boolean renameCodeToInoExtension(File pdeFile) { - for (SketchFile file : sketch.getFiles()) { - if (!file.getFile().equals(pdeFile)) - continue; - - String pdeName = pdeFile.getPath(); - pdeName = pdeName.substring(0, pdeName.length() - 4) + ".ino"; - return file.renameTo(new File(pdeName)); - } - return false; - } - - /** * Handles 'Save As' for a sketch. *

diff --git a/arduino-core/src/processing/app/Sketch.java b/arduino-core/src/processing/app/Sketch.java index 704364b88..134fb1f02 100644 --- a/arduino-core/src/processing/app/Sketch.java +++ b/arduino-core/src/processing/app/Sketch.java @@ -14,9 +14,9 @@ import static processing.app.I18n.tr; * This represents a single sketch, consisting of one or more files. */ public class Sketch { - public static final String DEFAULT_SKETCH_EXTENSION = "ino"; - public static final List SKETCH_EXTENSIONS = Arrays.asList(DEFAULT_SKETCH_EXTENSION, "pde"); + public static final List OLD_SKETCH_EXTENSIONS = Arrays.asList("pde"); + public static final List SKETCH_EXTENSIONS = Stream.concat(Stream.of(DEFAULT_SKETCH_EXTENSION), OLD_SKETCH_EXTENSIONS.stream()).collect(Collectors.toList()); public static final List OTHER_ALLOWED_EXTENSIONS = Arrays.asList("c", "cpp", "h", "hh", "hpp", "s"); public static final List EXTENSIONS = Stream.concat(SKETCH_EXTENSIONS.stream(), OTHER_ALLOWED_EXTENSIONS.stream()).collect(Collectors.toList()); diff --git a/arduino-core/src/processing/app/helpers/FileUtils.java b/arduino-core/src/processing/app/helpers/FileUtils.java index 4083a0a69..a4fdb53e7 100644 --- a/arduino-core/src/processing/app/helpers/FileUtils.java +++ b/arduino-core/src/processing/app/helpers/FileUtils.java @@ -245,6 +245,26 @@ public class FileUtils { return extensions.contains(extension.toLowerCase()); } + /** + * Returns the given filename with the extension replaced by the one + * given. If the filename does not have an extension yet, one is + * added. + */ + public static String replaceExtension(String filename, String extension) { + SplitFile split = splitFilename(filename); + split.extension = extension; + return split.join(); + } + + /** + * Returns the given filename with the extension replaced by the one + * given. If the filename does not have an extension yet, one is + * added. + */ + public static File replaceExtension(File file, String extension) { + return new File(file.getParentFile(), replaceExtension(file.getName(), extension)); + } + /** * The result of a splitFilename call. */ @@ -256,6 +276,13 @@ public class FileUtils { public String basename; public String extension; + + public String join() { + if (extension.equals("")) + return basename; + else + return basename + "." + extension; + } } /**