Skip to content

Commit

Permalink
Auto-generated commit
Browse files Browse the repository at this point in the history
  • Loading branch information
stdlib-bot committed Jun 1, 2024
1 parent 657ceae commit eb79a4d
Showing 1 changed file with 26 additions and 14 deletions.
40 changes: 26 additions & 14 deletions 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

0 comments on commit eb79a4d

Please sign in to comment.