Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

editor.ts

316 lines9.5 KB

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 // The IDE ships antigravity-ide; agy is the standalone CLI.
61 commands: ["antigravity-ide", "agy-ide"],
62 launchStyle: "goto",
63 },
64 {
65 id: "idea",
66 label: "IntelliJ IDEA",
67 commands: ["idea"],
68 launchStyle: "line-column",
69 jetbrainsProductCode: "IU",
70 },
71 {
72 id: "aqua",
73 label: "Aqua",
74 commands: ["aqua"],
75 launchStyle: "line-column",
76 jetbrainsProductCode: "QA",
77 },
78 {
79 id: "clion",
80 label: "CLion",
81 commands: ["clion"],
82 launchStyle: "line-column",
83 jetbrainsProductCode: "CL",
84 },
85 {
86 id: "datagrip",
87 label: "DataGrip",
88 commands: ["datagrip"],
89 launchStyle: "line-column",
90 jetbrainsProductCode: "DB",
91 },
92 {
93 id: "dataspell",
94 label: "DataSpell",
95 commands: ["dataspell"],
96 launchStyle: "line-column",
97 jetbrainsProductCode: "DS",
98 },
99 {
100 id: "goland",
101 label: "GoLand",
102 commands: ["goland"],
103 launchStyle: "line-column",
104 jetbrainsProductCode: "GO",
105 },
106 {
107 id: "phpstorm",
108 label: "PhpStorm",
109 commands: ["phpstorm"],
110 launchStyle: "line-column",
111 jetbrainsProductCode: "PS",
112 },
113 {
114 id: "pycharm",
115 label: "PyCharm",
116 commands: ["pycharm"],
117 launchStyle: "line-column",
118 jetbrainsProductCode: "PY",
119 },
120 {
121 id: "rider",
122 label: "Rider",
123 commands: ["rider"],
124 launchStyle: "line-column",
125 jetbrainsProductCode: "RD",
126 },
127 {
128 id: "rubymine",
129 label: "RubyMine",
130 commands: ["rubymine"],
131 launchStyle: "line-column",
132 jetbrainsProductCode: "RM",
133 },
134 {
135 id: "rustrover",
136 label: "RustRover",
137 commands: ["rustrover"],
138 launchStyle: "line-column",
139 jetbrainsProductCode: "RR",
140 },
141 {
142 id: "webstorm",
143 label: "WebStorm",
144 commands: ["webstorm"],
145 launchStyle: "line-column",
146 jetbrainsProductCode: "WS",
147 },
148 { id: "file-manager", label: "File Manager", commands: null, launchStyle: "direct-path" },
149] as const satisfies ReadonlyArray<EditorDefinition>;
150​
151export const EditorId = Schema.Literals(EDITORS.map((e) => e.id));
152export type EditorId = typeof EditorId.Type;
153​
154export const FileManagerRevealKind = Schema.Literals(["finder", "file-explorer", "files"]);
155export type FileManagerRevealKind = typeof FileManagerRevealKind.Type;
156​
157export const LaunchEditorInput = Schema.Struct({
158 cwd: TrimmedNonEmptyString,
159 editor: EditorId,
160 /** Only for file-manager; requires the server's shellRevealInFileManager capability. */
161 reveal: Schema.optional(Schema.Boolean),
162});
163export type LaunchEditorInput = typeof LaunchEditorInput.Type;
164​
165const remoteSchemeOf = (editor: EditorDefinition): string | undefined =>
166 editor.remoteScheme ?? (editor.jetbrainsProductCode === undefined ? undefined : "jetbrains");
167​
168export const REMOTE_CAPABLE_EDITOR_IDS: ReadonlyArray<EditorId> = EDITORS.flatMap((editor) =>
169 remoteSchemeOf(editor) !== undefined ? [editor.id] : [],
170);
171​
172export const remoteSchemeForEditor = (id: EditorId): string | undefined => {
173 const editor = EDITORS.find((candidate) => candidate.id === id);
174 return editor === undefined ? undefined : remoteSchemeOf(editor);
175};
176​
177export const buildRemoteOpenUrl = (input: {
178 readonly editor: EditorId;
179 readonly host: string;
180 readonly username?: string;
181 readonly absolutePath: string;
182 readonly isFile?: boolean;
183}): string | undefined => {
184 const scheme = remoteSchemeForEditor(input.editor);
185 if (scheme === undefined) {
186 return undefined;
187 }
188 const editor = EDITORS.find((candidate) => candidate.id === input.editor);
189 if (editor !== undefined && "jetbrainsProductCode" in editor) {
190 const params = new URLSearchParams({
191 h: input.host,
192 launchIde: "true",
193 ideHint: editor.jetbrainsProductCode,
194 projectHint: input.absolutePath.replaceAll("\\", "/"),
195 });
196 if (input.username !== undefined) {
197 params.set("u", input.username);
198 }
199 return `${scheme}://gateway/ssh/environment?${params.toString()}`;
200 }
201 // Windows server paths (`C:\...`) appear as `/C:/...` in vscode-remote URIs.
202 const posixPath = input.absolutePath.replaceAll("\\", "/");
203 const rootedPath = posixPath.startsWith("/") ? posixPath : `/${posixPath}`;
204 const encodedHost = encodeURIComponent(input.host);
205 if (input.editor === "zed") {
206 // Zed roots paths on the system drive, so omit the Windows drive prefix.
207 const zedPath = /^[Cc]:[\\/]/.test(input.absolutePath) ? rootedPath.slice(3) : rootedPath;
208 const encodedZedPath = zedPath.split("/").map(encodeURIComponent).join("/");
209 // Zed reads this segment as ssh URL userinfo, so the `@` stays literal.
210 const zedDestination =
211 input.username === undefined
212 ? encodedHost
213 : `${encodeURIComponent(input.username)}@${encodedHost}`;
214 return `${scheme}://ssh/${zedDestination}${encodedZedPath}`;
215 }
216 const encodedPath = rootedPath.split("/").map(encodeURIComponent).join("/");
217 const destination = input.username === undefined ? input.host : `${input.username}@${input.host}`;
218 // VS Code opens a bare vscode-remote path as a folder; a trailing position makes it open the file.
219 return `${scheme}://vscode-remote/ssh-remote+${encodeURIComponent(destination)}${encodedPath}${input.isFile ? ":1:1" : ""}`;
220};
221​
222export const RemoteOpenTargetKind = Schema.Literals(["tailscale", "mdns"]);
223export type RemoteOpenTargetKind = typeof RemoteOpenTargetKind.Type;
224​
225export const RemoteOpenTarget = Schema.Struct({
226 kind: RemoteOpenTargetKind,
227 host: TrimmedNonEmptyString,
228 username: Schema.optionalKey(TrimmedNonEmptyString),
229});
230export type RemoteOpenTarget = typeof RemoteOpenTarget.Type;
231​
232export class ExternalLauncherUnknownEditorError extends Schema.TaggedError<ExternalLauncherUnknownEditorError>()(
233 "ExternalLauncherUnknownEditorError",
234 {
235 editor: Schema.String,
236 },
237) {
238 override get message(): string {
239 return `Unknown editor: ${this.editor}`;
240 }
242​
243export class ExternalLauncherUnsupportedEditorError extends Schema.TaggedError<ExternalLauncherUnsupportedEditorError>()(
244 "ExternalLauncherUnsupportedEditorError",
245 {
246 editor: EditorId,
247 },
248) {
249 override get message(): string {
250 return `Unsupported editor: ${this.editor}`;
251 }
253​
254export class ExternalLauncherCommandNotFoundError extends Schema.TaggedError<ExternalLauncherCommandNotFoundError>()(
255 "ExternalLauncherCommandNotFoundError",
256 {
257 editor: EditorId,
258 command: Schema.String,
259 },
260) {
261 override get message(): string {
262 return `Editor command not found: ${this.command}`;
263 }
265​
266export class ExternalLauncherUnsupportedTargetError extends Schema.TaggedError<ExternalLauncherUnsupportedTargetError>()(
267 "ExternalLauncherUnsupportedTargetError",
268 {
269 editor: EditorId,
270 },
271) {
272 override get message(): string {
273 return `${this.editor} cannot open a path containing line breaks or double quotes`;
274 }
276​
277const ExternalLauncherSpawnFields = {
278 command: Schema.String,
279 args: Schema.Array(Schema.String),
280 cause: Schema.Defect(),
281};
282​
283export class ExternalLauncherBrowserSpawnError extends Schema.TaggedError<ExternalLauncherBrowserSpawnError>()(
284 "ExternalLauncherBrowserSpawnError",
285 {
286 ...ExternalLauncherSpawnFields,
287 target: Schema.String,
288 },
289) {
290 override get message(): string {
291 return `Failed to launch browser target '${this.target}' with '${[this.command, ...this.args].join(" ")}'`;
292 }
294​
295export class ExternalLauncherEditorSpawnError extends Schema.TaggedError<ExternalLauncherEditorSpawnError>()(
296 "ExternalLauncherEditorSpawnError",
297 {
298 ...ExternalLauncherSpawnFields,
299 editor: EditorId,
300 target: Schema.String,
301 },
302) {
303 override get message(): string {
304 return `Failed to launch '${this.target}' in ${this.editor} with '${[this.command, ...this.args].join(" ")}'`;
305 }
307​
308export const ExternalLauncherError = Schema.Union([
309 ExternalLauncherUnknownEditorError,
310 ExternalLauncherUnsupportedEditorError,
311 ExternalLauncherCommandNotFoundError,
312 ExternalLauncherUnsupportedTargetError,
313 ExternalLauncherBrowserSpawnError,
314 ExternalLauncherEditorSpawnError,
315]);
316export type ExternalLauncherError = typeof ExternalLauncherError.Type;