From a9858fe0d3fec190f5825a7e5a75b426caba8c4e Mon Sep 17 00:00:00 2001 From: Max H Fisher Date: Wed, 11 Nov 2026 17:29:32 -0500 Subject: [PATCH 1/2] repl: redraw highlighted input once per chunk Signed-off-by: Max H Fisher --- lib/internal/readline/interface.js | 42 ++++++++++- lib/repl.js | 7 ++ test/parallel/test-repl-paste-highlight.js | 84 ++++++++++++++++++++++ 3 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-repl-paste-highlight.js diff --git a/lib/internal/readline/interface.js b/lib/internal/readline/interface.js index bc8f30427616..ac73131b523e 100644 --- a/lib/internal/readline/interface.js +++ b/lib/internal/readline/interface.js @@ -158,6 +158,9 @@ const kMultilineMove = Symbol('_multilineMove'); const kPreviousPrevRows = Symbol('_previousPrevRows'); const kAddNewLineOnTTY = Symbol('_addNewLineOnTTY'); const kColorize = Symbol('_colorize'); +const kRefreshPending = Symbol('_refreshPending'); +const kFlushPendingRefresh = Symbol('_flushPendingRefresh'); +const kWillDeferRefresh = Symbol('_willDeferRefresh'); function InterfaceConstructor(input, output, completer, terminal) { this[kSawReturnAt] = 0; @@ -166,6 +169,7 @@ function InterfaceConstructor(input, output, completer, terminal) { this.isCompletionEnabled = true; this[kSawKeyPress] = false; this[kColorize] = undefined; + this[kRefreshPending] = false; this[kPreviousKey] = null; this.escapeCodeTimeout = ESCAPE_CODE_TIMEOUT; this.tabSize = 8; @@ -502,7 +506,34 @@ class Interface extends InterfaceConstructor { return this.historyManager.addHistory(this[kIsMultiline], this[kLastCommandErrored]); } + [kFlushPendingRefresh]() { + if (this[kRefreshPending]) { + this[kRefreshLine](); + } + } + + // Performance optimization for pasted input. Input arrives in chunks: each + // 'data' event from the input stream may carry many characters at once (a + // paste often does), and emitKeypressEvents() turns a chunk into one + // keypress per character, with isCompletionEnabled set to false for all but + // the chunk's last character. Redrawing a colorized line on every one of + // those keypresses makes pasting quadratic, so plain characters inserted in + // the middle of a chunk only update the line buffer and leave a redraw + // pending, and the chunk's last keypress redraws the line. + // Returns true if this keypress is such a mid-chunk insertion; any other + // keypress must flush the pending redraw before it writes to the output. + [kWillDeferRefresh](s, key) { + if (this[kColorize] === undefined || this.isCompletionEnabled) return false; + if (typeof s !== 'string' || s === '' || key?.ctrl || key?.meta) { + return false; + } + const name = key?.name; + return name === undefined || name.length === 1 || + name === 'space' || name === 'tab'; + } + [kRefreshLine]() { + this[kRefreshPending] = false; // line length const line = this[kPrompt] + this.line; const dispPos = this[kGetDisplayPos](line); @@ -562,6 +593,7 @@ class Interface extends InterfaceConstructor { */ close() { if (this.closed) return; + this[kFlushPendingRefresh](); this.pause(); if (this.terminal) { this[kSetRawMode](false); @@ -696,7 +728,9 @@ class Interface extends InterfaceConstructor { if (this[kColorize] === undefined) { this[kWriteToOutput](c); } else { - this[kRefreshLine](); + // More input from the same chunk follows synchronously; the last + // character of the chunk redraws the line once. + this[kRefreshPending] = true; } return; } @@ -963,6 +997,7 @@ class Interface extends InterfaceConstructor { } clearLine() { + this[kFlushPendingRefresh](); this[kMoveCursor](+Infinity); this[kWriteToOutput]('\r\n'); this[kSetLine](''); @@ -1283,6 +1318,9 @@ class Interface extends InterfaceConstructor { [kTtyWrite](s, key) { const previousKey = this[kPreviousKey]; key ||= kEmptyObject; + if (!this[kWillDeferRefresh](s, key)) { + this[kFlushPendingRefresh](); + } this[kPreviousKey] = key; let shouldResetPreviousCursorCols = true; @@ -1630,6 +1668,8 @@ module.exports = { kQuestionCancel, kQuestionReject, kRefreshLine, + kFlushPendingRefresh, + kWillDeferRefresh, kSawKeyPress, kSawReturnAt, kSetRawMode, diff --git a/lib/repl.js b/lib/repl.js index 4c227a0c17da..712f0602a804 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -150,6 +150,8 @@ const { kMultilinePrompt, kAddNewLineOnTTY, kLastCommandErrored, + kFlushPendingRefresh, + kWillDeferRefresh, } = require('internal/readline/interface'); const parentModule = module; @@ -696,6 +698,11 @@ class REPLServer extends Interface { ['key', [d, key], self.isCompletionEnabled]); return; } + // Redraw a line whose refresh was deferred within the current input + // chunk before anything else writes to the terminal. + if (!self[kWillDeferRefresh](d, key)) { + self[kFlushPendingRefresh](); + } if (!self.editorMode || !self.terminal) { // Before exiting, make sure to clear the line. if (key.ctrl && key.name === 'd' && diff --git a/test/parallel/test-repl-paste-highlight.js b/test/parallel/test-repl-paste-highlight.js new file mode 100644 index 000000000000..152bddf684e8 --- /dev/null +++ b/test/parallel/test-repl-paste-highlight.js @@ -0,0 +1,84 @@ +// Flags: --expose-internals +'use strict'; + +// Pasted text reaches the REPL as a single input chunk containing many +// characters. With syntax highlighting enabled, check that such a chunk leaves +// the correct line buffer, highlighted rendering and evaluated result. + +const common = require('../common'); +const assert = require('assert'); +const { highlight } = require('internal/repl/highlight'); +const { startNewREPLServer } = require('../common/repl'); + +common.skipIfInspectorDisabled(); + +// Everything written since the most recent full redraw of the input. +function lastRedraw(rendered) { + const clearScreenDown = '\x1b[0J'; + return rendered.slice(rendered.lastIndexOf(clearScreenDown) + clearScreenDown.length); +} + +// The expected rendering is built with the REPL's own highlighter on purpose. +// This test is not about which colors highlight() picks (that is covered by +// test-repl-highlight.js); whatever it produces is taken as correct. The point +// is that a pasted chunk ends up rendered exactly as highlight() renders the +// same text when it is typed, so changes to the highlighting style do not +// require changes here. +function expectedRendering(line) { + const [first, ...rest] = line.split('\n'); + return `> ${highlight(first)}` + rest.map((l) => `\n| ${highlight(l)}`).join(''); +} + +// Pastes `chunk` as one 'data' event, checks the resulting line and its +// rendering, then submits the line and returns the REPL context. +async function paste(chunk, expectedLine) { + const { replServer, output, run } = startNewREPLServer({ + prompt: '> ', + useColors: true, + }); + output.accumulator = ''; + + await run(chunk); + assert.strictEqual(replServer.line, expectedLine); + const rendered = lastRedraw(output.accumulator); + assert.ok( + rendered.startsWith(expectedRendering(expectedLine)), + `unexpected rendering: ${JSON.stringify(rendered)}`, + ); + + await run('\r'); + replServer.close(); + return replServer.context; +} + +function createObject(i) { + return { + id: i, + name: `item-${i}`, + tags: ['alpha', 'beta', 'gamma'], + nested: { enabled: true, ratio: 0.5, label: 'a string value' }, + }; +} + +(async () => { + // Large multi-line object. + { + const objects = []; + for (let i = 0; i < 16; i++) objects.push(createObject(i)); + const lines = `x = ${JSON.stringify(objects, null, 2)}`.split('\n'); + const context = await paste(lines.join('\r'), lines.join('\n')); + assert.strictEqual(JSON.stringify(context.x), JSON.stringify(objects)); + } + + // Long single line. + { + const items = []; + for (let i = 0; i < 100; i++) { + items.push(`{ id: ${i}, name: 'item-${i}', tags: ['a', 'b'] }`); + } + const line = `y = [${items.join(', ')}]`; + const context = await paste(line, line); + assert.strictEqual(context.y.length, 100); + assert.strictEqual(context.y[99].name, 'item-99'); + } +})().then(common.mustCall()); From debfb346bb0755a6235dc915c40dfb06cbd6dc0c Mon Sep 17 00:00:00 2001 From: Max H Fisher Date: Mon, 16 Nov 2026 11:40:00 -0500 Subject: [PATCH 2/2] repl: make last character of chunk redraw once --- lib/internal/readline/interface.js | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/lib/internal/readline/interface.js b/lib/internal/readline/interface.js index ac73131b523e..a9d03228f086 100644 --- a/lib/internal/readline/interface.js +++ b/lib/internal/readline/interface.js @@ -520,16 +520,22 @@ class Interface extends InterfaceConstructor { // those keypresses makes pasting quadratic, so plain characters inserted in // the middle of a chunk only update the line buffer and leave a redraw // pending, and the chunk's last keypress redraws the line. - // Returns true if this keypress is such a mid-chunk insertion; any other - // keypress must flush the pending redraw before it writes to the output. + // Returns true if this keypress inserts a plain character into a colorized + // line. Such a keypress either leaves the redraw pending or, as the last + // keypress of a chunk, redraws the whole line itself, so it does not need a + // pending redraw to be flushed first. Any other keypress must flush the + // pending redraw before it writes to the output. [kWillDeferRefresh](s, key) { - if (this[kColorize] === undefined || this.isCompletionEnabled) return false; + if (this[kColorize] === undefined) return false; if (typeof s !== 'string' || s === '' || key?.ctrl || key?.meta) { return false; } const name = key?.name; - return name === undefined || name.length === 1 || - name === 'space' || name === 'tab'; + if (name === 'tab') { + // Tab triggers completion instead of inserting when completion is on. + return typeof this.completer !== 'function' || !this.isCompletionEnabled; + } + return name === undefined || name.length === 1 || name === 'space'; } [kRefreshLine]() { @@ -728,8 +734,8 @@ class Interface extends InterfaceConstructor { if (this[kColorize] === undefined) { this[kWriteToOutput](c); } else { - // More input from the same chunk follows synchronously; the last - // character of the chunk redraws the line once. + // More input from the same chunk follows synchronously; the line is + // redrawn once the chunk's last character is inserted. this[kRefreshPending] = true; } return;