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 /**
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 FileManagerRevealKind = Schema.Literals(["finder", "file-explorer", "files"]);
73export type FileManagerRevealKind = typeof FileManagerRevealKind.Type;
74
75export const LaunchEditorInput = Schema.Struct({
76 cwd: TrimmedNonEmptyString,
77 editor: EditorId,
78 /** Reveal (select) `cwd` in the file manager instead of opening it. Only
79 honored by the "file-manager" editor; clients must check the server's
80 `shellRevealInFileManager` config flag before sending this. */
81 reveal: Schema.optional(Schema.Boolean),
82});
83export type LaunchEditorInput = typeof LaunchEditorInput.Type;
84
85const remoteSchemeOf = (editor: EditorDefinition): string | undefined => editor.remoteScheme;
86
87/** Editors that can open a remote workspace via `vscode-remote` deep links. */
88export const REMOTE_CAPABLE_EDITOR_IDS: ReadonlyArray<EditorId> = EDITORS.flatMap((editor) =>
89 remoteSchemeOf(editor) !== undefined ? [editor.id] : [],
90);
91
92export const remoteSchemeForEditor = (id: EditorId): string | undefined => {
93 const editor = EDITORS.find((candidate) => candidate.id === id);
94 return editor === undefined ? undefined : remoteSchemeOf(editor);
95};
96
97/**
98 * Builds a `<scheme>://vscode-remote/ssh-remote+<host><path>` deep link that
99 * opens `absolutePath` on `host` in the local editor over SSH. Returns
100 * undefined for editors without remote deep-link support.
101 */
102export const buildRemoteOpenUrl = (input: {
103 readonly editor: EditorId;
104 readonly host: string;
105 readonly absolutePath: string;
106}): string | undefined => {
107 const scheme = remoteSchemeForEditor(input.editor);
108 if (scheme === undefined) {
109 return undefined;
110 }
111 // Windows server paths (`C:\...`) appear as `/C:/...` in vscode-remote URIs.
112 const posixPath = input.absolutePath.replaceAll("\\", "/");
113 const rootedPath = posixPath.startsWith("/") ? posixPath : `/${posixPath}`;
114 const encodedPath = rootedPath.split("/").map(encodeURIComponent).join("/");
115 return `${scheme}://vscode-remote/ssh-remote+${encodeURIComponent(input.host)}${encodedPath}`;
116};
117
118/**
119 * SSH hostnames an environment advertises for remote open links. Reachability
120 * is client-side; the server only advertises names that resolve to itself and
121 * gates them on a local sshd listen check. Ordered most-reachable first
122 * (tailnet MagicDNS name, then mDNS `<hostname>.local`).
123 */
124export const RemoteOpenTargetKind = Schema.Literals(["tailscale", "mdns"]);
125export type RemoteOpenTargetKind = typeof RemoteOpenTargetKind.Type;
126
127export const RemoteOpenTarget = Schema.Struct({
128 kind: RemoteOpenTargetKind,
129 host: TrimmedNonEmptyString,
130});
131export type RemoteOpenTarget = typeof RemoteOpenTarget.Type;
132
133export class ExternalLauncherUnknownEditorError extends Schema.TaggedErrorClass<ExternalLauncherUnknownEditorError>()(
134 "ExternalLauncherUnknownEditorError",
135 {
136 editor: Schema.String,
137 },
138) {
139 override get message(): string {
140 return `Unknown editor: ${this.editor}`;
141 }
142}
143
144export class ExternalLauncherUnsupportedEditorError extends Schema.TaggedErrorClass<ExternalLauncherUnsupportedEditorError>()(
145 "ExternalLauncherUnsupportedEditorError",
146 {
147 editor: EditorId,
148 },
149) {
150 override get message(): string {
151 return `Unsupported editor: ${this.editor}`;
152 }
153}
154
155export class ExternalLauncherCommandNotFoundError extends Schema.TaggedErrorClass<ExternalLauncherCommandNotFoundError>()(
156 "ExternalLauncherCommandNotFoundError",
157 {
158 editor: EditorId,
159 command: Schema.String,
160 },
161) {
162 override get message(): string {
163 return `Editor command not found: ${this.command}`;
164 }
165}
166
167const ExternalLauncherSpawnFields = {
168 command: Schema.String,
169 args: Schema.Array(Schema.String),
170 cause: Schema.Defect(),
171};
172
173export class ExternalLauncherBrowserSpawnError extends Schema.TaggedErrorClass<ExternalLauncherBrowserSpawnError>()(
174 "ExternalLauncherBrowserSpawnError",
175 {
176 ...ExternalLauncherSpawnFields,
177 target: Schema.String,
178 },
179) {
180 override get message(): string {
181 return `Failed to launch browser target '${this.target}' with '${[this.command, ...this.args].join(" ")}'`;
182 }
183}
184
185export class ExternalLauncherEditorSpawnError extends Schema.TaggedErrorClass<ExternalLauncherEditorSpawnError>()(
186 "ExternalLauncherEditorSpawnError",
187 {
188 ...ExternalLauncherSpawnFields,
189 editor: EditorId,
190 target: Schema.String,
191 },
192) {
193 override get message(): string {
194 return `Failed to launch '${this.target}' in ${this.editor} with '${[this.command, ...this.args].join(" ")}'`;
195 }
196}
197
198export const ExternalLauncherError = Schema.Union([
199 ExternalLauncherUnknownEditorError,
200 ExternalLauncherUnsupportedEditorError,
201 ExternalLauncherCommandNotFoundError,
202 ExternalLauncherBrowserSpawnError,
203 ExternalLauncherEditorSpawnError,
204]);
205export type ExternalLauncherError = typeof ExternalLauncherError.Type;
206
207export const isExternalLauncherError = Schema.is(ExternalLauncherError);