Skip to content

fix: resolve clashes between syntax-highlighter & auto-closer in the REPL #2290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 26 additions & 14 deletions lib/node_modules/@stdlib/repl/lib/syntax_highlighter.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ function SyntaxHighlighter( repl, ostream ) {
// Initialize a buffer containing the current line to validate line changes:
this._line = '';

// Initialize a buffer to cache the highlighted line:
this._highlightedLine = '';

return this;
}

Expand Down Expand Up @@ -136,24 +139,33 @@ setNonEnumerableReadOnly( SyntaxHighlighter.prototype, 'onKeypress', function on
var highlightedLine;
var tokens;

if ( !this._rli.line || this._line === this._rli.line ) {
debug( 'Empty line or no change detected. Skipping highlighting...' );
return;
}
this._line = this._rli.line;

// Tokenize:
debug( 'Tokenizing line: %s', this._line );
tokens = tokenizer( this._line, this._repl._context );
if ( !tokens ) {
debug( 'No tokens found. Skipping highlighting...' );
if ( !this._rli.line ) {
debug( 'Empty line detected. Skipping highlighting...' );
return;
}

// Highlight:
debug( '%d tokens found. Highlighting...', tokens.length );
highlightedLine = this._highlightLine( this._line, tokens );
// If no line change is detected, use the highlighted line from cache...
if ( this._line === this._rli.line ) {
debug( 'No line change detected. Using cache...' );
highlightedLine = this._highlightedLine;
} else {
// Update line buffer:
this._line = this._rli.line;

// Tokenize:
debug( 'Line change detected. Tokenizing line: %s', this._line );
tokens = tokenizer( this._line, this._repl._context );
if ( !tokens ) {
debug( 'No tokens found. Skipping highlighting...' );
return;
}
// Highlight:
debug( '%d tokens found. Highlighting...', tokens.length );
highlightedLine = this._highlightLine( this._line, tokens );

// Cache the newly highlighted line:
this._highlightedLine = highlightedLine;
}
// Replace:
debug( 'Replacing current line with the highlighted line...' );
readline.moveCursor( this._ostream, -1 * this._rli.cursor, 0 );
Expand Down
Loading