fix(terminal): retain active ConPTY text when resizing

Цей коміт міститься в:
l0ng-ai committed 2026-11-18 21:27:58 +08:00
1 джерело 762b509b66
коміт 3feb5be695
6 files changed
+58 -23

No files matched your search

+7 -6
Переглянути файл
@@ -78,16 +78,17 @@ afterEach(() => {
});
it.each([
["windows", 19045, { backend: "conpty", buildNumber: 19045 }],
["windows", 22631, { backend: "conpty", buildNumber: 22631 }],
["windows", null, { backend: "conpty", buildNumber: undefined }],
["macos", null, undefined],
["linux", null, undefined],
] as const)("configures embedded terminals for %s build %s", (os, windowsBuildNumber, expected) => {
["windows", 19045, { backend: "conpty", buildNumber: 19045 }, false],
["windows", 22631, { backend: "conpty", buildNumber: 22631 }, true],
["windows", null, { backend: "conpty", buildNumber: undefined }, false],
["macos", null, undefined, false],
["linux", null, undefined, false],
] as const)("configures embedded terminals for %s build %s", (os, windowsBuildNumber, expected, reflowCursorLine) => {
usePlatform.setState({ os, windowsBuildNumber });
const term = createEmbeddedTerminal(document.createElement("div"), { onData() {}, onResize() {} });
try {
expect(terminal.options.windowsPty).toEqual(expected);
expect(terminal.options.reflowCursorLine).toBe(reflowCursorLine);
} finally {
term.dispose();
}
+2 -2
Переглянути файл
@@ -21,7 +21,7 @@ import { useProject } from "@/platform/project";
import { usePlatform } from "@/platform/platformInfo";
import { workflowSettings } from "@/platform/workflowSettings";
import { fitScheduler, settledResize } from "./fit";
import { watchWindowsPty, windowsPty } from "./windowsPty";
import { watchWindowsPty, windowsPtyOptions } from "./windowsPty";
import { findTerminalLinks } from "./links";
import { LINK_HANDLER, fontFor, openExternal, openFileLink } from "./session";
import { MIN_CONTRAST_RATIO, readLinkColor, readTerminalTheme, remeasureWhenFontsLoad } from "./theme";
@@ -42,7 +42,7 @@ export function createEmbeddedTerminal(host: HTMLElement, opts: EmbeddedTerminal
const settings = workflowSettings().terminal;
const font = fontFor(settings);
const term = new Terminal({
windowsPty: windowsPty(),
...windowsPtyOptions(),
linkHandler: LINK_HANDLER,
fontFamily: font.family,
fontSize: font.size,
+35 -6
Переглянути файл
@@ -51,16 +51,17 @@ describe("Windows PTY metadata", () => {
afterEach(() => usePlatform.setState(initialPlatform));
it.each([
["windows", 19045, { backend: "conpty", buildNumber: 19045 }],
["windows", 22631, { backend: "conpty", buildNumber: 22631 }],
["windows", null, { backend: "conpty", buildNumber: undefined }],
["macos", null, {}],
["linux", null, {}],
] as const)("configures %s build %s", (os, windowsBuildNumber, expected) => {
["windows", 19045, { backend: "conpty", buildNumber: 19045 }, false],
["windows", 22631, { backend: "conpty", buildNumber: 22631 }, true],
["windows", null, { backend: "conpty", buildNumber: undefined }, false],
["macos", null, {}, false],
["linux", null, {}, false],
] as const)("configures %s build %s", (os, windowsBuildNumber, expected, reflowCursorLine) => {
usePlatform.setState({ os, windowsBuildNumber });
const session = new TerminalSession({ key: "pty-options", cwd: "/repo", onChange() {}, onFocus() {} });
try {
expect(session.term.options.windowsPty).toEqual(expected);
expect(session.term.options.reflowCursorLine).toBe(reflowCursorLine);
} finally {
session.dispose();
}
@@ -69,11 +70,39 @@ describe("Windows PTY metadata", () => {
it("updates a restored terminal when platform info arrives and unsubscribes on disposal", () => {
usePlatform.setState({ os: "windows", windowsBuildNumber: null });
const session = new TerminalSession({ key: "restored", cwd: "/repo", onChange() {}, onFocus() {} });
expect(session.term.options.reflowCursorLine).toBe(false);
usePlatform.setState({ windowsBuildNumber: 22631 });
expect(session.term.options.windowsPty).toEqual({ backend: "conpty", buildNumber: 22631 });
expect(session.term.options.reflowCursorLine).toBe(true);
session.dispose();
usePlatform.setState({ windowsBuildNumber: 19045 });
expect(session.term.options.windowsPty).toEqual({ backend: "conpty", buildNumber: 22631 });
expect(session.term.options.reflowCursorLine).toBe(true);
});
it.each([
["idle prompt", ""],
["wrapped pending input", "Write-Output 'pending input spanning more than one terminal row"],
])("preserves the %s when a ConPTY session narrows and widens", async (_, input) => {
usePlatform.setState({ os: "windows", windowsBuildNumber: 26200 });
const session = new TerminalSession({ key: "pty-reflow", cwd: "/repo", onChange() {}, onFocus() {} });
try {
const prompt = "PS C:\\workspace\\a-long-project-directory\\src> ";
const content = prompt + input;
const { term } = session;
const terminalText = () => {
const buffer = term.buffer.active;
return Array.from({ length: buffer.length }, (_, i) => buffer.getLine(i)?.translateToString(true)).join("");
};
term.resize(80, 8);
await new Promise<void>((resolve) => term.write(content, resolve));
term.resize(30, 8);
expect(terminalText()).toBe(content);
term.resize(80, 8);
expect(terminalText()).toBe(content);
} finally {
session.dispose();
}
});
});
+2 -2
Переглянути файл
@@ -16,7 +16,7 @@ import { tx } from "@/platform/i18n";
import { editorService, notifications, registerFontZoomSurface } from "@/workbench/contributions";
import { OutputActivity } from "./activity";
import { fitScheduler, settledResize } from "./fit";
import { watchWindowsPty, windowsPty } from "./windowsPty";
import { watchWindowsPty, windowsPtyOptions } from "./windowsPty";
import { installTerminalDrop } from "./drop";
import { installDomRowCache } from "./domRowCache";
import { installSelectionHighlights, type SelectionHighlightController } from "./highlight";
@@ -311,7 +311,7 @@ export class TerminalSession {
const settings = workflowSettings().terminal;
const font = fontFor(settings);
this.term = new Terminal({
windowsPty: windowsPty(),
...windowsPtyOptions(),
fontFamily: font.family,
fontSize: font.size,
lineHeight: font.lineHeight,
+2 -2
Переглянути файл
@@ -1,5 +1,5 @@
import { afterEach, expect, it } from "vitest";
import { windowsPty } from "./windowsPty";
import { windowsPtyOptions } from "./windowsPty";
import { usePlatform } from "@/platform/platformInfo";
import { useRemote } from "@/platform/remote";
@@ -15,5 +15,5 @@ it.each(["linux", "windows"])("does not apply the desktop build to a remote %s P
useRemote.setState({
session: { os, host: "test", root: "/repo", target: { kind: "ssh", config: "test" }, commands: [] },
});
expect(windowsPty()).toBeUndefined();
expect(windowsPtyOptions()).toEqual({ reflowCursorLine: false });
});
+10 -5
Переглянути файл
@@ -2,19 +2,24 @@ import type { ITerminalOptions, Terminal } from "@xterm/xterm";
import { usePlatform } from "@/platform/platformInfo";
import { useRemote } from "@/platform/remote";
/** Report the local ConPTY backend even when the OS build is unavailable. */
export function windowsPty(): ITerminalOptions["windowsPty"] {
export function windowsPtyOptions(): Pick<ITerminalOptions, "windowsPty" | "reflowCursorLine"> {
const { os, windowsBuildNumber } = usePlatform.getState();
// Remote PTYs must not inherit the desktop's Windows build or backend.
if (useRemote.getState().session) return undefined;
return os === "windows" ? { backend: "conpty", buildNumber: windowsBuildNumber ?? undefined } : undefined;
if (os !== "windows" || useRemote.getState().session) return { reflowCursorLine: false };
return {
windowsPty: { backend: "conpty", buildNumber: windowsBuildNumber ?? undefined },
// Modern ConPTY can leave the prompt untouched on resize and echo input with absolute cursor moves.
reflowCursorLine: windowsBuildNumber !== null && windowsBuildNumber >= 21376,
};
}
/** Restored terminals can be constructed before appInfo answers. */
export function watchWindowsPty(term: Terminal) {
const unsubscribe = usePlatform.subscribe((next, previous) => {
if (next.os !== previous.os || next.windowsBuildNumber !== previous.windowsBuildNumber) {
term.options.windowsPty = windowsPty() ?? {};
const { windowsPty, reflowCursorLine } = windowsPtyOptions();
term.options.windowsPty = windowsPty ?? {};
term.options.reflowCursorLine = reflowCursorLine;
}
});
return { dispose: unsubscribe };