adjust and consolidate duplicated predicate code
This commit is contained in:
parent
fd0a433f59
commit
59d91087e9
|
@ -6,15 +6,33 @@ command.map = {}
|
||||||
local always_true = function() return true end
|
local always_true = function() return true end
|
||||||
|
|
||||||
|
|
||||||
function command.add(predicate, map)
|
---Used iternally by command.add, statusview, and contextmenu to generate a
|
||||||
|
---function with a condition to evaluate returning the boolean result of this
|
||||||
|
---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.
|
||||||
|
---
|
||||||
|
---@param predicate string | table | function
|
||||||
|
---@return function
|
||||||
|
function command.generate_predicate(predicate)
|
||||||
predicate = predicate or always_true
|
predicate = predicate or always_true
|
||||||
if type(predicate) == "string" then
|
if type(predicate) == "string" then
|
||||||
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:is(class) end
|
predicate = function() return core.active_view:extends(class) end
|
||||||
end
|
end
|
||||||
|
return predicate
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function command.add(predicate, map)
|
||||||
|
predicate = command.generate_predicate(predicate)
|
||||||
for name, fn in pairs(map) do
|
for name, fn in pairs(map) do
|
||||||
assert(not command.map[name], "command already exists: " .. name)
|
assert(not command.map[name], "command already exists: " .. name)
|
||||||
command.map[name] = { predicate = predicate, perform = fn }
|
command.map[name] = { predicate = predicate, perform = fn }
|
||||||
|
|
|
@ -39,14 +39,7 @@ local function get_item_size(item)
|
||||||
end
|
end
|
||||||
|
|
||||||
function ContextMenu:register(predicate, items)
|
function ContextMenu:register(predicate, items)
|
||||||
if type(predicate) == "string" then
|
predicate = command.generate_predicate(predicate)
|
||||||
predicate = require(predicate)
|
|
||||||
end
|
|
||||||
if type(predicate) == "table" then
|
|
||||||
local class = predicate
|
|
||||||
predicate = function() return core.active_view:is(class) end
|
|
||||||
end
|
|
||||||
|
|
||||||
local width, height = 0, 0 --precalculate the size of context menu
|
local width, height = 0, 0 --precalculate the size of context menu
|
||||||
for i, item in ipairs(items) do
|
for i, item in ipairs(items) do
|
||||||
if item ~= DIVIDER then
|
if item ~= DIVIDER then
|
||||||
|
|
|
@ -113,9 +113,6 @@ function StatusView.Item:hide() self.visible = false end
|
||||||
---Show the item on the status bar.
|
---Show the item on the status bar.
|
||||||
function StatusView.Item:show() self.visible = true end
|
function StatusView.Item:show() self.visible = true end
|
||||||
|
|
||||||
---Function assiged by default when user provides a nil predicate.
|
|
||||||
local function predicate_always_true() return true end
|
|
||||||
|
|
||||||
---A condition to evaluate if the item should be displayed. If a string
|
---A condition to evaluate if the item should be displayed. If a string
|
||||||
---is given it is treated as a require import that should return a valid object
|
---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
|
---which is checked against the current active view, the sames applies if a
|
||||||
|
@ -123,15 +120,7 @@ local function predicate_always_true() return true end
|
||||||
---perform a custom evaluation, setting to nil means always evaluates to true.
|
---perform a custom evaluation, setting to nil means always evaluates to true.
|
||||||
---@param predicate string | table | StatusView.Item.predicate
|
---@param predicate string | table | StatusView.Item.predicate
|
||||||
function StatusView.Item:set_predicate(predicate)
|
function StatusView.Item:set_predicate(predicate)
|
||||||
predicate = predicate or predicate_always_true
|
self.predicate = command.generate_predicate(predicate)
|
||||||
if type(predicate) == "string" then
|
|
||||||
predicate = require(predicate)
|
|
||||||
end
|
|
||||||
if type(predicate) == "table" then
|
|
||||||
local class = predicate
|
|
||||||
predicate = function() return core.active_view:is(class) end
|
|
||||||
end
|
|
||||||
self.predicate = predicate
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
@ -655,14 +644,14 @@ local function merge_deprecated_items(destination, items, alignment)
|
||||||
local position = alignment == StatusView.Item.LEFT and "left" or "right"
|
local position = alignment == StatusView.Item.LEFT and "left" or "right"
|
||||||
|
|
||||||
local item_start = StatusView.Item(
|
local item_start = StatusView.Item(
|
||||||
predicate_always_true,
|
nil,
|
||||||
"deprecated:"..position.."-start",
|
"deprecated:"..position.."-start",
|
||||||
alignment
|
alignment
|
||||||
)
|
)
|
||||||
item_start.get_item = items_start
|
item_start.get_item = items_start
|
||||||
|
|
||||||
local item_end = StatusView.Item(
|
local item_end = StatusView.Item(
|
||||||
predicate_always_true,
|
nil,
|
||||||
"deprecated:"..position.."-end",
|
"deprecated:"..position.."-end",
|
||||||
alignment
|
alignment
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue