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 absolutePath: string;
184 readonly isFile?: boolean;
185}): string | undefined => {
186 const scheme = remoteSchemeForEditor(input.editor);
187 if (scheme === undefined) {
188 return undefined;
189 }
190 const editor = EDITORS.find((candidate) => candidate.id === input.editor);
191 if (editor !== undefined && "jetbrainsProductCode" in editor) {
192 const params = new URLSearchParams({
193 h: input.host,
194 launchIde: "true",
195 ideHint: editor.jetbrainsProductCode,
196 projectHint: input.absolutePath.replaceAll("\\", "/"),
197 });
198 return `${scheme}://gateway/ssh/environment?${params.toString()}`;
199 }
200 // Windows server paths (`C:\...`) appear as `/C:/...` in vscode-remote URIs.
201 const posixPath = input.absolutePath.replaceAll("\\", "/");
202 const rootedPath = posixPath.startsWith("/") ? posixPath : `/${posixPath}`;
203 const encodedHost = encodeURIComponent(input.host);
204 if (input.editor === "zed") {
205 // Zed's remote server resolves a rooted path on the system drive, so a
206 // Windows `C:\Users\x` must become `/Users/x` (verified in #8938). Other
207 // drives are untested and kept as is rather than silently remapped, and a
208 // POSIX path that happens to start with `/C:` is left alone.
209 const zedPath = /^[Cc]:[\\/]/.test(input.absolutePath) ? rootedPath.slice(3) : rootedPath;
210 const encodedZedPath = zedPath.split("/").map(encodeURIComponent).join("/");
211 return `${scheme}://ssh/${encodedHost}${encodedZedPath}`;
212 }
213 const encodedPath = rootedPath.split("/").map(encodeURIComponent).join("/");
214 // VS Code opens a bare vscode-remote path as a folder; a trailing position makes it open the file.
215 return `${scheme}://vscode-remote/ssh-remote+${encodedHost}${encodedPath}${input.isFile ? ":1:1" : ""}`;
216};
217
218export const RemoteOpenTargetKind = Schema.Literals(["tailscale", "mdns"]);
219export type RemoteOpenTargetKind = typeof RemoteOpenTargetKind.Type;
220
221export const RemoteOpenTarget = Schema.Struct({
222 kind: RemoteOpenTargetKind,
223 host: TrimmedNonEmptyString,
224});
225export type RemoteOpenTarget = typeof RemoteOpenTarget.Type;
226
227export class ExternalLauncherUnknownEditorError extends Schema.TaggedError<ExternalLauncherUnknownEditorError>()(
228 "ExternalLauncherUnknownEditorError",
229 {
230 editor: Schema.String,
231 },
232) {
233 override get message(): string {
234 return `Unknown editor: ${this.editor}`;
235 }
236}
237
238export class ExternalLauncherUnsupportedEditorError extends Schema.TaggedError<ExternalLauncherUnsupportedEditorError>()(
239 "ExternalLauncherUnsupportedEditorError",
240 {
241 editor: EditorId,
242 },
243) {
244 override get message(): string {
245 return `Unsupported editor: ${this.editor}`;
246 }
247}
248
249export class ExternalLauncherCommandNotFoundError extends Schema.TaggedError<ExternalLauncherCommandNotFoundError>()(
250 "ExternalLauncherCommandNotFoundError",
251 {
252 editor: EditorId,
253 command: Schema.String,
254 },
255) {
256 override get message(): string {
257 return `Editor command not found: ${this.command}`;
258 }
259}
260
261export class ExternalLauncherUnsupportedTargetError extends Schema.TaggedError<ExternalLauncherUnsupportedTargetError>()(
262 "ExternalLauncherUnsupportedTargetError",
263 {
264 editor: EditorId,
265 },
266) {
267 override get message(): string {
268 return `${this.editor} cannot open a path containing line breaks or double quotes`;
269 }
270}
271
272const ExternalLauncherSpawnFields = {
273 command: Schema.String,
274 args: Schema.Array(Schema.String),
275 cause: Schema.Defect(),
276};
277
278export class ExternalLauncherBrowserSpawnError extends Schema.TaggedError<ExternalLauncherBrowserSpawnError>()(
279 "ExternalLauncherBrowserSpawnError",
280 {
281 ...ExternalLauncherSpawnFields,
282 target: Schema.String,
283 },
284) {
285 override get message(): string {
286 return `Failed to launch browser target '${this.target}' with '${[this.command, ...this.args].join(" ")}'`;
287 }
288}
289
290export class ExternalLauncherEditorSpawnError extends Schema.TaggedError<ExternalLauncherEditorSpawnError>()(
291 "ExternalLauncherEditorSpawnError",
292 {
293 ...ExternalLauncherSpawnFields,
294 editor: EditorId,
295 target: Schema.String,
296 },
297) {
298 override get message(): string {
299 return `Failed to launch '${this.target}' in ${this.editor} with '${[this.command, ...this.args].join(" ")}'`;
300 }
301}
302
303export const ExternalLauncherError = Schema.Union([
304 ExternalLauncherUnknownEditorError,
305 ExternalLauncherUnsupportedEditorError,
306 ExternalLauncherCommandNotFoundError,
307 ExternalLauncherUnsupportedTargetError,
308 ExternalLauncherBrowserSpawnError,
309 ExternalLauncherEditorSpawnError,
310]);
311export type ExternalLauncherError = typeof ExternalLauncherError.Type;