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
```
`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
the project. `/iris studio vscode` still works as an alias.
@@ -53,7 +53,7 @@ public class CommandEdit implements DirectorExecutor {
}
private boolean editorUnavailable() {
StudioEditor editor = StudioEditorLauncher.configuredEditor();
StudioEditor editor = StudioEditorLauncher.resolvedEditor();
if (editor == StudioEditor.NONE) {
sender().sendMessage(IrisLanguage.text(BukkitCommandMessagesExtended.COMMAND_EDIT_NO_EDITOR_CONFIGURED));
return true;
@@ -67,7 +67,7 @@ public class CommandEdit implements DirectorExecutor {
}
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())) {
sender().sendMessage(IrisLanguage.text(BukkitCommandMessagesExtended.COMMAND_EDIT_COULD_NOT_OPEN_EDITOR, editorName));
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)));
return;
}
StudioEditor editor = StudioEditorLauncher.configuredEditor();
StudioEditor editor = StudioEditorLauncher.resolvedEditor();
if (editor == StudioEditor.NONE) {
sender().sendMessage(IrisLanguage.text(BukkitCommandMessagesExtended.COMMAND_EDIT_NO_EDITOR_CONFIGURED));
return;
@@ -409,7 +409,7 @@ public class IrisSettings {
@Data
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.
@SerializedName("openVSCode")
public Boolean legacyOpenVSCode = null;
@@ -421,7 +421,7 @@ public class IrisSettings {
public String electron = "electron";
public StudioEditor getEditor() {
return editor == null ? StudioEditor.VSCODE : editor;
return editor == null ? StudioEditor.AUTO : editor;
}
private void migrateLegacyOpenVSCode() {
@@ -21,6 +21,8 @@ package art.arcane.iris.studio.workspace;
import com.google.gson.annotations.SerializedName;
public enum StudioEditor {
@SerializedName("auto")
AUTO("auto"),
@SerializedName("vscode")
VSCODE("VSCode"),
@SerializedName("zed")
@@ -30,7 +30,6 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
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,
@@ -38,27 +37,37 @@ import java.util.stream.Stream;
*/
public final class StudioEditorLauncher {
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() {
}
public static StudioEditor configuredEditor() {
return IrisSettings.get().getStudio().getEditor();
/** The editor to open, with {@link StudioEditor#AUTO} replaced by the one detected on this machine. */
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) {
return switch (configuredEditor()) {
return switch (resolvedEditor()) {
case VSCODE -> openWithDesktop(codeWorkspace);
case ZED -> startZed(projectRoot, null);
case NONE -> false;
case NONE, AUTO -> false;
};
}
public static boolean openFile(File projectRoot, File file) {
return switch (configuredEditor()) {
return switch (resolvedEditor()) {
case VSCODE -> openWithDesktop(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 ZED -> true;
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) {
Optional<Path> zed = findZedCommand();
Optional<Path> zed = findCommand(ZED_COMMANDS, System.getenv("PATH"));
if (zed.isEmpty()) {
IrisLogging.warn("Could not open Zed: none of " + String.join(", ", ZED_COMMANDS) + " is on the PATH.");
return false;
@@ -112,17 +122,16 @@ public final class StudioEditorLauncher {
}
}
private static Optional<Path> findZedCommand() {
String path = System.getenv("PATH");
private static Optional<Path> findCommand(List<String> commands, String path) {
if (path == null) {
return Optional.empty();
}
List<String> directories = Arrays.stream(path.split(File.pathSeparator))
.filter(directory -> !directory.isBlank())
.toList();
return ZED_COMMANDS.stream()
return commands.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))
.findFirst();
}
@@ -10,8 +10,8 @@ import static org.junit.Assert.assertFalse;
public class StudioEditorSettingTest {
@Test
public void editorDefaultsToVSCode() {
assertEquals(StudioEditor.VSCODE, IrisSettings.parseHotloadSnapshot("{}").getStudio().getEditor());
public void editorDefaultsToAuto() {
assertEquals(StudioEditor.AUTO, IrisSettings.parseHotloadSnapshot("{}").getStudio().getEditor());
}
@Test
@@ -30,7 +30,7 @@ public class StudioEditorSettingTest {
}
@Test
public void legacyOpenVSCodeTrueKeepsVSCode() {
assertEquals(StudioEditor.VSCODE, IrisSettings.parseHotloadSnapshot("{\"studio\":{\"openVSCode\":true}}").getStudio().getEditor());
public void legacyOpenVSCodeTrueKeepsTheDefaultEditor() {
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;
}
}