@@ -79,6 +79,9 @@ function SyntaxHighlighter( repl, ostream ) {
79
79
// Initialize a buffer containing the current line to validate line changes:
80
80
this . _line = '' ;
81
81
82
+ // Initialize a buffer to cache the highlighted line:
83
+ this . _highlightedLine = '' ;
84
+
82
85
return this ;
83
86
}
84
87
@@ -136,24 +139,33 @@ setNonEnumerableReadOnly( SyntaxHighlighter.prototype, 'onKeypress', function on
136
139
var highlightedLine ;
137
140
var tokens ;
138
141
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...' ) ;
150
144
return ;
151
145
}
152
146
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 ) ;
156
165
166
+ // Cache the newly highlighted line:
167
+ this . _highlightedLine = highlightedLine ;
168
+ }
157
169
// Replace:
158
170
debug ( 'Replacing current line with the highlighted line...' ) ;
159
171
readline . moveCursor ( this . _ostream , - 1 * this . _rli . cursor , 0 ) ;
0 commit comments