From 90c08fe17dc5372423ebee2cf41fd49350a8f9e3 Mon Sep 17 00:00:00 2001 From: meixg Date: Tue, 28 Feb 2023 17:30:08 +0800 Subject: [PATCH 1/3] 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 84eddf6e86322d..52ac0d07c41b35 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 = (noCompletion = false) => { // Prevent duplicated previews after a refresh. if (inputPreview !== null || !repl.isCompletionEnabled) { return; @@ -380,7 +380,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 241e25f0f2095a..372b40594e8e20 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; From 3ede2675eed78465d28e21ad449cb781489a57d9 Mon Sep 17 00:00:00 2001 From: Xuguang Mei Date: Tue, 28 Feb 2023 18:26:16 +0800 Subject: [PATCH 2/3] Change noCompletion to showCompletion Co-authored-by: Ruben Bridgewater --- lib/internal/repl/utils.js | 6 ++++-- lib/repl.js | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/internal/repl/utils.js b/lib/internal/repl/utils.js index 52ac0d07c41b35..ad34e064c29138 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 = (noCompletion = false) => { + const showPreview = (showCompletion = true) => { // Prevent duplicated previews after a refresh. if (inputPreview !== null || !repl.isCompletionEnabled) { return; @@ -380,7 +380,9 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active) { // Add the autocompletion preview. const insertPreview = false; - !noCompletion && showCompletionPreview(repl.line, insertPreview); + if (showCompletion) { + 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 372b40594e8e20..3240a66101474d 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -993,7 +993,8 @@ function REPLServer(prompt, clearPreview(key); if (!reverseSearch(d, key)) { ttyWrite(d, key); - showPreview(key.name === 'escape'); + const showCompletionPreview = key.name !== 'escape'; + showPreview(showCompletionPreview); } return; } From 3e54f3af463afbca9acaa3cf39c9092479f391a2 Mon Sep 17 00:00:00 2001 From: meixg Date: Fri, 3 Mar 2023 11:26:27 +0800 Subject: [PATCH 3/3] repl: preserve preview on ESCAPE key press change according to cr --- lib/internal/repl/utils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/repl/utils.js b/lib/internal/repl/utils.js index ad34e064c29138..f3697aa9b68ae2 100644 --- a/lib/internal/repl/utils.js +++ b/lib/internal/repl/utils.js @@ -379,8 +379,8 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active) { hasCompletions = false; // Add the autocompletion preview. - const insertPreview = false; if (showCompletion) { + const insertPreview = false; showCompletionPreview(repl.line, insertPreview); }