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

editor.ts

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