Файли
openidea/src/workbench/projectWindow.ts
T
l0ng-ai c8dc613485 fix(projects): offer New Tab when opening a remote project or from a remote window (#7017)
The Open Project prompt gated Attach and New Tab on one flag that required a local project in a local window, so opening an SSH/WSL project, or any project from a remote window, offered only New Window and This Window, and "Open project in: tab" fell back to a new window. Attach still needs a local project in a local window, but a project tab is a window of its own joined to this window's group, so it now applies whenever this window has a project: a remote project opens in a new window that joins the group.

Fixes #6923
2026-11-30 18:36:46 +08:00

112 рядки
5.1 KiB
TypeScript
Неформатований Анотація Історія

Цей файл містить неоднозначні символи Юнікоду
Цей файл містить символи Юнікоду, які можна сплутати з іншими символами. Якщо ви вважаєте, що це зроблено навмисно, можете сміливо ігнорувати це попередження. Щоб показати їх, скористайтеся кнопкою Escape.
/** Where a project opens when this window already has one: System Settings › Project › "Open project in". */
import { t, tx } from "@/platform/i18n";
import { call, commands, type RemoteTarget } from "@/platform/ipc";
import { confirmLeaveProject, useProject } from "@/platform/project";
import { connectRemote, isRemoteWindow } from "@/platform/remote";
import { isMacHost } from "@/platform/keymap";
import { ask } from "./messages";
import { maybeEditorService } from "./contributions";
/** The window a modifier key picks for opening a recent project, whatever "Open project in" says. */
export type ProjectWindowOverride = "new" | "same";
/** The modifier keys of a click or a key press. */
export type ModifierKeys = Pick<KeyboardEvent | MouseEvent, "altKey" | "ctrlKey" | "metaKey" | "shiftKey">;
/**
* The window a recent project opens in when it is picked with a modifier key, as in IntelliJ IDEA:
* Shift or Ctrl (⌘ on macOS, where Ctrl+click is a right-click) is a new window, Alt this one.
* `null`: no modifier, so "Open project in" decides.
*/
export function projectWindowOverride(e: ModifierKeys): ProjectWindowOverride | null {
if (e.shiftKey || e.ctrlKey || (isMacHost() && e.metaKey)) return "new";
return e.altKey ? "same" : null;
}
/** "Open project in": `new` | `same` | `ask`. */
export function openPolicy(): "ask" | "new" | "same" | "tab" {
const v = useProject.getState().settings?.general?.openProjectIn;
return v === "new" || v === "same" || v === "tab" ? v : "ask";
}
/**
* The window to open project `name` in: this one when it has no project (or LightEdit session), else
* per "Open project in", asking when set to (and remembering "Don't ask again").
* `null`: cancelled.
*/
export async function chooseProjectWindow(
name: string,
allowAttach = true,
): Promise<"new" | "same" | "attach" | "tab" | null> {
const { project, lightEdit } = useProject.getState();
if (!project && !lightEdit) return "same";
// Attach adds a local folder to this local window's workspace; a project tab is a window of its
// own joined to this one's group, so it takes remote projects and remote windows too.
const canTab = !!project;
const canAttach = allowAttach && canTab && !isRemoteWindow();
const mode = openPolicy();
// A project tab needs this window's project beside it; without one, a new window is the closest.
if (mode === "tab") return canTab ? "tab" : "new";
if (mode !== "ask") return mode;
const r = await ask({
title: t("app.window.askTitle"),
width: canAttach ? 640 : undefined,
body: canAttach
? tx`Projects can be opened in a new window, replace the current project, be attached to the already opened projects, or open as a new tab of this window.\nHow would you like to open "${name}"?`
: canTab
? tx`Projects can be opened in a new window, replace the current project, or open as a new tab of this window.\nHow would you like to open "${name}"?`
: t("app.window.askBody", { name }),
buttons: [
{ id: "cancel", label: t("common.cancel") },
...(canAttach ? [{ id: "attach", label: t("app.window.askAttach") }] : []),
...(canTab ? [{ id: "tab", label: tx`New Tab` }] : []),
{ id: "new", label: t("app.window.askNew") },
{ id: "same", label: t("app.window.askThis"), primary: true },
],
checkbox: t("app.window.askRemember"),
});
// "Attach" is one-off, as in IDEA: "Open project in" has no value for it, so Don't ask again ignores it.
if (r.button !== "same" && r.button !== "new" && r.button !== "attach" && r.button !== "tab") return null;
if (r.checked && r.button !== "attach") {
try {
useProject.setState({
settings: await call(commands.settingsSet("global", "general.openProjectIn", JSON.stringify(r.button))),
});
} catch {
/* the choice still applies to this open */
}
}
return r.button;
}
/**
* Opens folder `path` of `target` (an SSH host or a WSL distribution): focuses the
* window that has it, else opens it in a new window, a project tab of this window or
* (`here`) this one per "Open project in", or where `override` says. `false`: cancelled.
*/
export async function openRemoteProject(
target: RemoteTarget,
path: string,
here: () => Promise<unknown> = () => connectRemote(target, path),
override?: ProjectWindowOverride | null,
): Promise<boolean> {
if ((await call(commands.windowOpenRemote(target, path, false))).kind === "focused") return true;
const mode = override ?? (await chooseProjectWindow(path.replace(/\/+$/, "").split("/").pop() || path, false));
if (!mode) return false;
if (mode === "new" || mode === "tab") {
const outcome = await call(commands.windowOpenRemote(target, path, true));
// As a local project tab: the window holding the project, new or focused, joins this one's group.
if (mode === "tab" && outcome.kind !== "current") await call(commands.windowGroupJoin(outcome.label));
return true;
}
// The remote host's project doesn't take this window's untitled buffers.
if (!(await confirmLeaveProject(false))) return false;
try {
await here();
return true;
} catch (e) {
maybeEditorService()?.cancelClose?.();
throw e;
}
}