Skip to content

Commit

Permalink
repl: preserve preview on ESCAPE key press
Browse files Browse the repository at this point in the history
  • Loading branch information
meixg committed Feb 28, 2023
1 parent 225c578 commit 21dc2b9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
4 changes: 2 additions & 2 deletions lib/internal/repl/utils.js
Expand Up @@ -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;
Expand All @@ -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]) {
Expand Down
4 changes: 1 addition & 3 deletions lib/repl.js
Expand Up @@ -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;
}
Expand Down
22 changes: 20 additions & 2 deletions test/parallel/test-repl-history-navigation.js
Expand Up @@ -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;

Expand All @@ -633,9 +651,9 @@ function cleanupTmpFile() {
function runTest() {
const opts = tests.shift();
if (!opts) return; // All done

const { expected, skip } = opts;

// Test unsupported on platform.
if (skip) {
setImmediate(runTestWrap, true);
Expand Down

0 comments on commit 21dc2b9

Please sign in to comment.