Skip to content

Commit 9bd9783

Browse files
committed
fix(web): file editor shortcuts open over SSH on remote connections
1 parent 31d6be1 commit 9bd9783

3 files changed

Lines changed: 42 additions & 5 deletions

File tree

‎apps/web/src/components/files/FileBrowserPanel.tsx‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,12 @@ export default function FileBrowserPanel({
208208
const position = pointerIsFresh
209209
? { x: pointer.x, y: pointer.y }
210210
: { x: anchorRect.left, y: anchorRect.bottom };
211-
const fileTarget = { environmentId, filePath: relativePath, workspaceRoot: cwd };
211+
const fileTarget = {
212+
environmentId,
213+
filePath: relativePath,
214+
workspaceRoot: cwd,
215+
isDirectory: item.path.endsWith("/"),
216+
};
212217
const fileMenuItems = fileContextMenu.buildItems(fileTarget);
213218
void settlePromise(async () => {
214219
const clicked = await api.contextMenu.show(
@@ -526,6 +531,7 @@ export default function FileBrowserPanel({
526531
environmentId,
527532
filePath: itemPath.replace(/\/$/, ""),
528533
workspaceRoot: cwd,
534+
isDirectory: itemPath.endsWith("/"),
529535
});
530536
if (!handled) return;
531537
event.preventDefault();

‎apps/web/src/fileContextMenu.ts‎

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
buildRemoteOpenUrl,
23
EDITORS,
34
type ContextMenuItem,
45
type EditorId,
@@ -12,6 +13,7 @@ import {
1213
} from "~/components/preview/fileExplorerLabel";
1314
import { usePreferredEditor } from "./editorPreferences";
1415
import { readLocalApi } from "./localApi";
16+
import { openRemoteEditorUrl, useRemoteCapableEditors, useRemoteOpenState } from "./remoteOpen";
1517
import { serverEnvironment } from "./state/server";
1618
import { shellEnvironment } from "./state/shell";
1719
import { useAtomCommand } from "./state/use-atom-command";
@@ -33,6 +35,7 @@ export interface FileContextMenuTarget {
3335
readonly filePath: string;
3436
readonly workspaceRoot: string | undefined;
3537
readonly repositoryRoot?: string | undefined;
38+
readonly isDirectory?: boolean;
3639
}
3740

3841
export function resolveFileContextMenuAbsolutePath(target: FileContextMenuTarget): string | null {
@@ -109,20 +112,30 @@ export function useFileContextMenu(environmentId: EnvironmentId | null) {
109112
const openInEditor = useAtomCommand(shellEnvironment.openInEditor, { reportFailure: false });
110113
const serverConfig = useAtomValue(serverEnvironment.configValueAtom(environmentId));
111114

112-
const availableEditors = serverConfig?.availableEditors ?? [];
115+
const remote = useRemoteOpenState(environmentId);
116+
const remoteCapableEditors = useRemoteCapableEditors();
117+
const isLocal = remote.mode === "local-exec";
118+
// Away from the server's machine, editors open over SSH on this machine, which
119+
// only works for editors with a remote URL scheme and can't reveal files.
120+
const availableEditors = isLocal
121+
? (serverConfig?.availableEditors ?? [])
122+
: remote.mode === "remote-links"
123+
? remoteCapableEditors
124+
: [];
113125
const [preferredEditor] = usePreferredEditor(availableEditors);
114126
const capabilities: FileContextMenuCapabilities = {
115127
// The reveal wording comes from the server because on WSL the reveal can
116128
// run through Windows File Explorer even though the host reports Linux.
117129
revealLabel:
130+
isLocal &&
118131
environmentId !== null &&
119132
serverConfig?.shellRevealInFileManager === true &&
120133
serverConfig.availableEditors.includes("file-manager")
121134
? serverConfig.shellRevealInFileManagerKind === undefined
122135
? revealInFileExplorerLabelForOs(serverConfig.environment.platform.os)
123136
: revealInFileExplorerLabelForKind(serverConfig.shellRevealInFileManagerKind)
124137
: undefined,
125-
canOpenDefault: availableEditors.includes("file-manager"),
138+
canOpenDefault: isLocal && availableEditors.includes("file-manager"),
126139
editorIds: availableEditors,
127140
};
128141

@@ -140,6 +153,22 @@ export function useFileContextMenu(environmentId: EnvironmentId | null) {
140153
: (action.slice("editor:".length) as EditorId);
141154
if (action !== "open" && !reveal && !capabilities.editorIds.includes(editor)) return;
142155

156+
if (remote.mode === "remote-links") {
157+
const url = buildRemoteOpenUrl({
158+
editor,
159+
...remote.host,
160+
absolutePath,
161+
isFile: target.isDirectory !== true,
162+
});
163+
if (url !== undefined && (await openRemoteEditorUrl(url))) return;
164+
toastManager.add({
165+
type: "error",
166+
title: `Could not open in ${EDITOR_LABEL_BY_ID.get(editor) ?? editor}`,
167+
description: absolutePath,
168+
});
169+
return;
170+
}
171+
143172
const result = await openInEditor({
144173
environmentId,
145174
input: { cwd: absolutePath, editor, ...(reveal ? { reveal: true } : {}) },

‎docs/user/keybindings.md‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,10 @@ find only searches the files loaded so far.
6565
In a diff, a thread's changed files, or a file link in chat, `mod`-click a file
6666
to open it in your preferred editor. `mod+Alt`-click reveals it in Finder or
6767
your file manager. In the Files panel, `mod`-click adds to the selection, so use
68-
`mod+Shift`-click for the editor; `mod+Alt`-click still reveals. These clicks
69-
open the file on the environment's machine and are not configurable.
68+
`mod+Shift`-click for the editor; `mod+Alt`-click still reveals. When you're
69+
connected to another machine, diffs, changed files, and the Files panel open the
70+
editor on your machine over SSH, like the Open In button, and reveal isn't
71+
available. These clicks are not configurable.
7072

7173
## iPad
7274

0 commit comments

Comments
 (0)