Skip to content

Commit

Permalink
feat: add syntax highlighting in the REPL
Browse files Browse the repository at this point in the history
PR-URL: stdlib-js#2254
Resolves: stdlib-js#2072

---------

Signed-off-by: Snehil Shah <snehilshah.989@gmail.com>
Reviewed-by: Athan Reines <kgryte@gmail.com> 
Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com>
  • Loading branch information
Snehil-Shah authored and aman-095 committed Jun 13, 2024
1 parent b6bfdb8 commit 94ed852
Show file tree
Hide file tree
Showing 8 changed files with 834 additions and 177 deletions.
58 changes: 58 additions & 0 deletions lib/node_modules/@stdlib/repl/lib/ansi_colors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MAIN //

/**
* Table mapping generic color names to their ANSI color codes.
*
* @private
* @name ANSI
* @type {Object}
*/
var ANSI = {
// Original colors:
'black': '\u001b[30m',
'red': '\u001b[31m',
'green': '\u001b[32m',
'yellow': '\u001b[33m',
'blue': '\u001b[34m',
'magenta': '\u001b[35m',
'cyan': '\u001b[36m',
'white': '\u001b[37m',

// Bright colors:
'brightBlack': '\u001b[90m',
'brightRed': '\u001b[91m',
'brightGreen': '\u001b[92m',
'brightYellow': '\u001b[93m',
'brightBlue': '\u001b[94m',
'brightMagenta': '\u001b[95m',
'brightCyan': '\u001b[96m',
'brightWhite': '\u001b[97m',

// Reset colors:
'reset': '\u001b[0m'
};


// EXPORTS //

module.exports = ANSI;
2 changes: 1 addition & 1 deletion lib/node_modules/@stdlib/repl/lib/complete_expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ function complete( out, context, expression ) {
ast = parse( expression, AOPTS );

debug( 'Resolving local scopes within the AST.' );
ast = resolveLocalScopes( ast );
resolveLocalScopes( ast );

// Get the last program top-level AST "node":
debug( 'Number of statements: %d', ast.body.length );
Expand Down
8 changes: 8 additions & 0 deletions lib/node_modules/@stdlib/repl/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ var processLine = require( './process_line.js' );
var completerFactory = require( './completer.js' );
var PreviewCompleter = require( './completer_preview.js' );
var AutoCloser = require( './auto_close_pairs.js' );
var SyntaxHighlighter = require( './syntax_highlighter.js' );
var ALIAS_OVERRIDES = require( './alias_overrides.js' );
var SETTINGS = require( './settings.js' );
var SETTINGS_VALIDATORS = require( './settings_validators.js' );
Expand Down Expand Up @@ -270,6 +271,9 @@ function REPL( options ) {
// Initialize a preview completer:
setNonEnumerableReadOnly( this, '_previewCompleter', new PreviewCompleter( this._rli, this._completer, this._ostream, this._settings.completionPreviews ) );

// Initialize a syntax-highlighter:
setNonEnumerableReadOnly( this, '_syntaxHighlighter', new SyntaxHighlighter( this, this._ostream ) );

// Cache a reference to the private readline interface `ttyWrite` to allow calling the method when wanting default behavior:
setNonEnumerableReadOnly( this, '_ttyWrite', this._rli._ttyWrite );

Expand Down Expand Up @@ -334,6 +338,7 @@ function REPL( options ) {
*/
function onKeypress( data, key ) {
var autoClosed;

if ( key && key.name === 'tab' ) {
return;
}
Expand All @@ -343,6 +348,9 @@ function REPL( options ) {
if ( autoClosed ) {
self._previewCompleter.clear();
}
if ( self._isTTY ) {
self._syntaxHighlighter.onKeypress();
}
self._previewCompleter.onKeypress( data, key );
}

Expand Down
2 changes: 1 addition & 1 deletion lib/node_modules/@stdlib/repl/lib/resolve_globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ function resolveGlobals( ast ) {
globals = [];

// Resolve local scopes:
ast = resolveLocalScopes( ast );
resolveLocalScopes( ast );

// Define callbacks for relevant AST nodes:
visitors = {
Expand Down
Loading

0 comments on commit 94ed852

Please sign in to comment.