mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-24 08:03:19 +00:00
Fix/terminal ime caret (#5458)
Closes #4112 (its remaining half: #4347 stopped the terminal sliding sideways while composing, the candidate window still opened at the far right of pi's input box). xterm anchors the IME candidate window on its hidden helper textarea, and keeps that textarea on the terminal cursor — on every render, on `compositionstart` and again on each `compositionupdate`. A program that paints its own caret leaves the terminal cursor elsewhere: pi asks for a column past the right edge of its input box (`ESC[21;411H` in a 206-column terminal, which xterm clamps) while it Co-authored-by: H-TWINKLE <34811911+H-TWINKLE@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,291 @@
|
||||
// @vitest-environment jsdom
|
||||
import { describe, expect, it } from "vitest";
|
||||
import type { Terminal } from "@xterm/xterm";
|
||||
import { installImeAnchor } from "./imeAnchor";
|
||||
|
||||
/** jsdom has no layout: the pieces the anchor reads report their own boxes. */
|
||||
function rect(left: number, top: number, width = 8, height = 18): DOMRect {
|
||||
return {
|
||||
left,
|
||||
top,
|
||||
width,
|
||||
height,
|
||||
right: left + width,
|
||||
bottom: top + height,
|
||||
x: left,
|
||||
y: top,
|
||||
toJSON: () => ({}),
|
||||
} as DOMRect;
|
||||
}
|
||||
|
||||
interface Fake {
|
||||
host: HTMLElement;
|
||||
textarea: HTMLTextAreaElement;
|
||||
term: Terminal;
|
||||
/** Runs every listener xterm's render and cursor-move events would fire. */
|
||||
tick(): void;
|
||||
/** Moves the terminal cursor, as the program's next frame would. */
|
||||
cursor(x: number, y: number): void;
|
||||
}
|
||||
|
||||
function fake({
|
||||
cursorX = 0,
|
||||
cursorY = 0,
|
||||
cols = 80,
|
||||
rows = 20,
|
||||
showCursor = false,
|
||||
viewportY = 0,
|
||||
baseY = 0,
|
||||
} = {}): Fake {
|
||||
const host = document.createElement("div");
|
||||
const screen = document.createElement("div");
|
||||
screen.className = "xterm-screen";
|
||||
// 80 columns × 20 rows of 8 × 18 px cells.
|
||||
screen.getBoundingClientRect = () => rect(40, 40, 640, 360);
|
||||
const rowList = document.createElement("div");
|
||||
rowList.className = "xterm-rows";
|
||||
screen.append(rowList);
|
||||
const textarea = document.createElement("textarea");
|
||||
textarea.className = "xterm-helper-textarea";
|
||||
textarea.style.width = "8px";
|
||||
textarea.style.height = "18px";
|
||||
host.append(screen, textarea);
|
||||
const listeners: (() => void)[] = [];
|
||||
const active = { cursorX, cursorY, viewportY, baseY };
|
||||
const term = {
|
||||
textarea,
|
||||
cols,
|
||||
rows,
|
||||
buffer: { active },
|
||||
modes: { showCursor },
|
||||
onRender: (fn: () => void) => {
|
||||
listeners.push(fn);
|
||||
return { dispose: () => listeners.splice(listeners.indexOf(fn), 1) };
|
||||
},
|
||||
onCursorMove: (fn: () => void) => {
|
||||
listeners.push(fn);
|
||||
return { dispose: () => listeners.splice(listeners.indexOf(fn), 1) };
|
||||
},
|
||||
} as unknown as Terminal;
|
||||
return {
|
||||
host,
|
||||
textarea,
|
||||
term,
|
||||
tick: () => listeners.forEach((fn) => fn()),
|
||||
cursor: (x: number, y: number) => {
|
||||
active.cursorX = x;
|
||||
active.cursorY = y;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/** A reverse-video cell at `left`/`top`, on `row` of the rendered grid. */
|
||||
function addCaret(f: Fake, left: number, top: number, row = 0) {
|
||||
const rowList = f.host.querySelector(".xterm-rows")!;
|
||||
while (rowList.children.length <= row) rowList.append(document.createElement("div"));
|
||||
const span = document.createElement("span");
|
||||
span.className = "xterm-bg-257";
|
||||
span.textContent = " ";
|
||||
span.getBoundingClientRect = () => rect(left, top);
|
||||
rowList.children[row]!.append(span);
|
||||
}
|
||||
|
||||
/** Lets the `style` observer run, which is a microtask. */
|
||||
const settle = () => new Promise((resolve) => setTimeout(resolve, 0));
|
||||
|
||||
describe("installImeAnchor", () => {
|
||||
it("puts the IME anchor on the caret a program drew for itself", () => {
|
||||
const f = fake({ cursorX: 79, cursorY: 3 });
|
||||
// The caret cell sits 60px right of and 50px below the screen's origin.
|
||||
addCaret(f, 100, 90, 3);
|
||||
installImeAnchor({ host: f.host, term: f.term });
|
||||
|
||||
f.tick();
|
||||
|
||||
expect(f.textarea.style.left).toBe("60px");
|
||||
expect(f.textarea.style.top).toBe("50px");
|
||||
});
|
||||
|
||||
it("keeps the anchor inside the terminal, where the input method reads the caret", () => {
|
||||
const f = fake({ cursorX: 79 });
|
||||
addCaret(f, 100, 90);
|
||||
installImeAnchor({ host: f.host, term: f.term });
|
||||
|
||||
f.tick();
|
||||
|
||||
// A position outside the screen's box makes Windows put the candidate window in a corner.
|
||||
const left = Number.parseFloat(f.textarea.style.left);
|
||||
const top = Number.parseFloat(f.textarea.style.top);
|
||||
expect(left).toBeGreaterThanOrEqual(0);
|
||||
expect(top).toBeGreaterThanOrEqual(0);
|
||||
expect(left).toBeLessThan(640);
|
||||
expect(top).toBeLessThan(360);
|
||||
});
|
||||
|
||||
it("leaves xterm's own anchor alone while the cursor is inside the screen", () => {
|
||||
const f = fake({ cursorX: 12 });
|
||||
addCaret(f, 100, 90);
|
||||
installImeAnchor({ host: f.host, term: f.term });
|
||||
|
||||
f.tick();
|
||||
|
||||
expect(f.textarea.style.left).toBe("");
|
||||
expect(f.textarea.style.top).toBe("");
|
||||
});
|
||||
|
||||
it.each([
|
||||
["the terminal cursor is visible", { showCursor: true }],
|
||||
["the terminal is showing scrollback", { baseY: 30, viewportY: 15 }],
|
||||
] as const)("ignores reverse video when %s", (_scenario, options) => {
|
||||
const f = fake({ cursorX: 79, ...options });
|
||||
addCaret(f, 100, 90);
|
||||
installImeAnchor({ host: f.host, term: f.term });
|
||||
|
||||
f.tick();
|
||||
|
||||
expect(f.textarea.style.left).toBe("");
|
||||
expect(f.textarea.style.top).toBe("");
|
||||
});
|
||||
|
||||
it("ignores xterm's own cursor cell and takes the program's caret", () => {
|
||||
const f = fake({ cursorX: 79 });
|
||||
// The terminal cursor's own cell: column 79 (screen origin 40,40; 8x18 cells).
|
||||
addCaret(f, 40 + 79 * 8, 40);
|
||||
// The caret the program drew.
|
||||
addCaret(f, 100, 90);
|
||||
installImeAnchor({ host: f.host, term: f.term });
|
||||
|
||||
f.tick();
|
||||
|
||||
expect(f.textarea.style.left).toBe("60px");
|
||||
});
|
||||
it("still anchors when xterm clamps the cursor a cell or two short of the edge", () => {
|
||||
// Measured in pi: the clamped cursor ends up at cols-2 (and one past the row), never always
|
||||
// at the last cell.
|
||||
for (const cursorX of [78, 79, 80]) {
|
||||
const f = fake({ cursorX });
|
||||
addCaret(f, 100, 90);
|
||||
installImeAnchor({ host: f.host, term: f.term });
|
||||
|
||||
f.tick();
|
||||
|
||||
expect(f.textarea.style.left).toBe("60px");
|
||||
}
|
||||
});
|
||||
|
||||
it("prefers the caret on the terminal cursor's row", () => {
|
||||
const f = fake({ cursorX: 79, cursorY: 5 });
|
||||
addCaret(f, 300, 40, 0); // some other reverse-video cell, e.g. a highlighted row
|
||||
addCaret(f, 100, 150, 5);
|
||||
installImeAnchor({ host: f.host, term: f.term });
|
||||
|
||||
f.tick();
|
||||
|
||||
expect(f.textarea.style.left).toBe("60px");
|
||||
expect(f.textarea.style.top).toBe("110px");
|
||||
});
|
||||
|
||||
it("takes a single caret from another row when the program's cursor drifted", () => {
|
||||
// pi scrolls its input box but keeps asking for the last column of the row its hardware cursor
|
||||
// landed on, three rows below the caret: the only reverse-video cell is still the caret.
|
||||
const f = fake({ cursorX: 79, cursorY: 27 });
|
||||
addCaret(f, 100, 90, 24);
|
||||
installImeAnchor({ host: f.host, term: f.term });
|
||||
|
||||
f.tick();
|
||||
|
||||
expect(f.textarea.style.left).toBe("60px");
|
||||
expect(f.textarea.style.top).toBe("50px");
|
||||
});
|
||||
|
||||
it("gives up when the cells elsewhere are not the cursor and there is more than one", () => {
|
||||
const f = fake({ cursorX: 79, cursorY: 5 });
|
||||
addCaret(f, 100, 40, 0);
|
||||
addCaret(f, 300, 40, 1);
|
||||
installImeAnchor({ host: f.host, term: f.term });
|
||||
|
||||
f.tick();
|
||||
|
||||
expect(f.textarea.style.left).toBe("");
|
||||
});
|
||||
|
||||
it("leaves xterm's own anchor alone when two cells are not the cursor", () => {
|
||||
const f = fake({ cursorX: 79 });
|
||||
addCaret(f, 100, 90);
|
||||
addCaret(f, 300, 90); // a selection or a highlighted row, not a caret
|
||||
installImeAnchor({ host: f.host, term: f.term });
|
||||
|
||||
f.tick();
|
||||
|
||||
expect(f.textarea.style.left).toBe("");
|
||||
});
|
||||
|
||||
it("puts the anchor back when xterm re-places it on the cursor", async () => {
|
||||
const f = fake({ cursorX: 79 });
|
||||
addCaret(f, 100, 90);
|
||||
installImeAnchor({ host: f.host, term: f.term });
|
||||
f.tick();
|
||||
|
||||
// What xterm's composition helper writes on `compositionstart` and every update.
|
||||
f.textarea.style.left = "616.8px";
|
||||
f.textarea.style.top = "0px";
|
||||
await settle();
|
||||
|
||||
expect(f.textarea.style.left).toBe("60px");
|
||||
expect(f.textarea.style.top).toBe("50px");
|
||||
});
|
||||
|
||||
it("keeps the caret offset through a frame that misses it", async () => {
|
||||
const f = fake({ cursorX: 79 });
|
||||
addCaret(f, 100, 90);
|
||||
installImeAnchor({ host: f.host, term: f.term });
|
||||
f.tick();
|
||||
expect(f.textarea.style.left).toBe("60px");
|
||||
|
||||
// The program redraws without its caret for a moment: the composition opening now must not
|
||||
// fall back to the far end of the row.
|
||||
f.host.querySelector(".xterm-rows")!.replaceChildren();
|
||||
f.tick();
|
||||
await settle();
|
||||
|
||||
expect(f.textarea.style.left).toBe("60px");
|
||||
expect(f.textarea.style.top).toBe("50px");
|
||||
});
|
||||
|
||||
it("hands the position back to xterm when the cursor leaves the last column", async () => {
|
||||
const f = fake({ cursorX: 79 });
|
||||
addCaret(f, 100, 90);
|
||||
installImeAnchor({ host: f.host, term: f.term });
|
||||
f.tick();
|
||||
expect(f.textarea.style.left).toBe("60px");
|
||||
|
||||
// A program that puts its hardware cursor on the caret needs no anchor at all.
|
||||
f.cursor(12, 0);
|
||||
f.tick();
|
||||
await settle();
|
||||
|
||||
expect(f.textarea.style.left).toBe("96px");
|
||||
expect(f.textarea.style.top).toBe("0px");
|
||||
});
|
||||
|
||||
it("moves the composition view too, and stops once disposed", async () => {
|
||||
const f = fake({ cursorX: 79 });
|
||||
addCaret(f, 100, 90);
|
||||
const view = document.createElement("div");
|
||||
view.className = "composition-view";
|
||||
f.host.querySelector(".xterm-screen")!.append(view);
|
||||
const dispose = installImeAnchor({ host: f.host, term: f.term });
|
||||
|
||||
f.tick();
|
||||
expect(view.style.left).toBe("60px");
|
||||
expect(view.hasAttribute("data-openidea-ime-anchor")).toBe(true);
|
||||
|
||||
dispose();
|
||||
expect(view.style.left).toBe("632px");
|
||||
expect(view.hasAttribute("data-openidea-ime-anchor")).toBe(false);
|
||||
// xterm owns the position again: nothing moves it away any more.
|
||||
f.textarea.style.left = "640px";
|
||||
await settle();
|
||||
expect(f.textarea.style.left).toBe("640px");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,195 @@
|
||||
/**
|
||||
* Points the IME at the caret a full-screen program draws itself.
|
||||
*
|
||||
* xterm anchors the IME candidate window on its hidden helper textarea, which it keeps on the
|
||||
* terminal cursor: on every render, on `compositionstart`, and again on each `compositionupdate`.
|
||||
* A TUI that paints its own cursor hides the terminal's and leaves it wherever the frame's last
|
||||
* write ended — pi asks for a column past the right edge of its input box (`ESC[21;411H` in a
|
||||
* 206-column terminal, which xterm clamps to the last column) while it draws the caret itself at
|
||||
* column 5. The candidate window then opens at the far right of the input box, and every
|
||||
* composition started there keeps it there, because xterm re-places the textarea as the input
|
||||
* method opens.
|
||||
*
|
||||
* Nothing in the protocol reports where such a caret is, but the program draws it: a caret painted
|
||||
* with reverse video is an inverted cell, which xterm marks `xterm-bg-257`. While the hidden cursor
|
||||
* sits at the right edge (`EDGE_COLUMNS`) in the live viewport and one such cell stands out, that
|
||||
* cell is taken as the caret: one on the cursor's row wins, and a single one anywhere keeps working
|
||||
* when the program's own row tracking drifts. Anything else is a selection or a highlighted row.
|
||||
*
|
||||
* The textarea is moved with `left`/`top`, i.e. inside the terminal's box: Chromium reports that
|
||||
* rectangle to the input method, and a rectangle outside the window makes Windows open the
|
||||
* candidate window in the window's corner. xterm rewrites those properties from the cursor on its
|
||||
* own schedule, so the position is restored after every render, cursor move and composition event,
|
||||
* and again from a `style` observer, which lands in the microtask right after xterm's write.
|
||||
*/
|
||||
import type { Terminal } from "@xterm/xterm";
|
||||
|
||||
/** xterm's class for a cell drawn in `INVERTED_DEFAULT_COLOR` (reverse video). */
|
||||
const INVERTED = "xterm-bg-257";
|
||||
|
||||
/**
|
||||
* How close to the right edge the terminal cursor has to be for the anchor to apply.
|
||||
*
|
||||
* Such a program asks for a column past the edge, and xterm clamps that to the last cell — but the
|
||||
* cursor it ends up with is not always the last cell: a wrapped pending cell (`cursorX` one past
|
||||
* the row) and requests clamped a cell or two short both showed up while typing in pi, and every
|
||||
* one of them has to count as "the program cannot place its own cursor".
|
||||
*/
|
||||
const EDGE_COLUMNS = 3;
|
||||
|
||||
export interface ImeAnchorOptions {
|
||||
/** The xterm host inside the pane, where `.xterm-screen` lives. */
|
||||
host: HTMLElement;
|
||||
term: Terminal;
|
||||
}
|
||||
|
||||
/** One cell of the grid, in CSS pixels. */
|
||||
interface Cell {
|
||||
width: number;
|
||||
height: number;
|
||||
}
|
||||
|
||||
/** Where the helper textarea belongs, in the screen's own coordinates. */
|
||||
interface Spot {
|
||||
left: number;
|
||||
top: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* The cell a program drew its caret in.
|
||||
*
|
||||
* Every inverted cell is a candidate — xterm draws the terminal's own cursor cell inverted too —
|
||||
* so the one sitting on the terminal cursor is dropped. The caret cell is normally on the cursor's
|
||||
* row, which is where a hardware cursor request clamped by column leaves it, but that row can drift:
|
||||
* pi ends up a few rows below its input box once the viewport scrolls. So a single candidate on the
|
||||
* cursor's row wins, a single candidate anywhere on screen is taken as well, and anything else is a
|
||||
* selection or a highlighted row, not a caret.
|
||||
*/
|
||||
function ownCaret(term: Terminal, host: HTMLElement, cell: Cell): Spot | undefined {
|
||||
const screen = host.querySelector<HTMLElement>(".xterm-screen");
|
||||
if (!screen || cell.width <= 0 || cell.height <= 0) return undefined;
|
||||
const cursor = term.buffer.active;
|
||||
const screenRect = screen.getBoundingClientRect();
|
||||
const cursorCol = Math.min(cursor.cursorX, term.cols - 1);
|
||||
const candidates: { rect: DOMRect; row: number }[] = [];
|
||||
for (const [row, element] of [...host.querySelectorAll<HTMLElement>(".xterm-rows > div")].entries()) {
|
||||
for (const span of element.children) {
|
||||
if (!span.classList.contains(INVERTED)) continue;
|
||||
const rect = span.getBoundingClientRect();
|
||||
// The terminal's own cursor: xterm renders its cell inverted as well.
|
||||
if (row === cursor.cursorY && Math.round((rect.left - screenRect.left) / cell.width) === cursorCol) continue;
|
||||
if (rect.width > screenRect.width / 2) continue;
|
||||
candidates.push({ rect, row });
|
||||
}
|
||||
}
|
||||
const onCursorRow = candidates.filter((c) => c.row === cursor.cursorY);
|
||||
const found = onCursorRow.length === 1 ? onCursorRow[0] : candidates.length === 1 ? candidates[0] : undefined;
|
||||
if (!found) return undefined;
|
||||
return { left: found.rect.left - screenRect.left, top: found.rect.top - screenRect.top };
|
||||
}
|
||||
|
||||
/**
|
||||
* Keeps the IME anchor on the caret such a program draws. Returns a disposer.
|
||||
*
|
||||
* Runs after every render rather than on `compositionstart` alone: an input method asks the
|
||||
* focused element for its caret rectangle as it opens, and a reposition made from that event races
|
||||
* xterm's own handling of the same event, which puts the textarea back on the terminal cursor.
|
||||
*/
|
||||
export function installImeAnchor({ host, term }: ImeAnchorOptions): () => void {
|
||||
const textarea = term.textarea;
|
||||
if (!textarea) return () => {};
|
||||
const preedit = host.querySelector<HTMLElement>(".composition-view");
|
||||
const elements = preedit ? [textarea, preedit] : [textarea];
|
||||
|
||||
/** The caret cell, or `null` while xterm's own placement is the right one. */
|
||||
let target: Spot | null = null;
|
||||
/** Whether the anchor currently holds a position of ours that xterm would undo. */
|
||||
let moved = false;
|
||||
/**
|
||||
* The last caret seen, as an offset from the terminal cursor's cell. A frame can miss it — the
|
||||
* program may be mid-redraw, or two cells of its caret may be on screen for a moment — and the
|
||||
* composition that opens in such a frame must not fall back to the far end of the row.
|
||||
*/
|
||||
let caretFromCursor: Spot | null = null;
|
||||
|
||||
const write = (spot: Spot | undefined) => {
|
||||
if (!spot) return;
|
||||
const left = `${spot.left}px`;
|
||||
const top = `${spot.top}px`;
|
||||
for (const { style } of elements) {
|
||||
if (style.left !== left) style.left = left;
|
||||
if (style.top !== top) style.top = top;
|
||||
}
|
||||
};
|
||||
|
||||
/** What xterm itself writes: the cursor cell, clamped to the last column. */
|
||||
const cursorSpot = (): Spot | undefined => {
|
||||
const cols = term.cols;
|
||||
const screen = host.querySelector<HTMLElement>(".xterm-screen");
|
||||
if (!screen || cols <= 0) return undefined;
|
||||
const screenRect = screen.getBoundingClientRect();
|
||||
const cell = { width: screenRect.width / cols, height: term.rows > 0 ? screenRect.height / term.rows : 0 };
|
||||
if (cell.width <= 0 || cell.height <= 0) return undefined;
|
||||
const cursor = term.buffer.active;
|
||||
return { left: Math.min(cursor.cursorX, cols - 1) * cell.width, top: cursor.cursorY * cell.height };
|
||||
};
|
||||
|
||||
const place = () => {
|
||||
const cols = term.cols;
|
||||
const screen = host.querySelector<HTMLElement>(".xterm-screen");
|
||||
const buffer = term.buffer.active;
|
||||
target = null;
|
||||
const cursor = cursorSpot();
|
||||
if (
|
||||
screen &&
|
||||
cursor &&
|
||||
!term.modes.showCursor &&
|
||||
buffer.viewportY === buffer.baseY &&
|
||||
buffer.cursorX >= cols - EDGE_COLUMNS
|
||||
) {
|
||||
const screenRect = screen.getBoundingClientRect();
|
||||
const cell = { width: screenRect.width / cols, height: term.rows > 0 ? screenRect.height / term.rows : 0 };
|
||||
const anchor = ownCaret(term, host, cell);
|
||||
if (anchor) caretFromCursor = { left: anchor.left - cursor.left, top: anchor.top - cursor.top };
|
||||
target =
|
||||
anchor ??
|
||||
(caretFromCursor && { left: cursor.left + caretFromCursor.left, top: cursor.top + caretFromCursor.top });
|
||||
} else {
|
||||
caretFromCursor = null;
|
||||
}
|
||||
if (target) {
|
||||
if (!moved) preedit?.setAttribute("data-openidea-ime-anchor", "");
|
||||
moved = true;
|
||||
write(target);
|
||||
} else if (moved) {
|
||||
// The drawn caret is gone: hand the position back to xterm.
|
||||
moved = false;
|
||||
preedit?.removeAttribute("data-openidea-ime-anchor");
|
||||
write(cursorSpot());
|
||||
}
|
||||
};
|
||||
|
||||
// Registered after `open()`, so these run after xterm's own render and cursor handlers have
|
||||
// put the textarea back on the terminal cursor.
|
||||
const render = term.onRender(() => place());
|
||||
const cursorMove = term.onCursorMove(() => place());
|
||||
|
||||
// xterm re-places the textarea as the input method opens and on every update; the observer lands
|
||||
// in the microtask after that write, before the browser asks for the caret rectangle.
|
||||
const observer = new MutationObserver(() => {
|
||||
if (target) write(target);
|
||||
});
|
||||
for (const element of elements) observer.observe(element, { attributes: true, attributeFilter: ["style"] });
|
||||
|
||||
return () => {
|
||||
render.dispose();
|
||||
cursorMove.dispose();
|
||||
observer.disconnect();
|
||||
target = null;
|
||||
if (moved) {
|
||||
moved = false;
|
||||
preedit?.removeAttribute("data-openidea-ime-anchor");
|
||||
write(cursorSpot());
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -24,6 +24,7 @@ import { installSelectionHighlights, type SelectionHighlightController } from ".
|
||||
import { installLinkHighlights, xtermLink, type LinkHighlightController } from "./linkHighlights";
|
||||
import { installTerminalPaste, pasteIntoTerminal, pasteTerminalClipboard } from "./paste";
|
||||
import { ImeCommitGate } from "./imeCommit";
|
||||
import { installImeAnchor } from "./imeAnchor";
|
||||
import { ownTerminalInput } from "./imeInput";
|
||||
import { findTerminalLinks, resolvePath, type TerminalLink } from "./links";
|
||||
import { parseShellCommand } from "@/platform/commandLine";
|
||||
@@ -681,6 +682,7 @@ export class TerminalSession {
|
||||
this.opts.onFocus(this.key);
|
||||
});
|
||||
this.watchImeCommit();
|
||||
this.disposables.push({ dispose: installImeAnchor({ host: this.host, term: this.term }) });
|
||||
if (this.pendingFocus) {
|
||||
this.pendingFocus = false;
|
||||
if (this.focusStayedHere()) this.focus();
|
||||
|
||||
@@ -412,11 +412,12 @@ describe("terminalFont", () => {
|
||||
it("prefers the terminal's fallback over the editor's", () => {
|
||||
const font = terminalFont(
|
||||
{ ...t, fallbackFontFamily: "Sarasa Mono SC" },
|
||||
{ ...editor, fallbackFont: "PingFang SC" },
|
||||
{ ...editor, fallbackFont: "Source Code Pro" },
|
||||
BASE,
|
||||
);
|
||||
expect(font.family).toContain('"Sarasa Mono SC"');
|
||||
expect(font.family).not.toContain("PingFang SC");
|
||||
// The CJK faces are always named (see the test below), so the editor's pick must not be one.
|
||||
expect(font.family).not.toContain("Source Code Pro");
|
||||
});
|
||||
|
||||
it("follows the editor's Typography Settings weights with Use editor font", () => {
|
||||
@@ -446,6 +447,13 @@ describe("terminalFont", () => {
|
||||
expect(terminalFont({ ...t, useEditorFont: false }, editor, BASE).letterSpacing).toBe(0);
|
||||
expect(terminalFont({ ...t, useEditorFont: false, columnWidth: 1.5 }, editor, BASE).letterSpacing).toBe(3);
|
||||
});
|
||||
it("names CJK faces after the icon fonts so a wide character keeps two cells", () => {
|
||||
const { family } = terminalFont(t, editor, BASE);
|
||||
// After the icon fonts: they must win their own glyphs, and only CJK falls through to these.
|
||||
expect(family.indexOf('"Microsoft YaHei"')).toBeGreaterThan(family.indexOf('"Symbols Nerd Font"'));
|
||||
expect(family).toContain('"PingFang SC", "Noto Sans CJK SC", "Noto Sans SC", "Source Han Sans SC"');
|
||||
expect(family.endsWith(', "Source Han Sans SC", monospace')).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("terminal tab titles", () => {
|
||||
|
||||
@@ -6,7 +6,14 @@
|
||||
*/
|
||||
import type { ITheme, Terminal } from "@xterm/xterm";
|
||||
import { fontWeightOf } from "@/platform/editorSettings";
|
||||
import { fontsLoaded, monoStack, nerdFontFaces, resolveMonoFont } from "@/platform/fonts";
|
||||
import {
|
||||
CJK_FALLBACK_FACES,
|
||||
fontsLoaded,
|
||||
monoStack,
|
||||
nerdFontFaces,
|
||||
quoteFontFamily,
|
||||
resolveMonoFont,
|
||||
} from "@/platform/fonts";
|
||||
import { computedTokenColor, parseColor, type Rgba } from "@/platform/cssColor";
|
||||
|
||||
export { parseColor, type Rgba };
|
||||
@@ -178,9 +185,14 @@ export interface TerminalFont {
|
||||
boldWeight: number;
|
||||
}
|
||||
|
||||
/** Nerd Fonts before the generic family, so per-glyph fallback finds prompt icons. */
|
||||
/**
|
||||
* Nerd Fonts before the generic family, so per-glyph fallback finds prompt icons, then the CJK
|
||||
* faces: a wide character xterm only finds through the webview's own fallback measures one way and
|
||||
* draws another (see `CJK_FALLBACK_FACES`), which drifts any row that mixes CJK with a background.
|
||||
*/
|
||||
function withNerdFonts(family: string): string {
|
||||
return `${family.replace(/,\s*monospace\s*$/i, "")}, ${nerdFontFaces().join(", ")}, monospace`;
|
||||
const cjk = CJK_FALLBACK_FACES.map(quoteFontFamily).join(", ");
|
||||
return `${family.replace(/,\s*monospace\s*$/i, "")}, ${nerdFontFaces().join(", ")}, ${cjk}, monospace`;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -17,6 +17,15 @@
|
||||
overflow: clip;
|
||||
}
|
||||
|
||||
/* During a composition xterm clips its composition view to the space between the cursor and the
|
||||
right edge, and sizes the helper textarea from that view. A program that leaves the cursor in the
|
||||
last column therefore leaves one cell of room, and the pre-edit text is painted almost invisibly.
|
||||
The view is drawn on the caret instead (imeAnchor), so the width that matters is the space from
|
||||
that caret to the edge — which `.xterm` clips anyway. */
|
||||
.xterm .composition-view[data-openidea-ime-anchor] {
|
||||
max-width: none !important;
|
||||
}
|
||||
|
||||
/* An icon or fallback symbol wider than its cell stays over the next cell's background
|
||||
(domRowCache), while selected spans keep xterm's `.xterm-decoration-top` z-index (2). */
|
||||
.xterm .xterm-rows .openidea-terminal-icon:not(.xterm-decoration-top) {
|
||||
|
||||
@@ -121,6 +121,24 @@ const NERD_FONTS = [
|
||||
"Hasklug Nerd Font",
|
||||
"CaskaydiaCove Nerd Font",
|
||||
];
|
||||
|
||||
/**
|
||||
* CJK faces, named after the icon fonts on a grid's font stack. Such a renderer measures each
|
||||
* glyph once to derive its per-cell letter spacing, and when the face drawing wide characters is
|
||||
* only implicit (the webview's own fallback) that measurement and the rendering can disagree by
|
||||
* about a pixel per character: every wide character then takes less than its two cells, and rows
|
||||
* that mix CJK with a full-width background — pi's overlay panel, a boxed CLI — drift left, by up
|
||||
* to two cells across one panel. Naming the face makes measurement and rendering use the same one,
|
||||
* whatever its own advance, and two cells stay two cells. The webview skips missing families, so
|
||||
* one list covers every platform.
|
||||
*/
|
||||
export const CJK_FALLBACK_FACES = [
|
||||
"Microsoft YaHei",
|
||||
"PingFang SC",
|
||||
"Noto Sans CJK SC",
|
||||
"Noto Sans SC",
|
||||
"Source Han Sans SC",
|
||||
];
|
||||
/** In running text the proportional faces give icons their own width instead of one cell's. */
|
||||
const UI_NERD_FONTS = [
|
||||
"JetBrainsMono Nerd Font Propo",
|
||||
|
||||
Reference in New Issue
Block a user