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 readonly remoteScheme?: string;
14 readonly jetbrainsProductCode?: string;
15};
16
17export const EDITORS = [
18 {
19 id: "cursor",
20 label: "Cursor",
21 commands: ["cursor"],
22 // File and workspace opens must target the IDE even when the Agents Window is active.
23 baseArgs: ["--classic"],
24 launchStyle: "goto",
25 remoteScheme: "cursor",
26 },
27 { id: "trae", label: "Trae", commands: ["trae"], launchStyle: "goto" },
28 { id: "kiro", label: "Kiro", commands: ["kiro"], baseArgs: ["ide"], launchStyle: "goto" },
29 {
30 id: "vscode",
31 label: "VS Code",
32 commands: ["code"],
33 launchStyle: "goto",
34 remoteScheme: "vscode",
35 },
36 {
37 id: "vscode-insiders",
38 label: "VS Code Insiders",
39 commands: ["code-insiders"],
40 launchStyle: "goto",
41 remoteScheme: "vscode-insiders",
42 },
43 {
44 id: "vscodium",
45 label: "VSCodium",
46 commands: ["codium"],
47 launchStyle: "goto",
48 remoteScheme: "vscodium",
49 },
50 {
51 id: "zed",
52 label: "Zed",
53 commands: ["zed", "zeditor"],
54 launchStyle: "direct-path",
55 remoteScheme: "zed",
56 },
57 {
58 id: "antigravity",
59 label: "Antigravity",
60 // `agy` is the standalone Antigravity CLI, not the IDE. The IDE bundle
61 // ships `antigravity-ide`, so it comes first for install-folder lookups.
62 commands: ["antigravity-ide", "agy-ide"],
63 launchStyle: "goto",
64 },
65 {
66 id: "idea",
67 label: "IntelliJ IDEA",
68 commands: ["idea"],
69 launchStyle: "line-column",
70 jetbrainsProductCode: "IU",
71 },
72 {
73 id: "aqua",
74 label: "Aqua",
75 commands: ["aqua"],
76 launchStyle: "line-column",
77 jetbrainsProductCode: "QA",
78 },
79 {
80 id: "clion",
81 label: "CLion",
82 commands: ["clion"],
83 launchStyle: "line-column",
84 jetbrainsProductCode: "CL",
85 },
86 {
87 id: "datagrip",
88 label: "DataGrip",
89 commands: ["datagrip"],
90 launchStyle: "line-column",
91 jetbrainsProductCode: "DB",
92 },
93 {
94 id: "dataspell",
95 label: "DataSpell",
96 commands: ["dataspell"],
97 launchStyle: "line-column",
98 jetbrainsProductCode: "DS",
99 },
100 {
101 id: "goland",
102 label: "GoLand",
103 commands: ["goland"],
104 launchStyle: "line-column",
105 jetbrainsProductCode: "GO",
106 },
107 {
108 id: "phpstorm",
109 label: "PhpStorm",
110 commands: ["phpstorm"],
111 launchStyle: "line-column",
112 jetbrainsProductCode: "PS",
113 },
114 {
115 id: "pycharm",
116 label: "PyCharm",
117 commands: ["pycharm"],
118 launchStyle: "line-column",
119 jetbrainsProductCode: "PY",
120 },
121 {
122 id: "rider",
123 label: "Rider",
124 commands: ["rider"],
125 launchStyle: "line-column",
126 jetbrainsProductCode: "RD",
127 },
128 {
129 id: "rubymine",
130 label: "RubyMine",
131 commands: ["rubymine"],
132 launchStyle: "line-column",
133 jetbrainsProductCode: "RM",
134 },
135 {
136 id: "rustrover",
137 label: "RustRover",
138 commands: ["rustrover"],
139 launchStyle: "line-column",
140 jetbrainsProductCode: "RR",
141 },
142 {
143 id: "webstorm",
144 label: "WebStorm",
145 commands: ["webstorm"],
146 launchStyle: "line-column",
147 jetbrainsProductCode: "WS",
148 },
149 { id: "file-manager", label: "File Manager", commands: null, launchStyle: "direct-path" },
150] as const satisfies ReadonlyArray<EditorDefinition>;
151
152export const EditorId = Schema.Literals(EDITORS.map((e) => e.id));
153export type EditorId = typeof EditorId.Type;
154
155export const FileManagerRevealKind = Schema.Literals(["finder", "file-explorer", "files"]);
156export type FileManagerRevealKind = typeof FileManagerRevealKind.Type;
157
158export const LaunchEditorInput = Schema.Struct({
159 cwd: TrimmedNonEmptyString,
160 editor: EditorId,
161 /** Reveal (select) `cwd` in the file manager instead of opening it. Only
162 honored by the "file-manager" editor; clients must check the server's
163 `shellRevealInFileManager` config flag before sending this. */
164 reveal: Schema.optional(Schema.Boolean),
165});
166export type LaunchEditorInput = typeof LaunchEditorInput.Type;
167
168const remoteSchemeOf = (editor: EditorDefinition): string | undefined =>
169 editor.remoteScheme ?? (editor.jetbrainsProductCode === undefined ? undefined : "jetbrains");
170
171export const REMOTE_CAPABLE_EDITOR_IDS: ReadonlyArray<EditorId> = EDITORS.flatMap((editor) =>
172 remoteSchemeOf(editor) !== undefined ? [editor.id] : [],
173);
174
175export const remoteSchemeForEditor = (id: EditorId): string | undefined => {
176 const editor = EDITORS.find((candidate) => candidate.id === id);
177 return editor === undefined ? undefined : remoteSchemeOf(editor);
178};
179
180export const buildRemoteOpenUrl = (input: {
181 readonly editor: EditorId;
182 readonly host: string;
183 readonly username?: string;
184 readonly absolutePath: string;
185 readonly isFile?: boolean;
186}): string | undefined => {
187 const scheme = remoteSchemeForEditor(input.editor);
188 if (scheme === undefined) {
189 return undefined;
190 }
191 const editor = EDITORS.find((candidate) => candidate.id === input.editor);
192 if (editor !== undefined && "jetbrainsProductCode" in editor) {
193 const params = new URLSearchParams({
194 h: input.host,
195 launchIde: "true",
196 ideHint: editor.jetbrainsProductCode,
197 projectHint: input.absolutePath.replaceAll("\\", "/"),
198 });
199 if (input.username !== undefined) {
200 params.set("u", input.username);
201 }
202 return `${scheme}://gateway/ssh/environment?${params.toString()}`;
203 }
204 // Windows server paths (`C:\...`) appear as `/C:/...` in vscode-remote URIs.
205 const posixPath = input.absolutePath.replaceAll("\\", "/");
206 const rootedPath = posixPath.startsWith("/") ? posixPath : `/${posixPath}`;
207 const encodedHost = encodeURIComponent(input.host);
208 if (input.editor === "zed") {
209 // Zed's remote server resolves a rooted path on the system drive, so a
210 // Windows `C:\Users\x` must become `/Users/x` (verified in #8938). Other
211 // drives are untested and kept as is rather than silently remapped, and a
212 // POSIX path that happens to start with `/C:` is left alone.
213 const zedPath = /^[Cc]:[\\/]/.test(input.absolutePath) ? rootedPath.slice(3) : rootedPath;
214 const encodedZedPath = zedPath.split("/").map(encodeURIComponent).join("/");
215 // Zed reads this segment as ssh URL userinfo, so the `@` stays literal.
216 const zedDestination =
217 input.username === undefined
218 ? encodedHost
219 : `${encodeURIComponent(input.username)}@${encodedHost}`;
220 return `${scheme}://ssh/${zedDestination}${encodedZedPath}`;
221 }
222 const encodedPath = rootedPath.split("/").map(encodeURIComponent).join("/");
223 const destination = input.username === undefined ? input.host : `${input.username}@${input.host}`;
224 // VS Code opens a bare vscode-remote path as a folder; a trailing position makes it open the file.
225 return `${scheme}://vscode-remote/ssh-remote+${encodeURIComponent(destination)}${encodedPath}${input.isFile ? ":1:1" : ""}`;
226};
227
228export const RemoteOpenTargetKind = Schema.Literals(["tailscale", "mdns"]);
229export type RemoteOpenTargetKind = typeof RemoteOpenTargetKind.Type;
230
231export const RemoteOpenTarget = Schema.Struct({
232 kind: RemoteOpenTargetKind,
233 host: TrimmedNonEmptyString,
234 username: Schema.optionalKey(TrimmedNonEmptyString),
235});
236export type RemoteOpenTarget = typeof RemoteOpenTarget.Type;
237
238export class ExternalLauncherUnknownEditorError extends Schema.TaggedError<ExternalLauncherUnknownEditorError>()(
239 "ExternalLauncherUnknownEditorError",
240 {
241 editor: Schema.String,
242 },
243) {
244 override get message(): string {
245 return `Unknown editor: ${this.editor}`;
246 }
247}
248
249export class ExternalLauncherUnsupportedEditorError extends Schema.TaggedError<ExternalLauncherUnsupportedEditorError>()(
250 "ExternalLauncherUnsupportedEditorError",
251 {
252 editor: EditorId,
253 },
254) {
255 override get message(): string {
256 return `Unsupported editor: ${this.editor}`;
257 }
258}
259
260export class ExternalLauncherCommandNotFoundError extends Schema.TaggedError<ExternalLauncherCommandNotFoundError>()(
261 "ExternalLauncherCommandNotFoundError",
262 {
263 editor: EditorId,
264 command: Schema.String,
265 },
266) {
267 override get message(): string {
268 return `Editor command not found: ${this.command}`;
269 }
270}
271
272export class ExternalLauncherUnsupportedTargetError extends Schema.TaggedError<ExternalLauncherUnsupportedTargetError>()(
273 "ExternalLauncherUnsupportedTargetError",
274 {
275 editor: EditorId,
276 },
277) {
278 override get message(): string {
279 return `${this.editor} cannot open a path containing line breaks or double quotes`;
280 }
281}
282
283const ExternalLauncherSpawnFields = {
284 command: Schema.String,
285 args: Schema.Array(Schema.String),
286 cause: Schema.Defect(),
287};
288
289export class ExternalLauncherBrowserSpawnError extends Schema.TaggedError<ExternalLauncherBrowserSpawnError>()(
290 "ExternalLauncherBrowserSpawnError",
291 {
292 ...ExternalLauncherSpawnFields,
293 target: Schema.String,
294 },
295) {
296 override get message(): string {
297 return `Failed to launch browser target '${this.target}' with '${[this.command, ...this.args].join(" ")}'`;
298 }
299}
300
301export class ExternalLauncherEditorSpawnError extends Schema.TaggedError<ExternalLauncherEditorSpawnError>()(
302 "ExternalLauncherEditorSpawnError",
303 {
304 ...ExternalLauncherSpawnFields,
305 editor: EditorId,
306 target: Schema.String,
307 },
308) {
309 override get message(): string {
310 return `Failed to launch '${this.target}' in ${this.editor} with '${[this.command, ...this.args].join(" ")}'`;
311 }
312}
313
314export const ExternalLauncherError = Schema.Union([
315 ExternalLauncherUnknownEditorError,
316 ExternalLauncherUnsupportedEditorError,
317 ExternalLauncherCommandNotFoundError,
318 ExternalLauncherUnsupportedTargetError,
319 ExternalLauncherBrowserSpawnError,
320 ExternalLauncherEditorSpawnError,
321]);
322export type ExternalLauncherError = typeof ExternalLauncherError.Type;