Skip to content

Commit ffdbb59

Browse files
committed
repl: add block-based history navigation
When --experimental-repl-enhancements is enabled, the Up and Down arrow keys now navigate between history entries as whole blocks instead of moving line-by-line within a recalled multiline entry. Previously, recalling a multiline function from history and pressing Up would move the cursor within that entry's lines before advancing to the previous history entry. With this change, Up/Down always jump directly to the previous/next history entry, matching the behavior of Ctrl-P/Ctrl-N. Fixes: #48146 Signed-off-by: hemanth <hemanth.hm@gmail.com>
1 parent eec5763 commit ffdbb59

3 files changed

Lines changed: 34 additions & 3 deletions

File tree

doc/api/cli.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,8 +1390,8 @@ added: REPLACEME
13901390
> Stability: 1 - Experimental
13911391
13921392
Enable experimental REPL enhancements including inline syntax highlighting,
1393-
auto-indentation, and function signature hints. See the
1394-
[REPL][] documentation for details.
1393+
auto-indentation, function signature hints, and block-based history
1394+
navigation. See the [REPL][] documentation for details.
13951395

13961396
### `--experimental-sea-config`
13971397

doc/api/repl.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,22 @@ inspector protocol to safely extract function signatures without side
498498
effects. Signature hints require the `preview` option to be enabled and
499499
the inspector to be available.
500500

501+
### Block-based history navigation
502+
503+
<!-- YAML
504+
added: REPLACEME
505+
-->
506+
507+
> Stability: 1 - Experimental
508+
509+
When the `--experimental-repl-enhancements` flag is enabled, pressing the
510+
Up and Down arrow keys navigates between history entries as whole blocks.
511+
Without this flag, pressing Up within a recalled multiline entry first moves
512+
the cursor through the entry's lines before advancing to the previous history
513+
entry. With the flag enabled, Up/Down always jump directly to the
514+
previous/next history entry, making it easy to recall multiline functions
515+
and code blocks.
516+
501517
## Class: `REPLServer`
502518

503519
<!-- YAML

lib/repl.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ const {
175175
kMultilinePrompt,
176176
kAddNewLineOnTTY,
177177
kLastCommandErrored,
178+
kHistoryPrev,
179+
kHistoryNext,
178180
} = require('internal/readline/interface');
179181

180182
// Lazy-loaded.
@@ -967,7 +969,20 @@ class REPLServer extends Interface {
967969
clearPreview(key);
968970
clearSignatureHint();
969971
if (!reverseSearch(d, key)) {
970-
ttyWrite(d, key);
972+
// When enhancements are enabled, Up/Down navigate history in
973+
// blocks rather than moving line-by-line within a recalled
974+
// multiline entry. This makes arrow-key history navigation
975+
// behave like Ctrl-P / Ctrl-N (see issue #48146).
976+
if (experimentalREPLEnhancements &&
977+
(key.name === 'up' || key.name === 'down')) {
978+
if (key.name === 'up') {
979+
self[kHistoryPrev]();
980+
} else {
981+
self[kHistoryNext]();
982+
}
983+
} else {
984+
ttyWrite(d, key);
985+
}
971986
const showCompletionPreview = key.name !== 'escape';
972987
showPreview(showCompletionPreview);
973988
// Show signature hint when user types '('.

0 commit comments

Comments
 (0)