command predicates: added support for strict matching by appending '!' on string predicates
This commit is contained in:
parent
0a66163c10
commit
4d3e8d8bd0
|
@ -11,21 +11,32 @@ local always_true = function() return true end
|
|||
---evaluation.
|
||||
---
|
||||
---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
|
||||
---sames applies if a table is given. A function that returns a boolean can be
|
||||
---used instead to perform a custom evaluation, setting to nil means always
|
||||
---evaluates to true.
|
||||
---return a valid object which is checked against the current active view,
|
||||
---eg: "core.docview" will match any view that inherits from DocView. Appending
|
||||
---a `!` at the end of the string means we want to match the given object
|
||||
---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
|
||||
---@return function
|
||||
function command.generate_predicate(predicate)
|
||||
predicate = predicate or always_true
|
||||
local strict = false
|
||||
if type(predicate) == "string" then
|
||||
if predicate:match("!$") then
|
||||
strict = true
|
||||
predicate = predicate:gsub("!$", "")
|
||||
end
|
||||
predicate = require(predicate)
|
||||
end
|
||||
if type(predicate) == "table" then
|
||||
local class = predicate
|
||||
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
|
||||
return predicate
|
||||
end
|
||||
|
|
|
@ -21,10 +21,7 @@ end
|
|||
|
||||
|
||||
function Object:is(T)
|
||||
if getmetatable(self) == T then
|
||||
return true
|
||||
end
|
||||
return false
|
||||
return getmetatable(self) == T
|
||||
end
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue