From d636ce8563103885044998cb2d886739e6d7d6a8 Mon Sep 17 00:00:00 2001 From: meixg Date: Tue, 28 Feb 2023 17:30:08 +0800 Subject: [PATCH] repl: preserve preview on ESCAPE key press Fix: #46876 --- lib/internal/repl/utils.js | 4 ++-- lib/repl.js | 4 +--- test/parallel/test-repl-history-navigation.js | 18 ++++++++++++++++++ 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/lib/internal/repl/utils.js b/lib/internal/repl/utils.js index 8405e6758fc7b7..e5bd406544fccd 100644 --- a/lib/internal/repl/utils.js +++ b/lib/internal/repl/utils.js @@ -365,7 +365,7 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active) { }, () => callback(new ERR_INSPECTOR_NOT_AVAILABLE())); } - const showPreview = () => { + const showPreview = (noCompletion = false) => { // Prevent duplicated previews after a refresh. if (inputPreview !== null || !repl.isCompletionEnabled) { return; @@ -382,7 +382,7 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active) { // Add the autocompletion preview. const insertPreview = false; - showCompletionPreview(repl.line, insertPreview); + !noCompletion && 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 3875858871ebfb..0ef3f7835882bc 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -993,9 +993,7 @@ function REPLServer(prompt, clearPreview(key); if (!reverseSearch(d, key)) { ttyWrite(d, key); - if (key.name !== 'escape') { - showPreview(); - } + showPreview(key.name === 'escape'); } 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;