command predicates: added support for strict matching by appending '!' on string predicates

This commit is contained in:
jgmdev 2022-05-15 16:10:57 -04:00
parent 0a66163c10
commit 4d3e8d8bd0
2 changed files with 17 additions and 9 deletions

View File

@ -11,21 +11,32 @@ local always_true = function() return true end
---evaluation. ---evaluation.
--- ---
---If a string predicate is given it is treated as a require import that should ---If a string predicate is given it is treated as a require import that should
---return a valid object which is checked against the current active view, the ---return a valid object which is checked against the current active view,
---sames applies if a table is given. A function that returns a boolean can be ---eg: "core.docview" will match any view that inherits from DocView. Appending
---used instead to perform a custom evaluation, setting to nil means always ---a `!` at the end of the string means we want to match the given object
---evaluates to true. ---from the import strcitly eg: "core.docview!" only DocView is matched.
---A function that returns a boolean can be used instead to perform a custom
---evaluation, setting to nil means always evaluates to true.
--- ---
---@param predicate string | table | function ---@param predicate string | table | function
---@return function ---@return function
function command.generate_predicate(predicate) function command.generate_predicate(predicate)
predicate = predicate or always_true predicate = predicate or always_true
local strict = false
if type(predicate) == "string" then if type(predicate) == "string" then
if predicate:match("!$") then
strict = true
predicate = predicate:gsub("!$", "")
end
predicate = require(predicate) predicate = require(predicate)
end end
if type(predicate) == "table" then if type(predicate) == "table" then
local class = predicate local class = predicate
predicate = function() return core.active_view:extends(class) end if not strict then
predicate = function() return core.active_view:extends(class) end
else
predicate = function() return core.active_view:is(class) end
end
end end
return predicate return predicate
end end

View File

@ -21,10 +21,7 @@ end
function Object:is(T) function Object:is(T)
if getmetatable(self) == T then return getmetatable(self) == T
return true
end
return false
end end