Skip to content

Commit 16ee02c

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 16ee02c

8 files changed

Lines changed: 1301 additions & 1 deletion

File tree

doc/api/repl.md

Lines changed: 67 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,65 @@ function myWriter(output) {
439440
}
440441
```
441442

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

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

0 commit comments

Comments
 (0)