diff options
| author | Georgi Kirilov <in.the@repo> | 2020-02-13 20:58:09 +0200 |
|---|---|---|
| committer | Marc André Tanner <mat@brain-dump.org> | 2020-02-24 10:51:27 +0100 |
| commit | a8bb508770f36f0110d9b69219dff503b4adf2a8 (patch) | |
| tree | 58a432681152602c22f1585abf0bdc1e2474e53a /lua/vis-std.lua | |
| parent | c57e4e35c3fb7e234cf273f798a12810411fb6cf (diff) | |
| download | vis-a8bb508770f36f0110d9b69219dff503b4adf2a8.tar.gz vis-a8bb508770f36f0110d9b69219dff503b4adf2a8.tar.xz | |
lua: optimize the win:style() loop
barely noticeable CPU usage improvement, although the number of
iterations went down by an order of magnitude.
Diffstat (limited to 'lua/vis-std.lua')
| -rw-r--r-- | lua/vis-std.lua | 23 |
1 files changed, 12 insertions, 11 deletions
diff --git a/lua/vis-std.lua b/lua/vis-std.lua index 05bf832..31439c2 100644 --- a/lua/vis-std.lua +++ b/lua/vis-std.lua @@ -49,22 +49,23 @@ vis.events.subscribe(vis.events.WIN_HIGHLIGHT, function(win) local horizon = viewport.start < horizon_max and viewport.start or horizon_max local view_start = viewport.start local lex_start = viewport.start - horizon - local token_start = lex_start - viewport.start = token_start + viewport.start = lex_start local data = win.file:content(viewport) local token_styles = lexer._TOKENSTYLES local tokens = lexer:lex(data, 1) + local token_end = lex_start + (tokens[#tokens] or 1) - 1 - for i = 1, #tokens, 2 do - local token_end = lex_start + tokens[i+1] - 1 - if token_end >= view_start then - local name = tokens[i] - local style = token_styles[name] - if style ~= nil then - win:style(style, token_start, token_end) - end + for i = #tokens - 1, 1, -2 do + local token_start = lex_start + (tokens[i-1] or 1) - 1 + if token_end < view_start then + break end - token_start = token_end + local name = tokens[i] + local style = token_styles[name] + if style ~= nil then + win:style(style, token_start, token_end) + end + token_end = token_start - 1 end end) |
