Skip to content

Commit 088a905

Browse files
committed
repl: add syntax highlighting, auto-indentation, and signature hints
Add experimental REPL enhancements behind the --experimental-repl-enhancements flag: 1. Inline syntax highlighting using the Acorn tokenizer to colorize keywords (magenta), strings (green), numbers (yellow), booleans/null (yellow), regular expressions (red), and comments (gray). 2. Auto-indentation for multiline input based on brace/bracket/paren depth, using 2-space indentation. 3. Function signature hints that display parameter lists as dimmed text below the input when the user types a function call. All features are off by default and require the CLI flag to enable. Highlighting is applied at display time only so cursor positioning and evaluation are unaffected. Refs: #48164 Signed-off-by: hemanth <hemanth.hm@gmail.com>
1 parent 14166a5 commit 088a905

8 files changed

Lines changed: 1121 additions & 1 deletion

File tree

doc/api/cli.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,6 +1381,18 @@ added:
13811381
13821382
Enable experimental support for the QUIC protocol.
13831383

1384+
### `--experimental-repl-enhancements`
1385+
1386+
<!-- YAML
1387+
added: REPLACEME
1388+
-->
1389+
1390+
> Stability: 1 - Experimental
1391+
1392+
Enable experimental REPL enhancements including inline syntax highlighting,
1393+
auto-indentation, and function signature hints. See the
1394+
[REPL][] documentation for details.
1395+
13841396
### `--experimental-sea-config`
13851397

13861398
<!-- YAML
@@ -3829,6 +3841,7 @@ one is included in the list below.
38293841
* `--experimental-package-map`
38303842
* `--experimental-print-required-tla`
38313843
* `--experimental-quic`
3844+
* `--experimental-repl-enhancements`
38323845
* `--experimental-require-module`
38333846
* `--experimental-shadow-realm`
38343847
* `--experimental-specifier-resolution`

doc/api/repl.md

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ result. Input and output may be from `stdin` and `stdout`, respectively, or may
2727
be connected to any Node.js [stream][].
2828

2929
Instances of [`repl.REPLServer`][] support automatic completion of inputs,
30-
completion preview, simplistic Emacs-style line editing, multi-line inputs,
30+
completion preview, inline syntax highlighting, automatic indentation,
31+
function signature hints, simplistic Emacs-style line editing, multi-line inputs,
3132
[ZSH][]-like reverse-i-search, [ZSH][]-like substring-based history search,
3233
ANSI-styled output, saving and restoring current REPL session state, error
3334
recovery, and customizable evaluation functions. Terminals that do not support
@@ -439,6 +440,64 @@ function myWriter(output) {
439440
}
440441
```
441442

443+
### Syntax highlighting
444+
445+
<!-- YAML
446+
added: REPLACEME
447+
-->
448+
449+
> Stability: 1 - Experimental
450+
451+
These features require the `--experimental-repl-enhancements` flag.
452+
453+
```bash
454+
node --experimental-repl-enhancements
455+
```
456+
457+
When the flag is enabled and `useColors` is `true`, the REPL highlights
458+
JavaScript input using the following color scheme:
459+
460+
* **Keywords** (`const`, `let`, `function`, `if`, `return`, etc.): Magenta
461+
* **Strings** (single-quoted, double-quoted, and template literals): Green
462+
* **Numbers**: Yellow
463+
* **Boolean literals** (`true`, `false`), `null`, `undefined`, `NaN`,
464+
`Infinity`: Yellow
465+
* **Regular expressions**: Red
466+
* **Comments** (line and block): Gray
467+
468+
Syntax highlighting is applied only to the display; the internal `line`
469+
property remains plain text. Highlighting can be disabled by passing
470+
`syntaxHighlighting: false` to [`repl.start()`][].
471+
472+
### Auto-indentation
473+
474+
<!-- YAML
475+
added: REPLACEME
476+
-->
477+
478+
> Stability: 1 - Experimental
479+
480+
When the `--experimental-repl-enhancements` flag is enabled and entering
481+
multi-line input (e.g., a function body or an object literal), the REPL
482+
automatically indents continuation lines based on the current nesting
483+
depth of braces (`{}`), brackets (`[]`), and parentheses (`()`). The REPL
484+
uses 2-space indentation following the Node.js coding convention.
485+
486+
### Function signature hints
487+
488+
<!-- YAML
489+
added: REPLACEME
490+
-->
491+
492+
> Stability: 1 - Experimental
493+
494+
When the `--experimental-repl-enhancements` flag is enabled and the user
495+
types a function name followed by `(`, the REPL displays the function's
496+
parameter list as a dimmed hint below the input line. This uses the V8
497+
inspector protocol to safely extract function signatures without side
498+
effects. Signature hints require the `preview` option to be enabled and
499+
the inspector to be available.
500+
442501
## Class: `REPLServer`
443502

444503
<!-- YAML
@@ -791,6 +850,12 @@ changes:
791850
previews or not. **Default:** `true` with the default eval function and
792851
`false` in case a custom eval function is used. If `terminal` is falsy, then
793852
there are no previews and the value of `preview` has no effect.
853+
* `syntaxHighlighting` {boolean} If `true`, enables inline syntax
854+
highlighting of REPL input using ANSI color codes. Keywords are displayed
855+
in magenta, strings in green, numbers in yellow, regular expressions in
856+
red, and comments in gray. Requires `--experimental-repl-enhancements`,
857+
`useColors` to be `true`, and `terminal` to be `true`.
858+
**Default:** `true` when `--experimental-repl-enhancements` is set.
794859
* `handleError` {Function} This function customizes error handling in the REPL.
795860
It receives the thrown exception as its first argument and must return one
796861
of the following values synchronously:

0 commit comments

Comments
 (0)