vis

a vi-like editor based on Plan 9's structural regular expressions

git clone https://9o.is/git/vis.git

commit 1fd71ceb6d9d7bf9c7db3547eb3c1a956d81cb38
parent 7807fa6b16b1c5675f0936a3780f2327fab66c53
Author: Michiel van den Heuvel <michielvdnheuvel@gmail.com>
Date:   Tue, 28 May 2024 12:49:55 +0200

Fail silently when syntax has no lexer

This'll patch vis.lexers.load to return nil when the lexer could not be
found. Previously it would've errored out, which the load in lexer.lua
still will as this is used in lexers themselves.

Another possibility is to only patch set_syntax in vis.lua and the
WIN_HIGHLIGHT handler in vis-std.lua, but as most references to
vis.lexers.load already handle a nil return, this seems better.

Diffstat:
Mlua/plugins/filetype.lua | 10+---------
Mlua/plugins/textobject-lexer.lua | 6+++++-
Mlua/vis.lua | 5+++--
3 files changed, 9 insertions(+), 12 deletions(-)

diff --git a/lua/plugins/filetype.lua b/lua/plugins/filetype.lua @@ -506,15 +506,7 @@ vis.events.subscribe(vis.events.WIN_OPEN, function(win) for _, cmd in pairs(filetype.cmd or {}) do vis:command(cmd) end - if not vis.lexers.property then return end - local path = vis.lexers.property['scintillua.lexers']:gsub(';', '/?.lua;') - local lexname = filetype.alt_name or syntax - local lexpath = package.searchpath(lexname, path) - if lexpath ~= nil then - win:set_syntax(lexname) - else - win:set_syntax(nil) - end + win:set_syntax(filetype.alt_name or syntax) end local path = win.file.name -- filepath diff --git a/lua/plugins/textobject-lexer.lua b/lua/plugins/textobject-lexer.lua @@ -8,13 +8,17 @@ vis:textobject_new("ii", function(win, pos) return nil end + local lexer = vis.lexers.load(win.syntax, nil, true) + if not lexer then + return nil + end + local before, after = pos - MAX_CONTEXT, pos + MAX_CONTEXT if before < 0 then before = 0 end -- TODO make sure we start at a line boundary? - local lexer = vis.lexers.load(win.syntax, nil, true) local data = win.file:content(before, after - before) local tokens = lexer:lex(data) local cur = before diff --git a/lua/vis.lua b/lua/vis.lua @@ -131,7 +131,8 @@ else 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) + local status, lexer = pcall(load_lexer, name, alt_name) + if not status then return nil end if cache then lexers[alt_name or name] = lexer end return lexer end @@ -276,6 +277,7 @@ vis.types.window.set_syntax = function(win, syntax) win.syntax = nil return true end + win.syntax = syntax if not lexers.load then return false end local lexer = lexers.load(syntax) @@ -297,7 +299,6 @@ vis.types.window.set_syntax = function(win, syntax) if style ~= nil then win:style_define(id, style) end end - win.syntax = syntax return true end