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

editor.ts

216 lines7.8 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 launchStyle: "goto",
28 remoteScheme: "cursor",
29 },
30 { id: "trae", label: "Trae", commands: ["trae"], launchStyle: "goto" },
31 { id: "kiro", label: "Kiro", commands: ["kiro"], baseArgs: ["ide"], launchStyle: "goto" },
32 {
33 id: "vscode",
34 label: "VS Code",
35 commands: ["code"],
36 launchStyle: "goto",
37 remoteScheme: "vscode",
38 },
39 {
40 id: "vscode-insiders",
41 label: "VS Code Insiders",
42 commands: ["code-insiders"],
43 launchStyle: "goto",
44 remoteScheme: "vscode-insiders",
45 },
46 {
47 id: "vscodium",
48 label: "VSCodium",
49 commands: ["codium"],
50 launchStyle: "goto",
51 remoteScheme: "vscodium",
52 },
53 {
54 id: "zed",
55 label: "Zed",
56 commands: ["zed", "zeditor"],
57 launchStyle: "direct-path",
58 remoteScheme: "zed",
59 },
60 { id: "antigravity", label: "Antigravity", commands: ["agy"], launchStyle: "goto" },
61 { id: "idea", label: "IntelliJ IDEA", commands: ["idea"], launchStyle: "line-column" },
62 { id: "aqua", label: "Aqua", commands: ["aqua"], launchStyle: "line-column" },
63 { id: "clion", label: "CLion", commands: ["clion"], launchStyle: "line-column" },
64 { id: "datagrip", label: "DataGrip", commands: ["datagrip"], launchStyle: "line-column" },
65 { id: "dataspell", label: "DataSpell", commands: ["dataspell"], launchStyle: "line-column" },
66 { id: "goland", label: "GoLand", commands: ["goland"], launchStyle: "line-column" },
67 { id: "phpstorm", label: "PhpStorm", commands: ["phpstorm"], launchStyle: "line-column" },
68 { id: "pycharm", label: "PyCharm", commands: ["pycharm"], launchStyle: "line-column" },
69 { id: "rider", label: "Rider", commands: ["rider"], launchStyle: "line-column" },
70 { id: "rubymine", label: "RubyMine", commands: ["rubymine"], launchStyle: "line-column" },
71 { id: "rustrover", label: "RustRover", commands: ["rustrover"], launchStyle: "line-column" },
72 { id: "webstorm", label: "WebStorm", commands: ["webstorm"], launchStyle: "line-column" },
73 { id: "file-manager", label: "File Manager", commands: null, launchStyle: "direct-path" },
74] as const satisfies ReadonlyArray<EditorDefinition>;
75​
76export const EditorId = Schema.Literals(EDITORS.map((e) => e.id));
77export type EditorId = typeof EditorId.Type;
78​
79export const FileManagerRevealKind = Schema.Literals(["finder", "file-explorer", "files"]);
80export type FileManagerRevealKind = typeof FileManagerRevealKind.Type;
81​
82export const LaunchEditorInput = Schema.Struct({
83 cwd: TrimmedNonEmptyString,
84 editor: EditorId,
85 /** Reveal (select) `cwd` in the file manager instead of opening it. Only
86 honored by the "file-manager" editor; clients must check the server's
87 `shellRevealInFileManager` config flag before sending this. */
88 reveal: Schema.optional(Schema.Boolean),
89});
90export type LaunchEditorInput = typeof LaunchEditorInput.Type;
91​
92const remoteSchemeOf = (editor: EditorDefinition): string | undefined => editor.remoteScheme;
93​
94/** Editors that can open a remote workspace via `vscode-remote` deep links. */
95export const REMOTE_CAPABLE_EDITOR_IDS: ReadonlyArray<EditorId> = EDITORS.flatMap((editor) =>
96 remoteSchemeOf(editor) !== undefined ? [editor.id] : [],
97);
98​
99export const remoteSchemeForEditor = (id: EditorId): string | undefined => {
100 const editor = EDITORS.find((candidate) => candidate.id === id);
101 return editor === undefined ? undefined : remoteSchemeOf(editor);
102};
103​
104/**
105 * Builds a `<scheme>://vscode-remote/ssh-remote+<host><path>` deep link (Zed
106 * takes `zed://ssh/<host><path>`) that opens `absolutePath` on `host` in the
107 * local editor over SSH. Returns undefined for editors without remote
108 * deep-link support.
109 */
110export const buildRemoteOpenUrl = (input: {
111 readonly editor: EditorId;
112 readonly host: string;
113 readonly absolutePath: string;
114}): string | undefined => {
115 const scheme = remoteSchemeForEditor(input.editor);
116 if (scheme === undefined) {
117 return undefined;
118 }
119 // Windows server paths (`C:\...`) appear as `/C:/...` in vscode-remote URIs.
120 const posixPath = input.absolutePath.replaceAll("\\", "/");
121 const rootedPath = posixPath.startsWith("/") ? posixPath : `/${posixPath}`;
122 const encodedPath = rootedPath.split("/").map(encodeURIComponent).join("/");
123 const encodedHost = encodeURIComponent(input.host);
124 return input.editor === "zed"
125 ? `${scheme}://ssh/${encodedHost}${encodedPath}`
126 : `${scheme}://vscode-remote/ssh-remote+${encodedHost}${encodedPath}`;
127};
128​
129/**
130 * SSH hostnames an environment advertises for remote open links. Reachability
131 * is client-side; the server only advertises names that resolve to itself and
132 * gates them on a local sshd listen check. Ordered most-reachable first
133 * (tailnet MagicDNS name, then mDNS `<hostname>.local`).
134 */
135export const RemoteOpenTargetKind = Schema.Literals(["tailscale", "mdns"]);
136export type RemoteOpenTargetKind = typeof RemoteOpenTargetKind.Type;
137​
138export const RemoteOpenTarget = Schema.Struct({
139 kind: RemoteOpenTargetKind,
140 host: TrimmedNonEmptyString,
141});
142export type RemoteOpenTarget = typeof RemoteOpenTarget.Type;
143​
144export class ExternalLauncherUnknownEditorError extends Schema.TaggedError<ExternalLauncherUnknownEditorError>()(
145 "ExternalLauncherUnknownEditorError",
146 {
147 editor: Schema.String,
148 },
149) {
150 override get message(): string {
151 return `Unknown editor: ${this.editor}`;
152 }
154​
155export class ExternalLauncherUnsupportedEditorError extends Schema.TaggedError<ExternalLauncherUnsupportedEditorError>()(
156 "ExternalLauncherUnsupportedEditorError",
157 {
158 editor: EditorId,
159 },
160) {
161 override get message(): string {
162 return `Unsupported editor: ${this.editor}`;
163 }
165​
166export class ExternalLauncherCommandNotFoundError extends Schema.TaggedError<ExternalLauncherCommandNotFoundError>()(
167 "ExternalLauncherCommandNotFoundError",
168 {
169 editor: EditorId,
170 command: Schema.String,
171 },
172) {
173 override get message(): string {
174 return `Editor command not found: ${this.command}`;
175 }
177​
178const ExternalLauncherSpawnFields = {
179 command: Schema.String,
180 args: Schema.Array(Schema.String),
181 cause: Schema.Defect(),
182};
183​
184export class ExternalLauncherBrowserSpawnError extends Schema.TaggedError<ExternalLauncherBrowserSpawnError>()(
185 "ExternalLauncherBrowserSpawnError",
186 {
187 ...ExternalLauncherSpawnFields,
188 target: Schema.String,
189 },
190) {
191 override get message(): string {
192 return `Failed to launch browser target '${this.target}' with '${[this.command, ...this.args].join(" ")}'`;
193 }
195​
196export class ExternalLauncherEditorSpawnError extends Schema.TaggedError<ExternalLauncherEditorSpawnError>()(
197 "ExternalLauncherEditorSpawnError",
198 {
199 ...ExternalLauncherSpawnFields,
200 editor: EditorId,
201 target: Schema.String,
202 },
203) {
204 override get message(): string {
205 return `Failed to launch '${this.target}' in ${this.editor} with '${[this.command, ...this.args].join(" ")}'`;
206 }
208​
209export const ExternalLauncherError = Schema.Union([
210 ExternalLauncherUnknownEditorError,
211 ExternalLauncherUnsupportedEditorError,
212 ExternalLauncherCommandNotFoundError,
213 ExternalLauncherBrowserSpawnError,
214 ExternalLauncherEditorSpawnError,
215]);
216export type ExternalLauncherError = typeof ExternalLauncherError.Type;