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

editor.ts

225 lines8.4 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 /**
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 { id: "antigravity", label: "Antigravity", commands: ["agy"], launchStyle: "goto" },
63 { id: "idea", label: "IntelliJ IDEA", commands: ["idea"], launchStyle: "line-column" },
64 { id: "aqua", label: "Aqua", commands: ["aqua"], launchStyle: "line-column" },
65 { id: "clion", label: "CLion", commands: ["clion"], launchStyle: "line-column" },
66 { id: "datagrip", label: "DataGrip", commands: ["datagrip"], launchStyle: "line-column" },
67 { id: "dataspell", label: "DataSpell", commands: ["dataspell"], launchStyle: "line-column" },
68 { id: "goland", label: "GoLand", commands: ["goland"], launchStyle: "line-column" },
69 { id: "phpstorm", label: "PhpStorm", commands: ["phpstorm"], launchStyle: "line-column" },
70 { id: "pycharm", label: "PyCharm", commands: ["pycharm"], launchStyle: "line-column" },
71 { id: "rider", label: "Rider", commands: ["rider"], launchStyle: "line-column" },
72 { id: "rubymine", label: "RubyMine", commands: ["rubymine"], launchStyle: "line-column" },
73 { id: "rustrover", label: "RustRover", commands: ["rustrover"], launchStyle: "line-column" },
74 { id: "webstorm", label: "WebStorm", commands: ["webstorm"], launchStyle: "line-column" },
75 { id: "file-manager", label: "File Manager", commands: null, launchStyle: "direct-path" },
76] as const satisfies ReadonlyArray<EditorDefinition>;
77​
78export const EditorId = Schema.Literals(EDITORS.map((e) => e.id));
79export type EditorId = typeof EditorId.Type;
80​
81export const FileManagerRevealKind = Schema.Literals(["finder", "file-explorer", "files"]);
82export type FileManagerRevealKind = typeof FileManagerRevealKind.Type;
83​
84export const LaunchEditorInput = Schema.Struct({
85 cwd: TrimmedNonEmptyString,
86 editor: EditorId,
87 /** Reveal (select) `cwd` in the file manager instead of opening it. Only
88 honored by the "file-manager" editor; clients must check the server's
89 `shellRevealInFileManager` config flag before sending this. */
90 reveal: Schema.optional(Schema.Boolean),
91});
92export type LaunchEditorInput = typeof LaunchEditorInput.Type;
93​
94const remoteSchemeOf = (editor: EditorDefinition): string | undefined => editor.remoteScheme;
95​
96/** Editors that can open a remote workspace via an SSH deep link. */
97export const REMOTE_CAPABLE_EDITOR_IDS: ReadonlyArray<EditorId> = EDITORS.flatMap((editor) =>
98 remoteSchemeOf(editor) !== undefined ? [editor.id] : [],
99);
100​
101export const remoteSchemeForEditor = (id: EditorId): string | undefined => {
102 const editor = EDITORS.find((candidate) => candidate.id === id);
103 return editor === undefined ? undefined : remoteSchemeOf(editor);
104};
105​
106/**
107 * Builds a `<scheme>://vscode-remote/ssh-remote+<host><path>` deep link (Zed
108 * takes `zed://ssh/<host><path>`) that opens `absolutePath` on `host` in the
109 * local editor over SSH. Returns undefined for editors without remote
110 * deep-link support.
111 */
112export const buildRemoteOpenUrl = (input: {
113 readonly editor: EditorId;
114 readonly host: string;
115 readonly absolutePath: string;
116}): string | undefined => {
117 const scheme = remoteSchemeForEditor(input.editor);
118 if (scheme === undefined) {
119 return undefined;
120 }
121 // Windows server paths (`C:\...`) appear as `/C:/...` in vscode-remote URIs.
122 const posixPath = input.absolutePath.replaceAll("\\", "/");
123 const rootedPath = posixPath.startsWith("/") ? posixPath : `/${posixPath}`;
124 const encodedHost = encodeURIComponent(input.host);
125 if (input.editor === "zed") {
126 // Zed's remote server resolves a rooted path on the system drive, so a
127 // Windows `C:\Users\x` must become `/Users/x` (verified in #8938). Other
128 // drives are untested and kept as is rather than silently remapped, and a
129 // POSIX path that happens to start with `/C:` is left alone.
130 const zedPath = /^[Cc]:[\\/]/.test(input.absolutePath) ? rootedPath.slice(3) : rootedPath;
131 const encodedZedPath = zedPath.split("/").map(encodeURIComponent).join("/");
132 return `${scheme}://ssh/${encodedHost}${encodedZedPath}`;
133 }
134 const encodedPath = rootedPath.split("/").map(encodeURIComponent).join("/");
135 return `${scheme}://vscode-remote/ssh-remote+${encodedHost}${encodedPath}`;
136};
137​
138/**
139 * SSH hostnames an environment advertises for remote open links. Reachability
140 * is client-side; the server only advertises names that resolve to itself and
141 * gates them on a local sshd listen check. Ordered most-reachable first
142 * (tailnet MagicDNS name, then mDNS `<hostname>.local`).
143 */
144export const RemoteOpenTargetKind = Schema.Literals(["tailscale", "mdns"]);
145export type RemoteOpenTargetKind = typeof RemoteOpenTargetKind.Type;
146​
147export const RemoteOpenTarget = Schema.Struct({
148 kind: RemoteOpenTargetKind,
149 host: TrimmedNonEmptyString,
150});
151export type RemoteOpenTarget = typeof RemoteOpenTarget.Type;
152​
153export class ExternalLauncherUnknownEditorError extends Schema.TaggedError<ExternalLauncherUnknownEditorError>()(
154 "ExternalLauncherUnknownEditorError",
155 {
156 editor: Schema.String,
157 },
158) {
159 override get message(): string {
160 return `Unknown editor: ${this.editor}`;
161 }
163​
164export class ExternalLauncherUnsupportedEditorError extends Schema.TaggedError<ExternalLauncherUnsupportedEditorError>()(
165 "ExternalLauncherUnsupportedEditorError",
166 {
167 editor: EditorId,
168 },
169) {
170 override get message(): string {
171 return `Unsupported editor: ${this.editor}`;
172 }
174​
175export class ExternalLauncherCommandNotFoundError extends Schema.TaggedError<ExternalLauncherCommandNotFoundError>()(
176 "ExternalLauncherCommandNotFoundError",
177 {
178 editor: EditorId,
179 command: Schema.String,
180 },
181) {
182 override get message(): string {
183 return `Editor command not found: ${this.command}`;
184 }
186​
187const ExternalLauncherSpawnFields = {
188 command: Schema.String,
189 args: Schema.Array(Schema.String),
190 cause: Schema.Defect(),
191};
192​
193export class ExternalLauncherBrowserSpawnError extends Schema.TaggedError<ExternalLauncherBrowserSpawnError>()(
194 "ExternalLauncherBrowserSpawnError",
195 {
196 ...ExternalLauncherSpawnFields,
197 target: Schema.String,
198 },
199) {
200 override get message(): string {
201 return `Failed to launch browser target '${this.target}' with '${[this.command, ...this.args].join(" ")}'`;
202 }
204​
205export class ExternalLauncherEditorSpawnError extends Schema.TaggedError<ExternalLauncherEditorSpawnError>()(
206 "ExternalLauncherEditorSpawnError",
207 {
208 ...ExternalLauncherSpawnFields,
209 editor: EditorId,
210 target: Schema.String,
211 },
212) {
213 override get message(): string {
214 return `Failed to launch '${this.target}' in ${this.editor} with '${[this.command, ...this.args].join(" ")}'`;
215 }
217​
218export const ExternalLauncherError = Schema.Union([
219 ExternalLauncherUnknownEditorError,
220 ExternalLauncherUnsupportedEditorError,
221 ExternalLauncherCommandNotFoundError,
222 ExternalLauncherBrowserSpawnError,
223 ExternalLauncherEditorSpawnError,
224]);
225export type ExternalLauncherError = typeof ExternalLauncherError.Type;