Skip to content

Commit a7bf2be

Browse files
committed
repl: add syntax highlighting, auto-indentation, and signature hints
Signed-off-by: hemanth <hemanth.hm@gmail.com>
1 parent 4f844f4 commit a7bf2be

5 files changed

Lines changed: 1082 additions & 1 deletion

File tree

doc/api/repl.md

Lines changed: 53 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
@@ -232,6 +233,52 @@ undefined
232233
undefined
233234
```
234235

236+
### Syntax highlighting
237+
238+
<!-- YAML
239+
added: REPLACEME
240+
-->
241+
242+
When `useColors` is `true`, the REPL highlights
243+
JavaScript input using the following color scheme:
244+
245+
* **Keywords** (`const`, `let`, `function`, `if`, `return`, etc.): Magenta
246+
* **Strings** (single-quoted, double-quoted, and template literals): Green
247+
* **Numbers**: Yellow
248+
* **Boolean literals** (`true`, `false`), `null`, `undefined`, `NaN`,
249+
`Infinity`: Yellow
250+
* **Regular expressions**: Red
251+
* **Comments** (line and block): Gray
252+
253+
Syntax highlighting is applied only to the display; the internal `line`
254+
property remains plain text. Highlighting can be disabled by passing
255+
`syntaxHighlighting: false` to [`repl.start()`][].
256+
257+
### Auto-indentation
258+
259+
<!-- YAML
260+
added: REPLACEME
261+
-->
262+
263+
When entering
264+
multi-line input (e.g., a function body or an object literal), the REPL
265+
automatically indents continuation lines based on the current nesting
266+
depth of braces (`{}`), brackets (`[]`), and parentheses (`()`). The REPL
267+
uses 2-space indentation following the Node.js coding convention.
268+
269+
### Function signature hints
270+
271+
<!-- YAML
272+
added: REPLACEME
273+
-->
274+
275+
When the user
276+
types a function name followed by `(`, the REPL displays the function's
277+
parameter list as a dimmed hint below the input line. This uses the V8
278+
inspector protocol to safely extract function signatures without side
279+
effects. Signature hints require the `preview` option to be enabled and
280+
the inspector to be available.
281+
235282
### Reverse-i-search
236283

237284
<!-- YAML
@@ -747,6 +794,11 @@ changes:
747794
previews or not. **Default:** `true` with the default eval function and
748795
`false` in case a custom eval function is used. If `terminal` is falsy, then
749796
there are no previews and the value of `preview` has no effect.
797+
* `syntaxHighlighting` {boolean} If `true`, enables inline syntax
798+
highlighting of REPL input using ANSI color codes. Keywords are displayed
799+
in magenta, strings in green, numbers in yellow, regular expressions in
800+
red, and comments in gray. Requires `useColors` to be `true` and `terminal` to be `true`.
801+
**Default:** `true`.
750802
* `handleError` {Function} This function customizes error handling in the REPL.
751803
It receives the thrown exception as its first argument and must return one
752804
of the following values synchronously:

0 commit comments

Comments
 (0)