Skip to content

Commit e538d43

Browse files
authored
fix: resolve clash between syntax-highlighter and auto-closer
PR-URL: #2290 Reviewed-by: Athan Reines <kgryte@gmail.com> Signed-off-by: Snehil Shah <snehilshah.989@gmail.com>
1 parent 3347951 commit e538d43

File tree

1 file changed

+26
-14
lines changed

1 file changed

+26
-14
lines changed

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

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ function SyntaxHighlighter( repl, ostream ) {
7979
// Initialize a buffer containing the current line to validate line changes:
8080
this._line = '';
8181

82+
// Initialize a buffer to cache the highlighted line:
83+
this._highlightedLine = '';
84+
8285
return this;
8386
}
8487

@@ -136,24 +139,33 @@ setNonEnumerableReadOnly( SyntaxHighlighter.prototype, 'onKeypress', function on
136139
var highlightedLine;
137140
var tokens;
138141

139-
if ( !this._rli.line || this._line === this._rli.line ) {
140-
debug( 'Empty line or no change detected. Skipping highlighting...' );
141-
return;
142-
}
143-
this._line = this._rli.line;
144-
145-
// Tokenize:
146-
debug( 'Tokenizing line: %s', this._line );
147-
tokens = tokenizer( this._line, this._repl._context );
148-
if ( !tokens ) {
149-
debug( 'No tokens found. Skipping highlighting...' );
142+
if ( !this._rli.line ) {
143+
debug( 'Empty line detected. Skipping highlighting...' );
150144
return;
151145
}
152146

153-
// Highlight:
154-
debug( '%d tokens found. Highlighting...', tokens.length );
155-
highlightedLine = this._highlightLine( this._line, tokens );
147+
// If no line change is detected, use the highlighted line from cache...
148+
if ( this._line === this._rli.line ) {
149+
debug( 'No line change detected. Using cache...' );
150+
highlightedLine = this._highlightedLine;
151+
} else {
152+
// Update line buffer:
153+
this._line = this._rli.line;
154+
155+
// Tokenize:
156+
debug( 'Line change detected. Tokenizing line: %s', this._line );
157+
tokens = tokenizer( this._line, this._repl._context );
158+
if ( !tokens ) {
159+
debug( 'No tokens found. Skipping highlighting...' );
160+
return;
161+
}
162+
// Highlight:
163+
debug( '%d tokens found. Highlighting...', tokens.length );
164+
highlightedLine = this._highlightLine( this._line, tokens );
156165

166+
// Cache the newly highlighted line:
167+
this._highlightedLine = highlightedLine;
168+
}
157169
// Replace:
158170
debug( 'Replacing current line with the highlighted line...' );
159171
readline.moveCursor( this._ostream, -1 * this._rli.cursor, 0 );

0 commit comments

Comments
 (0)