diff options
| author | Georgi Kirilov <in.the@repo> | 2020-02-17 14:42:24 +0200 |
|---|---|---|
| committer | Marc André Tanner <mat@brain-dump.org> | 2020-02-24 11:10:55 +0100 |
| commit | 15d213e4b6e33670cb50d472ad3f532245ebcc3b (patch) | |
| tree | 166e249426a946beb3b64b3653e99cde6f651153 /lua/vis-std.lua | |
| parent | f63f26aac0be25b36959112af412773561daab5e (diff) | |
| download | vis-15d213e4b6e33670cb50d472ad3f532245ebcc3b.tar.gz vis-15d213e4b6e33670cb50d472ad3f532245ebcc3b.tar.xz | |
lua: add `redrawtime` option
Upper bound lexing time and cancel highlighting if it is exceeded.
Diffstat (limited to 'lua/vis-std.lua')
| -rw-r--r-- | lua/vis-std.lua | 16 |
1 files changed, 15 insertions, 1 deletions
diff --git a/lua/vis-std.lua b/lua/vis-std.lua index 31439c2..470872f 100644 --- a/lua/vis-std.lua +++ b/lua/vis-std.lua @@ -37,6 +37,17 @@ vis:option_register("horizon", "number", function(horizon) return true end, "Number of bytes to consider for syntax highlighting") +vis:option_register("redrawtime", "string", function(redrawtime) + if not vis.win then return false end + local value = tonumber(redrawtime) + if not value or value <= 0 then + vis:info("A positive real number expected") + return false + end + vis.win.redrawtime = value + return true +end, "Seconds to wait for syntax highlighting before aborting it") + vis.events.subscribe(vis.events.WIN_HIGHLIGHT, function(win) if not win.syntax or not vis.lexers.load then return end local lexer = vis.lexers.load(win.syntax, nil, true) @@ -45,6 +56,7 @@ vis.events.subscribe(vis.events.WIN_HIGHLIGHT, function(win) -- TODO: improve heuristic for initial style local viewport = win.viewport if not viewport then return end + local redrawtime_max = win.redrawtime or 1.0 local horizon_max = win.horizon or 32768 local horizon = viewport.start < horizon_max and viewport.start or horizon_max local view_start = viewport.start @@ -52,9 +64,11 @@ vis.events.subscribe(vis.events.WIN_HIGHLIGHT, function(win) viewport.start = lex_start local data = win.file:content(viewport) local token_styles = lexer._TOKENSTYLES - local tokens = lexer:lex(data, 1) + local tokens, timedout = lexer:lex(data, 1, redrawtime_max) local token_end = lex_start + (tokens[#tokens] or 1) - 1 + if timedout then return 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 |
