From 1838bd2b567af8a89e692096476b2aa0801e7bef Mon Sep 17 00:00:00 2001 From: Snehil Shah Date: Sat, 1 Jun 2024 05:05:39 +0000 Subject: [PATCH] fix: resolve clashes with auto-closer Signed-off-by: Snehil Shah --- .../@stdlib/repl/lib/syntax_highlighter.js | 40 ++++++++++++------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/lib/node_modules/@stdlib/repl/lib/syntax_highlighter.js b/lib/node_modules/@stdlib/repl/lib/syntax_highlighter.js index 4cba7313d554..0fb92b3de858 100644 --- a/lib/node_modules/@stdlib/repl/lib/syntax_highlighter.js +++ b/lib/node_modules/@stdlib/repl/lib/syntax_highlighter.js @@ -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; } @@ -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 );