Файли
openidea/src/platform/consoleColumns.ts
T
l0ng-ai 4f0ab6c968 fix(debugger): bound the debug console's output backlog under a log flood (#5847)
Debug output arriving faster than the console parsed it piled up in the per-frame queue, and each frame re-measured the widest line of all 50,000 lines because the growing last line is replaced by a copy. Pending output is now capped at 4 MiB per session (oldest whole lines first), the widest-line measure resumes by line id, lines over 8,000 characters are broken up as in the Run console, and dropped lines are counted and shown as "N earlier lines were discarded".

Fixes #5769
2026-11-25 17:09:51 +08:00

71 рядки
2.6 KiB
TypeScript

/**
* Width of console output in monospace columns. Virtualized consoles place their rows
* absolutely, and such rows only widen the scroll area while they are mounted: without a
* fixed content width the horizontal scrollbar comes and goes as long lines scroll in and
* out of view, and where scrollbars take layout space (Windows) that change of height
* scrolls again, so the view flickers at the bottom.
*/
/** Wide characters take two columns: CJK, Hangul, fullwidth forms and emoji. */
function isWide(c: number): boolean {
return (
(c >= 0x1100 && c <= 0x115f) ||
(c >= 0x2e80 && c <= 0xa4cf) ||
(c >= 0xac00 && c <= 0xd7a3) ||
(c >= 0xf900 && c <= 0xfaff) ||
(c >= 0xfe30 && c <= 0xfe4f) ||
(c >= 0xff00 && c <= 0xff60) ||
(c >= 0xffe0 && c <= 0xffe6) ||
(c >= 0x1f300 && c <= 0x1faff) ||
(c >= 0x20000 && c <= 0x3fffd)
);
}
/** Columns of the widest line in `text`; a tab moves to the next multiple of `tabSize`. */
export function textColumns(text: string, tabSize: number): number {
let col = 0;
let max = 0;
for (const ch of text) {
const c = ch.codePointAt(0)!;
if (c === 10) {
max = Math.max(max, col);
col = 0;
} else if (c === 9) col += tabSize - (col % tabSize);
else if (c >= 32) col += isWide(c) ? 2 : 1;
}
return Math.max(max, col);
}
/**
* The widest of a console's lines, measured incrementally. The lines are append-only
* (front lines may be dropped) and the last one may still grow, so each call measures
* only what came after the last line seen before; anything else (a cleared console)
* measures everything again.
*/
export class WidestColumns {
private last: object | null = null;
private lastId: number | null = null;
private max = 0;
constructor(private readonly tabSize = 4) {}
measure(lines: readonly { text: string; id?: number }[]): number {
let from = this.last ? lines.lastIndexOf(this.last as { text: string }) : -1;
// A store that replaces the growing last line with a copy keeps its id: find it by that,
// walking back only over the lines added since.
if (from < 0 && this.lastId != null) {
let i = lines.length - 1;
while (i >= 0 && (lines[i]!.id ?? -Infinity) > this.lastId) i--;
if (i >= 0 && lines[i]!.id === this.lastId) from = i;
}
if (from < 0) {
this.max = 0;
from = 0;
}
for (let i = from; i < lines.length; i++) this.max = Math.max(this.max, textColumns(lines[i]!.text, this.tabSize));
this.last = lines.at(-1) ?? null;
this.lastId = lines.at(-1)?.id ?? null;
return this.max;
}
}