vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
commit 995435dcbf57c236f9adb8b5483be1d93886ea6c parent 7ab142f0d9c80b257b8987ed8befb19d01cab51e Author: Florian Fischer <florian.fischer@muhq.space> Date: Thu, 7 Sep 2023 00:06:51 +0200 lua: cache loaded lexers Caching lexers causes lexer tables to be constructed once and reused during each HIGHLIGHT event. Additionally it allows to modify the lexer used for syntax highlighting from Lua code. This is used for example for the syntax aware spellchecking performed by the vis-spellcheck plugin. Diffstat:
| M | lua/vis.lua | | | 14 | ++++++++++++++ |
1 file changed, 14 insertions(+), 0 deletions(-)
diff --git a/lua/vis.lua b/lua/vis.lua @@ -121,6 +121,20 @@ elseif not vis:module_exist('lexer') then vis:info('WARNING: could not find lexer module') else vis.lexers = require('lexer') + + --- Cache of loaded lexers + -- + -- Caching lexers causes lexer tables to be constructed once and reused + -- during each HIGHLIGHT event. Additionally it allows to modify the lexer + -- used for syntax highlighting from Lua code. + local lexers = {} + local load_lexer = vis.lexers.load + vis.lexers.load = function (name, alt_name, cache) + if cache and lexers[alt_name or name] then return lexers[alt_name or name] end + local lexer = load_lexer(name, alt_name) + if cache then lexers[alt_name or name] = lexer end + return lexer + end vis.lpeg = require('lpeg') end