Detect the studio editor by default

This commit is contained in:
piexel committed 2026-11-24 12:05:15 +01:00
1 parent 98cd73bc8f
commit 72852b1300
8 files changed
+79 -22

No files matched your search

+2 -1
View File
@@ -152,7 +152,8 @@ Plugin:
/iris studio close /iris studio close
``` ```
`studio.editor` in `iris.json` picks the editor the studio opens: `vscode` (default), `zed` or `none`. `studio.editor` in `iris.json` picks the editor the studio opens: `auto` (default), `vscode`, `zed` or `none`.
`auto` uses Zed when it is on the `PATH` and VSCode (`code` or `codium`) is not, otherwise VSCode.
Zed is started through its CLI (`zed`, `zeditor` or `zedit` on the `PATH`) with the pack folder as Zed is started through its CLI (`zed`, `zeditor` or `zedit` on the `PATH`) with the pack folder as
the project. `/iris studio vscode` still works as an alias. the project. `/iris studio vscode` still works as an alias.
@@ -53,7 +53,7 @@ public class CommandEdit implements DirectorExecutor {
} }
private boolean editorUnavailable() { private boolean editorUnavailable() {
StudioEditor editor = StudioEditorLauncher.configuredEditor(); StudioEditor editor = StudioEditorLauncher.resolvedEditor();
if (editor == StudioEditor.NONE) { if (editor == StudioEditor.NONE) {
sender().sendMessage(IrisLanguage.text(BukkitCommandMessagesExtended.COMMAND_EDIT_NO_EDITOR_CONFIGURED)); sender().sendMessage(IrisLanguage.text(BukkitCommandMessagesExtended.COMMAND_EDIT_NO_EDITOR_CONFIGURED));
return true; return true;
@@ -67,7 +67,7 @@ public class CommandEdit implements DirectorExecutor {
} }
private void openInEditor(IrisRegistrant registrant) { private void openInEditor(IrisRegistrant registrant) {
MessageArgument editorName = MessageArgument.untrusted("editor", StudioEditorLauncher.configuredEditor().getDisplayName()); MessageArgument editorName = MessageArgument.untrusted("editor", StudioEditorLauncher.resolvedEditor().getDisplayName());
if (!StudioEditorLauncher.openFile(registrant.getLoader().getDataFolder(), registrant.getLoadFile())) { if (!StudioEditorLauncher.openFile(registrant.getLoader().getDataFolder(), registrant.getLoadFile())) {
sender().sendMessage(IrisLanguage.text(BukkitCommandMessagesExtended.COMMAND_EDIT_COULD_NOT_OPEN_EDITOR, editorName)); sender().sendMessage(IrisLanguage.text(BukkitCommandMessagesExtended.COMMAND_EDIT_COULD_NOT_OPEN_EDITOR, editorName));
return; return;
@@ -181,7 +181,7 @@ public class CommandStudio implements DirectorExecutor {
sender().sendMessage(IrisLanguage.text(BukkitCommandMessagesExtended.COMMAND_STUDIO_COULD_NOT_RESOLVE_PACK_DIMENSION, MessageArgument.untrusted("value", pack))); sender().sendMessage(IrisLanguage.text(BukkitCommandMessagesExtended.COMMAND_STUDIO_COULD_NOT_RESOLVE_PACK_DIMENSION, MessageArgument.untrusted("value", pack)));
return; return;
} }
StudioEditor editor = StudioEditorLauncher.configuredEditor(); StudioEditor editor = StudioEditorLauncher.resolvedEditor();
if (editor == StudioEditor.NONE) { if (editor == StudioEditor.NONE) {
sender().sendMessage(IrisLanguage.text(BukkitCommandMessagesExtended.COMMAND_EDIT_NO_EDITOR_CONFIGURED)); sender().sendMessage(IrisLanguage.text(BukkitCommandMessagesExtended.COMMAND_EDIT_NO_EDITOR_CONFIGURED));
return; return;
@@ -409,7 +409,7 @@ public class IrisSettings {
@Data @Data
public static class IrisSettingsStudio { public static class IrisSettingsStudio {
public StudioEditor editor = StudioEditor.VSCODE; public StudioEditor editor = StudioEditor.AUTO;
// Read-only bridge for iris.json files written before `editor` existed; Gson omits it again on save. // Read-only bridge for iris.json files written before `editor` existed; Gson omits it again on save.
@SerializedName("openVSCode") @SerializedName("openVSCode")
public Boolean legacyOpenVSCode = null; public Boolean legacyOpenVSCode = null;
@@ -421,7 +421,7 @@ public class IrisSettings {
public String electron = "electron"; public String electron = "electron";
public StudioEditor getEditor() { public StudioEditor getEditor() {
return editor == null ? StudioEditor.VSCODE : editor; return editor == null ? StudioEditor.AUTO : editor;
} }
private void migrateLegacyOpenVSCode() { private void migrateLegacyOpenVSCode() {
@@ -21,6 +21,8 @@ package art.arcane.iris.studio.workspace;
import com.google.gson.annotations.SerializedName; import com.google.gson.annotations.SerializedName;
public enum StudioEditor { public enum StudioEditor {
@SerializedName("auto")
AUTO("auto"),
@SerializedName("vscode") @SerializedName("vscode")
VSCODE("VSCode"), VSCODE("VSCode"),
@SerializedName("zed") @SerializedName("zed")
@@ -30,7 +30,6 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Stream;
/** /**
* Opens pack files in the editor chosen by {@code studio.editor}. Zed is always started on the pack folder, * Opens pack files in the editor chosen by {@code studio.editor}. Zed is always started on the pack folder,
@@ -38,27 +37,37 @@ import java.util.stream.Stream;
*/ */
public final class StudioEditorLauncher { public final class StudioEditorLauncher {
private static final List<String> ZED_COMMANDS = List.of("zed", "zeditor", "zedit"); private static final List<String> ZED_COMMANDS = List.of("zed", "zeditor", "zedit");
private static final List<String> VSCODE_COMMANDS = List.of("code", "codium");
private static final List<String> EXECUTABLE_SUFFIXES = List.of("", ".exe", ".cmd");
private StudioEditorLauncher() { private StudioEditorLauncher() {
} }
public static StudioEditor configuredEditor() { /** The editor to open, with {@link StudioEditor#AUTO} replaced by the one detected on this machine. */
return IrisSettings.get().getStudio().getEditor(); public static StudioEditor resolvedEditor() {
StudioEditor configured = IrisSettings.get().getStudio().getEditor();
return configured == StudioEditor.AUTO ? detectInstalledEditor(System.getenv("PATH")) : configured;
}
// Zed has no workspace file the OS could route to it, so it only wins when it is the sole editor found.
static StudioEditor detectInstalledEditor(String path) {
boolean onlyZedInstalled = findCommand(ZED_COMMANDS, path).isPresent() && findCommand(VSCODE_COMMANDS, path).isEmpty();
return onlyZedInstalled ? StudioEditor.ZED : StudioEditor.VSCODE;
} }
public static boolean openProject(File projectRoot, File codeWorkspace) { public static boolean openProject(File projectRoot, File codeWorkspace) {
return switch (configuredEditor()) { return switch (resolvedEditor()) {
case VSCODE -> openWithDesktop(codeWorkspace); case VSCODE -> openWithDesktop(codeWorkspace);
case ZED -> startZed(projectRoot, null); case ZED -> startZed(projectRoot, null);
case NONE -> false; case NONE, AUTO -> false;
}; };
} }
public static boolean openFile(File projectRoot, File file) { public static boolean openFile(File projectRoot, File file) {
return switch (configuredEditor()) { return switch (resolvedEditor()) {
case VSCODE -> openWithDesktop(file); case VSCODE -> openWithDesktop(file);
case ZED -> startZed(projectRoot, file); case ZED -> startZed(projectRoot, file);
case NONE -> false; case NONE, AUTO -> false;
}; };
} }
@@ -68,6 +77,7 @@ public final class StudioEditorLauncher {
case VSCODE -> !GraphicsEnvironment.isHeadless() && Desktop.isDesktopSupported(); case VSCODE -> !GraphicsEnvironment.isHeadless() && Desktop.isDesktopSupported();
case ZED -> true; case ZED -> true;
case NONE -> false; case NONE -> false;
case AUTO -> canLaunch(detectInstalledEditor(System.getenv("PATH")));
}; };
} }
@@ -90,7 +100,7 @@ public final class StudioEditorLauncher {
} }
private static boolean startZed(File projectRoot, File file) { private static boolean startZed(File projectRoot, File file) {
Optional<Path> zed = findZedCommand(); Optional<Path> zed = findCommand(ZED_COMMANDS, System.getenv("PATH"));
if (zed.isEmpty()) { if (zed.isEmpty()) {
IrisLogging.warn("Could not open Zed: none of " + String.join(", ", ZED_COMMANDS) + " is on the PATH."); IrisLogging.warn("Could not open Zed: none of " + String.join(", ", ZED_COMMANDS) + " is on the PATH.");
return false; return false;
@@ -112,17 +122,16 @@ public final class StudioEditorLauncher {
} }
} }
private static Optional<Path> findZedCommand() { private static Optional<Path> findCommand(List<String> commands, String path) {
String path = System.getenv("PATH");
if (path == null) { if (path == null) {
return Optional.empty(); return Optional.empty();
} }
List<String> directories = Arrays.stream(path.split(File.pathSeparator)) List<String> directories = Arrays.stream(path.split(File.pathSeparator))
.filter(directory -> !directory.isBlank()) .filter(directory -> !directory.isBlank())
.toList(); .toList();
return ZED_COMMANDS.stream() return commands.stream()
.flatMap(command -> directories.stream() .flatMap(command -> directories.stream()
.flatMap(directory -> Stream.of(Path.of(directory, command), Path.of(directory, command + ".exe")))) .flatMap(directory -> EXECUTABLE_SUFFIXES.stream().map(suffix -> Path.of(directory, command + suffix))))
.filter(candidate -> Files.isRegularFile(candidate) && Files.isExecutable(candidate)) .filter(candidate -> Files.isRegularFile(candidate) && Files.isExecutable(candidate))
.findFirst(); .findFirst();
} }
@@ -10,8 +10,8 @@ import static org.junit.Assert.assertFalse;
public class StudioEditorSettingTest { public class StudioEditorSettingTest {
@Test @Test
public void editorDefaultsToVSCode() { public void editorDefaultsToAuto() {
assertEquals(StudioEditor.VSCODE, IrisSettings.parseHotloadSnapshot("{}").getStudio().getEditor()); assertEquals(StudioEditor.AUTO, IrisSettings.parseHotloadSnapshot("{}").getStudio().getEditor());
} }
@Test @Test
@@ -30,7 +30,7 @@ public class StudioEditorSettingTest {
} }
@Test @Test
public void legacyOpenVSCodeTrueKeepsVSCode() { public void legacyOpenVSCodeTrueKeepsTheDefaultEditor() {
assertEquals(StudioEditor.VSCODE, IrisSettings.parseHotloadSnapshot("{\"studio\":{\"openVSCode\":true}}").getStudio().getEditor()); assertEquals(StudioEditor.AUTO, IrisSettings.parseHotloadSnapshot("{\"studio\":{\"openVSCode\":true}}").getStudio().getEditor());
} }
} }
@@ -0,0 +1,45 @@
package art.arcane.iris.studio.workspace;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import java.io.File;
import java.io.IOException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class StudioEditorLauncherTest {
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
@Test
public void autoPicksZedWhenItIsTheOnlyEditorOnThePath() throws IOException {
File bin = binWith("zeditor");
assertEquals(StudioEditor.ZED, StudioEditorLauncher.detectInstalledEditor(bin.getPath()));
}
@Test
public void autoPrefersVSCodeWhenBothEditorsAreOnThePath() throws IOException {
File zedBin = binWith("zed");
File codeBin = binWith("code");
assertEquals(StudioEditor.VSCODE, StudioEditorLauncher.detectInstalledEditor(zedBin.getPath() + File.pathSeparator + codeBin.getPath()));
}
@Test
public void autoFallsBackToVSCodeWhenNoEditorIsOnThePath() throws IOException {
assertEquals(StudioEditor.VSCODE, StudioEditorLauncher.detectInstalledEditor(temporaryFolder.newFolder().getPath()));
assertEquals(StudioEditor.VSCODE, StudioEditorLauncher.detectInstalledEditor(null));
}
private File binWith(String command) throws IOException {
File bin = temporaryFolder.newFolder();
File executable = new File(bin, command);
assertTrue(executable.createNewFile());
assertTrue(executable.setExecutable(true));
return bin;
}
}