From e668efac6b92e4908a56627a6cec8b0acaaebee9 Mon Sep 17 00:00:00 2001 From: Xuguang Mei Date: Thu, 16 Mar 2023 11:24:23 +0800 Subject: [PATCH] repl: preserve preview on ESCAPE key press Fix: #46876 PR-URL: https://github.com/nodejs/node/pull/46878 Fixes: https://github.com/nodejs/node/issues/46876 Reviewed-By: Ruben Bridgewater --- lib/internal/repl/utils.js | 8 +++++--- lib/repl.js | 5 ++--- test/parallel/test-repl-history-navigation.js | 18 ++++++++++++++++++ 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/lib/internal/repl/utils.js b/lib/internal/repl/utils.js index 84eddf6e86322d..f3697aa9b68ae2 100644 --- a/lib/internal/repl/utils.js +++ b/lib/internal/repl/utils.js @@ -363,7 +363,7 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active) { }, () => callback(new ERR_INSPECTOR_NOT_AVAILABLE())); } - const showPreview = () => { + const showPreview = (showCompletion = true) => { // Prevent duplicated previews after a refresh. if (inputPreview !== null || !repl.isCompletionEnabled) { return; @@ -379,8 +379,10 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active) { hasCompletions = false; // Add the autocompletion preview. - const insertPreview = false; - showCompletionPreview(repl.line, insertPreview); + if (showCompletion) { + const insertPreview = false; + showCompletionPreview(repl.line, insertPreview); + } // Do not preview if the command is buffered. if (repl[bufferSymbol]) { diff --git a/lib/repl.js b/lib/repl.js index ce9e72987f7f27..c81a9e47f2af6e 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -995,9 +995,8 @@ function REPLServer(prompt, clearPreview(key); if (!reverseSearch(d, key)) { ttyWrite(d, key); - if (key.name !== 'escape') { - showPreview(); - } + const showCompletionPreview = key.name !== 'escape'; + showPreview(showCompletionPreview); } return; } diff --git a/test/parallel/test-repl-history-navigation.js b/test/parallel/test-repl-history-navigation.js index 39ccc3732c3d82..a3826e547a27fe 100644 --- a/test/parallel/test-repl-history-navigation.js +++ b/test/parallel/test-repl-history-navigation.js @@ -614,6 +614,24 @@ const tests = [ ], clean: false }, + { + // Test that preview should not be removed when pressing ESCAPE key + env: { NODE_REPL_HISTORY: defaultHistoryPath }, + skip: !process.features.inspector, + test: [ + '1+1', + ESCAPE, + ENTER, + ], + expected: [ + prompt, ...'1+1', + '\n// 2', + '\n// 2', + '2\n', + prompt, + ], + clean: false + }, ]; const numtests = tests.length;