mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-24 16:04:21 +00:00
When a ConPTY terminal got narrower (for example, a split opened and then closed), xterm dropped the end of the active PowerShell prompt. PowerShell then echoed input at absolute columns without printing the prompt again, so it came back garbled. Local terminals and embedded Run consoles now turn on xterm's cursor-line reflow on Windows builds whose ConPTY needs it. Remote and non-Windows sessions are unchanged. Closes #4155
159 lines
6.1 KiB
TypeScript
159 lines
6.1 KiB
TypeScript
/**
|
|
* Terminal widgets embedded in other views (the `embeddedTerminal` service):
|
|
* an xterm.js instance with the terminal's font, theme and links, whose input
|
|
* and size go to the owner instead of a terminal PTY (a Run console emulating
|
|
* a terminal feeds its process).
|
|
*/
|
|
import { writeClipboardText } from "@/platform/clipboard";
|
|
import { noteCopySource } from "@/platform/clipboardSource";
|
|
import { installTerminalDrop } from "./drop";
|
|
import { installDomRowCache } from "./domRowCache";
|
|
import { installTerminalPaste, pasteIntoTerminal } from "./paste";
|
|
import { ownTerminalInput } from "./imeInput";
|
|
import { Terminal, type IDisposable } from "@xterm/xterm";
|
|
import { FitAddon } from "@xterm/addon-fit";
|
|
import "@xterm/xterm/css/xterm.css";
|
|
import "./xtermInput.css";
|
|
import type { EmbeddedTerminal, EmbeddedTerminalOptions } from "@/workbench/contributions";
|
|
import { isImeKey } from "@/platform/ime";
|
|
import { terminalKeyAction } from "./keys";
|
|
import { useProject } from "@/platform/project";
|
|
import { usePlatform } from "@/platform/platformInfo";
|
|
import { workflowSettings } from "@/platform/workflowSettings";
|
|
import { fitScheduler, settledResize } from "./fit";
|
|
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";
|
|
import { installLinkHighlights, xtermLink, type LinkHighlightController } from "./linkHighlights";
|
|
|
|
const live = new Map<Terminal, LinkHighlightController>();
|
|
|
|
/** Follows a theme change (called with the terminal sessions' own update). */
|
|
export function applyThemeToEmbedded() {
|
|
const theme = readTerminalTheme();
|
|
for (const [t, links] of live) {
|
|
t.options.theme = theme;
|
|
links.refresh();
|
|
}
|
|
}
|
|
|
|
export function createEmbeddedTerminal(host: HTMLElement, opts: EmbeddedTerminalOptions): EmbeddedTerminal {
|
|
const settings = workflowSettings().terminal;
|
|
const font = fontFor(settings);
|
|
const term = new Terminal({
|
|
...windowsPtyOptions(),
|
|
linkHandler: LINK_HANDLER,
|
|
fontFamily: font.family,
|
|
fontSize: font.size,
|
|
lineHeight: font.lineHeight,
|
|
letterSpacing: font.letterSpacing,
|
|
fontWeight: font.weight,
|
|
fontWeightBold: font.boldWeight,
|
|
cursorBlink: settings.cursorBlink,
|
|
cursorWidth: 2,
|
|
cursorInactiveStyle: "outline",
|
|
allowProposedApi: true,
|
|
scrollback: 10_000,
|
|
macOptionIsMeta: settings.optionAsMeta,
|
|
macOptionClickForcesSelection: true,
|
|
drawBoldTextInBrightColors: false,
|
|
minimumContrastRatio: MIN_CONTRAST_RATIO,
|
|
smoothScrollDuration: 0,
|
|
convertEol: false,
|
|
theme: readTerminalTheme(),
|
|
});
|
|
term.attachCustomKeyEventHandler((e) => {
|
|
if (isImeKey(e)) return true;
|
|
const action = terminalKeyAction(e, term.hasSelection());
|
|
if (action.kind === "send" && e.key === "Enter") {
|
|
if (e.type === "keydown") term.input(action.data);
|
|
} else if (action.kind === "copy") {
|
|
if (e.type === "keydown") {
|
|
const text = term.getSelection();
|
|
noteCopySource(text, { kind: "terminal", title: "Terminal" });
|
|
void writeClipboardText(text).catch(() => undefined);
|
|
}
|
|
} else {
|
|
return true;
|
|
}
|
|
e.preventDefault();
|
|
return false;
|
|
});
|
|
const fit = new FitAddon();
|
|
term.loadAddon(fit);
|
|
live.set(term, installLinkHighlights(term, readLinkColor));
|
|
let lastSize = { cols: term.cols, rows: term.rows };
|
|
const processResize = settledResize((cols, rows) => {
|
|
if (cols === lastSize.cols && rows === lastSize.rows) return;
|
|
lastSize = { cols, rows };
|
|
opts.onResize(cols, rows);
|
|
});
|
|
const disposables: IDisposable[] = [
|
|
watchWindowsPty(term),
|
|
term.onData((d) => opts.onData(d)),
|
|
term.onBinary((d) => opts.onData(d)),
|
|
term.registerLinkProvider({
|
|
provideLinks(y, callback) {
|
|
const line = term.buffer.active.getLine(y - 1);
|
|
const found = line ? findTerminalLinks(line.translateToString(true)) : [];
|
|
if (!line || !found.length) return callback(undefined);
|
|
callback(
|
|
found.map((l) =>
|
|
xtermLink(term, line, y, l, (event) => {
|
|
event.preventDefault();
|
|
if (l.kind === "url") void openExternal(l.url);
|
|
else void openFileLink(l, opts.cwd ?? useProject.getState().project?.root ?? "");
|
|
}),
|
|
),
|
|
);
|
|
},
|
|
}),
|
|
];
|
|
const removePaste = installTerminalPaste(host, (text) => pasteIntoTerminal(term, text));
|
|
const removeDrop = installTerminalDrop(host, (text) => pasteIntoTerminal(term, text));
|
|
term.open(host);
|
|
installDomRowCache(term);
|
|
if (opts["aria-label"]) term.textarea?.setAttribute("aria-label", opts["aria-label"]);
|
|
// The same WKWebView input-method fixes as the terminal sessions.
|
|
if (term.textarea && usePlatform.getState().os === "macos") {
|
|
disposables.push({ dispose: ownTerminalInput({ host, textarea: term.textarea, send: opts.onData }) });
|
|
}
|
|
|
|
const fitter = fitScheduler(() => {
|
|
if (!host.isConnected || host.offsetWidth === 0 || host.offsetHeight === 0) return;
|
|
const dims = fit.proposeDimensions();
|
|
if (!dims || !Number.isFinite(dims.cols) || !Number.isFinite(dims.rows)) return;
|
|
if (dims.cols !== term.cols || dims.rows !== term.rows) term.resize(dims.cols, dims.rows);
|
|
processResize.resized(dims.cols, dims.rows);
|
|
});
|
|
const scheduleFit = () => {
|
|
processResize.cancel();
|
|
fitter.schedule();
|
|
};
|
|
const observer = new ResizeObserver(scheduleFit);
|
|
observer.observe(host);
|
|
scheduleFit();
|
|
remeasureWhenFontsLoad(term, font, () => live.has(term), scheduleFit);
|
|
// The owner learns the initial size even when it matches xterm's default.
|
|
queueMicrotask(() => opts.onResize(term.cols, term.rows));
|
|
|
|
return {
|
|
write: (data) => term.write(data),
|
|
clear: () => term.reset(),
|
|
focus: () => term.focus(),
|
|
fit: () => fitter.flush(),
|
|
dispose: () => {
|
|
fitter.cancel();
|
|
processResize.cancel();
|
|
observer.disconnect();
|
|
removePaste();
|
|
removeDrop();
|
|
for (const d of disposables) d.dispose();
|
|
live.get(term)?.dispose();
|
|
live.delete(term);
|
|
term.dispose();
|
|
},
|
|
};
|
|
}
|