Open History to see the last commit
1import * as Schema from "effect/Schema";
2import { TrimmedNonEmptyString } from "./baseSchemas.ts";
3
4export const EditorLaunchStyle = Schema.Literals(["direct-path", "goto", "line-column"]);
5export type EditorLaunchStyle = typeof EditorLaunchStyle.Type;
6
7type EditorDefinition = {
8 readonly id: string;
9 readonly label: string;
10 readonly commands: readonly [string, ...string[]] | null;
11 readonly baseArgs?: readonly string[];
12 readonly launchStyle: EditorLaunchStyle;
13 /**
14 * URL scheme for editors that support VS Code's remote deep links
15 * (`<scheme>://vscode-remote/ssh-remote+<host><path>`). Only set for VS Code
16 * and forks that ship the Remote-SSH machinery, plus Zed, which uses its own
17 * `zed://ssh/<host><path>` shape.
18 */
19 readonly remoteScheme?: string;
20};
21
22export const EDITORS = [
23 {
24 id: "cursor",
25 label: "Cursor",
26 commands: ["cursor"],
27 // File and workspace opens must target the IDE even when the Agents Window is active.
28 baseArgs: ["--classic"],
29 launchStyle: "goto",
30 remoteScheme: "cursor",
31 },
32 { id: "trae", label: "Trae", commands: ["trae"], launchStyle: "goto" },
33 { id: "kiro", label: "Kiro", commands: ["kiro"], baseArgs: ["ide"], launchStyle: "goto" },
34 {
35 id: "vscode",
36 label: "VS Code",
37 commands: ["code"],
38 launchStyle: "goto",
39 remoteScheme: "vscode",
40 },
41 {
42 id: "vscode-insiders",
43 label: "VS Code Insiders",
44 commands: ["code-insiders"],
45 launchStyle: "goto",
46 remoteScheme: "vscode-insiders",
47 },
48 {
49 id: "vscodium",
50 label: "VSCodium",
51 commands: ["codium"],
52 launchStyle: "goto",
53 remoteScheme: "vscodium",
54 },
55 {
56 id: "zed",
57 label: "Zed",
58 commands: ["zed", "zeditor"],
59 launchStyle: "direct-path",
60 remoteScheme: "zed",
61 },
62 {
63 id: "antigravity",
64 label: "Antigravity",
65 // `agy` is the standalone Antigravity CLI, not the IDE. The IDE bundle
66 // ships `antigravity-ide`, so it comes first for install-folder lookups.
67 commands: ["antigravity-ide", "agy-ide"],
68 launchStyle: "goto",
69 },
70 { id: "idea", label: "IntelliJ IDEA", commands: ["idea"], launchStyle: "line-column" },
71 { id: "aqua", label: "Aqua", commands: ["aqua"], launchStyle: "line-column" },
72 { id: "clion", label: "CLion", commands: ["clion"], launchStyle: "line-column" },
73 { id: "datagrip", label: "DataGrip", commands: ["datagrip"], launchStyle: "line-column" },
74 { id: "dataspell", label: "DataSpell", commands: ["dataspell"], launchStyle: "line-column" },
75 { id: "goland", label: "GoLand", commands: ["goland"], launchStyle: "line-column" },
76 { id: "phpstorm", label: "PhpStorm", commands: ["phpstorm"], launchStyle: "line-column" },
77 { id: "pycharm", label: "PyCharm", commands: ["pycharm"], launchStyle: "line-column" },
78 { id: "rider", label: "Rider", commands: ["rider"], launchStyle: "line-column" },
79 { id: "rubymine", label: "RubyMine", commands: ["rubymine"], launchStyle: "line-column" },
80 { id: "rustrover", label: "RustRover", commands: ["rustrover"], launchStyle: "line-column" },
81 { id: "webstorm", label: "WebStorm", commands: ["webstorm"], launchStyle: "line-column" },
82 { id: "file-manager", label: "File Manager", commands: null, launchStyle: "direct-path" },
83] as const satisfies ReadonlyArray<EditorDefinition>;
84
85export const EditorId = Schema.Literals(EDITORS.map((e) => e.id));
86export type EditorId = typeof EditorId.Type;
87
88export const FileManagerRevealKind = Schema.Literals(["finder", "file-explorer", "files"]);
89export type FileManagerRevealKind = typeof FileManagerRevealKind.Type;
90
91export const LaunchEditorInput = Schema.Struct({
92 cwd: TrimmedNonEmptyString,
93 editor: EditorId,
94 /** Reveal (select) `cwd` in the file manager instead of opening it. Only
95 honored by the "file-manager" editor; clients must check the server's
96 `shellRevealInFileManager` config flag before sending this. */
97 reveal: Schema.optional(Schema.Boolean),
98});
99export type LaunchEditorInput = typeof LaunchEditorInput.Type;
100
101const remoteSchemeOf = (editor: EditorDefinition): string | undefined => editor.remoteScheme;
102
103/** Editors that can open a remote workspace via an SSH deep link. */
104export const REMOTE_CAPABLE_EDITOR_IDS: ReadonlyArray<EditorId> = EDITORS.flatMap((editor) =>
105 remoteSchemeOf(editor) !== undefined ? [editor.id] : [],
106);
107
108export const remoteSchemeForEditor = (id: EditorId): string | undefined => {
109 const editor = EDITORS.find((candidate) => candidate.id === id);
110 return editor === undefined ? undefined : remoteSchemeOf(editor);
111};
112
113/**
114 * Builds a `<scheme>://vscode-remote/ssh-remote+<host><path>` deep link (Zed
115 * takes `zed://ssh/<host><path>`) that opens `absolutePath` on `host` in the
116 * local editor over SSH. Returns undefined for editors without remote
117 * deep-link support.
118 */
119export const buildRemoteOpenUrl = (input: {
120 readonly editor: EditorId;
121 readonly host: string;
122 readonly absolutePath: string;
123}): string | undefined => {
124 const scheme = remoteSchemeForEditor(input.editor);
125 if (scheme === undefined) {
126 return undefined;
127 }
128 // Windows server paths (`C:\...`) appear as `/C:/...` in vscode-remote URIs.
129 const posixPath = input.absolutePath.replaceAll("\\", "/");
130 const rootedPath = posixPath.startsWith("/") ? posixPath : `/${posixPath}`;
131 const encodedHost = encodeURIComponent(input.host);
132 if (input.editor === "zed") {
133 // Zed's remote server resolves a rooted path on the system drive, so a
134 // Windows `C:\Users\x` must become `/Users/x` (verified in #8938). Other
135 // drives are untested and kept as is rather than silently remapped, and a
136 // POSIX path that happens to start with `/C:` is left alone.
137 const zedPath = /^[Cc]:[\\/]/.test(input.absolutePath) ? rootedPath.slice(3) : rootedPath;
138 const encodedZedPath = zedPath.split("/").map(encodeURIComponent).join("/");
139 return `${scheme}://ssh/${encodedHost}${encodedZedPath}`;
140 }
141 const encodedPath = rootedPath.split("/").map(encodeURIComponent).join("/");
142 return `${scheme}://vscode-remote/ssh-remote+${encodedHost}${encodedPath}`;
143};
144
145/**
146 * SSH hostnames an environment advertises for remote open links. Reachability
147 * is client-side; the server only advertises names that resolve to itself and
148 * gates them on a local sshd listen check. Ordered most-reachable first
149 * (tailnet MagicDNS name, then mDNS `<hostname>.local`).
150 */
151export const RemoteOpenTargetKind = Schema.Literals(["tailscale", "mdns"]);
152export type RemoteOpenTargetKind = typeof RemoteOpenTargetKind.Type;
153
154export const RemoteOpenTarget = Schema.Struct({
155 kind: RemoteOpenTargetKind,
156 host: TrimmedNonEmptyString,
157});
158export type RemoteOpenTarget = typeof RemoteOpenTarget.Type;
159
160export class ExternalLauncherUnknownEditorError extends Schema.TaggedError<ExternalLauncherUnknownEditorError>()(
161 "ExternalLauncherUnknownEditorError",
162 {
163 editor: Schema.String,
164 },
165) {
166 override get message(): string {
167 return `Unknown editor: ${this.editor}`;
168 }
169}
170
171export class ExternalLauncherUnsupportedEditorError extends Schema.TaggedError<ExternalLauncherUnsupportedEditorError>()(
172 "ExternalLauncherUnsupportedEditorError",
173 {
174 editor: EditorId,
175 },
176) {
177 override get message(): string {
178 return `Unsupported editor: ${this.editor}`;
179 }
180}
181
182export class ExternalLauncherCommandNotFoundError extends Schema.TaggedError<ExternalLauncherCommandNotFoundError>()(
183 "ExternalLauncherCommandNotFoundError",
184 {
185 editor: EditorId,
186 command: Schema.String,
187 },
188) {
189 override get message(): string {
190 return `Editor command not found: ${this.command}`;
191 }
192}
193
194const ExternalLauncherSpawnFields = {
195 command: Schema.String,
196 args: Schema.Array(Schema.String),
197 cause: Schema.Defect(),
198};
199
200export class ExternalLauncherBrowserSpawnError extends Schema.TaggedError<ExternalLauncherBrowserSpawnError>()(
201 "ExternalLauncherBrowserSpawnError",
202 {
203 ...ExternalLauncherSpawnFields,
204 target: Schema.String,
205 },
206) {
207 override get message(): string {
208 return `Failed to launch browser target '${this.target}' with '${[this.command, ...this.args].join(" ")}'`;
209 }
210}
211
212export class ExternalLauncherEditorSpawnError extends Schema.TaggedError<ExternalLauncherEditorSpawnError>()(
213 "ExternalLauncherEditorSpawnError",
214 {
215 ...ExternalLauncherSpawnFields,
216 editor: EditorId,
217 target: Schema.String,
218 },
219) {
220 override get message(): string {
221 return `Failed to launch '${this.target}' in ${this.editor} with '${[this.command, ...this.args].join(" ")}'`;
222 }
223}
224
225export const ExternalLauncherError = Schema.Union([
226 ExternalLauncherUnknownEditorError,
227 ExternalLauncherUnsupportedEditorError,
228 ExternalLauncherCommandNotFoundError,
229 ExternalLauncherBrowserSpawnError,
230 ExternalLauncherEditorSpawnError,
231]);
232export type ExternalLauncherError = typeof ExternalLauncherError.Type;