Skip to content

Commit 24f4a8f

Browse files
authored
feat: add syntax highlighting in the REPL
PR-URL: #2254 Resolves: #2072 --------- Signed-off-by: Snehil Shah <[email protected]> Reviewed-by: Athan Reines <[email protected]> Reviewed-by: Philipp Burckhardt <[email protected]>
1 parent d3215eb commit 24f4a8f

File tree

8 files changed

+834
-177
lines changed

8 files changed

+834
-177
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MAIN //
22+
23+
/**
24+
* Table mapping generic color names to their ANSI color codes.
25+
*
26+
* @private
27+
* @name ANSI
28+
* @type {Object}
29+
*/
30+
var ANSI = {
31+
// Original colors:
32+
'black': '\u001b[30m',
33+
'red': '\u001b[31m',
34+
'green': '\u001b[32m',
35+
'yellow': '\u001b[33m',
36+
'blue': '\u001b[34m',
37+
'magenta': '\u001b[35m',
38+
'cyan': '\u001b[36m',
39+
'white': '\u001b[37m',
40+
41+
// Bright colors:
42+
'brightBlack': '\u001b[90m',
43+
'brightRed': '\u001b[91m',
44+
'brightGreen': '\u001b[92m',
45+
'brightYellow': '\u001b[93m',
46+
'brightBlue': '\u001b[94m',
47+
'brightMagenta': '\u001b[95m',
48+
'brightCyan': '\u001b[96m',
49+
'brightWhite': '\u001b[97m',
50+
51+
// Reset colors:
52+
'reset': '\u001b[0m'
53+
};
54+
55+
56+
// EXPORTS //
57+
58+
module.exports = ANSI;

lib/node_modules/@stdlib/repl/lib/complete_expression.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ function complete( out, context, expression ) {
7474
ast = parse( expression, AOPTS );
7575

7676
debug( 'Resolving local scopes within the AST.' );
77-
ast = resolveLocalScopes( ast );
77+
resolveLocalScopes( ast );
7878

7979
// Get the last program top-level AST "node":
8080
debug( 'Number of statements: %d', ast.body.length );

lib/node_modules/@stdlib/repl/lib/main.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ var processLine = require( './process_line.js' );
6363
var completerFactory = require( './completer.js' );
6464
var PreviewCompleter = require( './completer_preview.js' );
6565
var AutoCloser = require( './auto_close_pairs.js' );
66+
var SyntaxHighlighter = require( './syntax_highlighter.js' );
6667
var ALIAS_OVERRIDES = require( './alias_overrides.js' );
6768
var SETTINGS = require( './settings.js' );
6869
var SETTINGS_VALIDATORS = require( './settings_validators.js' );
@@ -270,6 +271,9 @@ function REPL( options ) {
270271
// Initialize a preview completer:
271272
setNonEnumerableReadOnly( this, '_previewCompleter', new PreviewCompleter( this._rli, this._completer, this._ostream, this._settings.completionPreviews ) );
272273

274+
// Initialize a syntax-highlighter:
275+
setNonEnumerableReadOnly( this, '_syntaxHighlighter', new SyntaxHighlighter( this, this._ostream ) );
276+
273277
// Cache a reference to the private readline interface `ttyWrite` to allow calling the method when wanting default behavior:
274278
setNonEnumerableReadOnly( this, '_ttyWrite', this._rli._ttyWrite );
275279

@@ -334,6 +338,7 @@ function REPL( options ) {
334338
*/
335339
function onKeypress( data, key ) {
336340
var autoClosed;
341+
337342
if ( key && key.name === 'tab' ) {
338343
return;
339344
}
@@ -343,6 +348,9 @@ function REPL( options ) {
343348
if ( autoClosed ) {
344349
self._previewCompleter.clear();
345350
}
351+
if ( self._isTTY ) {
352+
self._syntaxHighlighter.onKeypress();
353+
}
346354
self._previewCompleter.onKeypress( data, key );
347355
}
348356

lib/node_modules/@stdlib/repl/lib/resolve_globals.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ function resolveGlobals( ast ) {
7676
globals = [];
7777

7878
// Resolve local scopes:
79-
ast = resolveLocalScopes( ast );
79+
resolveLocalScopes( ast );
8080

8181
// Define callbacks for relevant AST nodes:
8282
visitors = {

0 commit comments

Comments
 (0)