Commit a6dcfec1307 for nodejs
commit a6dcfec1307c91a0a71b66612bed4eea5c283a50
Author: Max Fisher <66274614+maxhfisher@users.noreply.github.com>
Date: Fri Nov 20 23:17:55 2026 -0500
repl: redraw highlighted input once per chunk
Signed-off-by: Max H Fisher <mfisher187@bloomberg.net>
PR-URL: https://github.com/nodejs/node/pull/67422
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
diff --git a/lib/internal/readline/interface.js b/lib/internal/readline/interface.js
index bc8f3042761..a9d03228f08 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,40 @@ 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 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) return false;
+ if (typeof s !== 'string' || s === '' || key?.ctrl || key?.meta) {
+ return false;
+ }
+ const name = key?.name;
+ 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]() {
+ this[kRefreshPending] = false;
// line length
const line = this[kPrompt] + this.line;
const dispPos = this[kGetDisplayPos](line);
@@ -562,6 +599,7 @@ class Interface extends InterfaceConstructor {
*/
close() {
if (this.closed) return;
+ this[kFlushPendingRefresh]();
this.pause();
if (this.terminal) {
this[kSetRawMode](false);
@@ -696,7 +734,9 @@ class Interface extends InterfaceConstructor {
if (this[kColorize] === undefined) {
this[kWriteToOutput](c);
} else {
- this[kRefreshLine]();
+ // More input from the same chunk follows synchronously; the line is
+ // redrawn once the chunk's last character is inserted.
+ this[kRefreshPending] = true;
}
return;
}
@@ -963,6 +1003,7 @@ class Interface extends InterfaceConstructor {
}
clearLine() {
+ this[kFlushPendingRefresh]();
this[kMoveCursor](+Infinity);
this[kWriteToOutput]('\r\n');
this[kSetLine]('');
@@ -1283,6 +1324,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 +1674,8 @@ module.exports = {
kQuestionCancel,
kQuestionReject,
kRefreshLine,
+ kFlushPendingRefresh,
+ kWillDeferRefresh,
kSawKeyPress,
kSawReturnAt,
kSetRawMode,
diff --git a/lib/repl.js b/lib/repl.js
index 4c227a0c17d..712f0602a80 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 00000000000..152bddf684e
--- /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());