Allow command predicates to manage parameters passed to the commands

When a command is performed with parameters, those are now passed to the
predicate.
The predicate can then return, after the validity boolean, any other
value that will then be passed to the actual command.
If the predicate only returns the validity boolean, the original
parameters are passed through to the actual command.

This allows predicates to manipulate the received parameters, and allows
them to pass the result of an expensive computation to the actual
command, which won't have to recalculate it.

String and table predicates will now also return `core.active_view`.
This commit is contained in:
Guldoman
2022-08-16 22:13:16 +02:00
parent 6ccc5f6dde
commit cf29a6a45f
13 changed files with 223 additions and 233 deletions
+17 -19
View File
@@ -679,16 +679,16 @@ local function treeitem() return view.hovered_item or view.selected_item end
command.add(
function()
return treeitem() ~= nil
local item = treeitem()
return item ~= nil
and (
core.active_view == view or core.active_view == menu
or (view.toolbar and core.active_view == view.toolbar)
-- sometimes the context menu is shown on top of statusbar
or core.active_view == core.status_view
)
), item
end, {
["treeview:delete"] = function()
local item = treeitem()
["treeview:delete"] = function(item)
local filename = item.abs_filename
local relfilename = item.filename
if item.dir_name ~= core.project_dir then
@@ -732,9 +732,11 @@ command.add(
})
command.add(function() return treeitem() ~= nil end, {
["treeview:rename"] = function()
command.add(function()
local item = treeitem()
return item ~= nil, item
end, {
["treeview:rename"] = function(item)
local old_filename = item.filename
local old_abs_filename = item.abs_filename
core.command_view:enter("Rename", {
@@ -764,9 +766,8 @@ command.add(function() return treeitem() ~= nil end, {
})
end,
["treeview:new-file"] = function()
["treeview:new-file"] = function(item)
local text
local item = treeitem()
if not is_project_folder(item.abs_filename) then
text = item.filename .. PATHSEP
end
@@ -787,9 +788,8 @@ command.add(function() return treeitem() ~= nil end, {
})
end,
["treeview:new-folder"] = function()
["treeview:new-folder"] = function(item)
local text
local item = treeitem()
if not is_project_folder(item.abs_filename) then
text = item.filename .. PATHSEP
end
@@ -806,15 +806,13 @@ command.add(function() return treeitem() ~= nil end, {
})
end,
["treeview:open-in-system"] = function()
local hovered_item = treeitem()
["treeview:open-in-system"] = function(item)
if PLATFORM == "Windows" then
system.exec(string.format("start \"\" %q", hovered_item.abs_filename))
system.exec(string.format("start \"\" %q", item.abs_filename))
elseif string.find(PLATFORM, "Mac") then
system.exec(string.format("open %q", hovered_item.abs_filename))
system.exec(string.format("open %q", item.abs_filename))
elseif PLATFORM == "Linux" or string.find(PLATFORM, "BSD") then
system.exec(string.format("xdg-open %q", hovered_item.abs_filename))
system.exec(string.format("xdg-open %q", item.abs_filename))
end
end
})
@@ -823,10 +821,10 @@ command.add(function()
local item = treeitem()
return item
and not is_primary_project_folder(item.abs_filename)
and is_project_folder(item.abs_filename)
and is_project_folder(item.abs_filename), item
end, {
["treeview:remove-project-directory"] = function()
core.remove_project_directory(treeitem().dir_name)
["treeview:remove-project-directory"] = function(item)
core.remove_project_directory(item.dir_name)
end,
})