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