Skip to content

Commit 63831d5

Browse files
committed
refactor(actions): move on_keypress to dispatch module
1 parent b81ab19 commit 63831d5

File tree

4 files changed

+104
-93
lines changed

4 files changed

+104
-93
lines changed

lua/nvim-tree.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ function M.change_root(filepath, bufnr)
6969
end
7070

7171
---@deprecated
72-
M.on_keypress = require("nvim-tree.actions").on_keypress
72+
M.on_keypress = require("nvim-tree.actions.dispatch").dispatch
7373

7474
function M.toggle(find_file, no_focus, cwd)
7575
if view.is_visible() then

lua/nvim-tree/actions/dispatch.lua

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
local view = require "nvim-tree.view"
2+
local lib = require "nvim-tree.lib"
3+
4+
local M = {}
5+
6+
local keypress_funcs = {
7+
close = view.close,
8+
close_node = require("nvim-tree.actions.movements").parent_node(true),
9+
collapse_all = require("nvim-tree.actions.collapse-all").fn,
10+
expand_all = require("nvim-tree.actions.expand-all").fn,
11+
copy_absolute_path = require("nvim-tree.actions.copy-paste").copy_absolute_path,
12+
copy_name = require("nvim-tree.actions.copy-paste").copy_filename,
13+
copy_path = require("nvim-tree.actions.copy-paste").copy_path,
14+
copy = require("nvim-tree.actions.copy-paste").copy,
15+
create = require("nvim-tree.actions.create-file").fn,
16+
cut = require("nvim-tree.actions.copy-paste").cut,
17+
dir_up = require("nvim-tree.actions.dir-up").fn,
18+
first_sibling = require("nvim-tree.actions.movements").sibling(-math.huge),
19+
full_rename = require("nvim-tree.actions.rename-file").fn(true),
20+
last_sibling = require("nvim-tree.actions.movements").sibling(math.huge),
21+
live_filter = require("nvim-tree.live-filter").start_filtering,
22+
clear_live_filter = require("nvim-tree.live-filter").clear_filter,
23+
next_diag_item = require("nvim-tree.actions.movements").find_item("next", "diag"),
24+
next_git_item = require("nvim-tree.actions.movements").find_item("next", "git"),
25+
next_sibling = require("nvim-tree.actions.movements").sibling(1),
26+
parent_node = require("nvim-tree.actions.movements").parent_node(false),
27+
paste = require("nvim-tree.actions.copy-paste").paste,
28+
prev_diag_item = require("nvim-tree.actions.movements").find_item("prev", "diag"),
29+
prev_git_item = require("nvim-tree.actions.movements").find_item("prev", "git"),
30+
prev_sibling = require("nvim-tree.actions.movements").sibling(-1),
31+
refresh = require("nvim-tree.actions.reloaders").reload_explorer,
32+
remove = require("nvim-tree.actions.remove-file").fn,
33+
rename = require("nvim-tree.actions.rename-file").fn(false),
34+
run_file_command = require("nvim-tree.actions.run-command").run_file_command,
35+
search_node = require("nvim-tree.actions.search-node").fn,
36+
toggle_file_info = require("nvim-tree.actions.file-popup").toggle_file_info,
37+
system_open = require("nvim-tree.actions.system-open").fn,
38+
toggle_dotfiles = require("nvim-tree.actions.toggles").dotfiles,
39+
toggle_help = require("nvim-tree.actions.toggles").help,
40+
toggle_custom = require("nvim-tree.actions.toggles").custom,
41+
toggle_git_ignored = require("nvim-tree.actions.toggles").git_ignored,
42+
trash = require("nvim-tree.actions.trash").fn,
43+
}
44+
45+
function M.dispatch(action)
46+
if view.is_help_ui() and action == "close" then
47+
action = "toggle_help"
48+
end
49+
if view.is_help_ui() and action ~= "toggle_help" then
50+
return
51+
end
52+
53+
if action == "live_filter" or action == "clear_live_filter" then
54+
return keypress_funcs[action]()
55+
end
56+
57+
local node = lib.get_node_at_cursor()
58+
if not node then
59+
return
60+
end
61+
62+
local custom_function = M.custom_keypress_funcs[action]
63+
local default_function = keypress_funcs[action]
64+
65+
if type(custom_function) == "function" then
66+
return custom_function(node)
67+
elseif default_function then
68+
return default_function(node)
69+
end
70+
71+
if action == "preview" then
72+
if node.name == ".." then
73+
return
74+
end
75+
if not node.nodes then
76+
return require("nvim-tree.actions.open-file").fn("preview", node.absolute_path)
77+
end
78+
elseif node.name == ".." then
79+
return require("nvim-tree.actions.change-dir").fn ".."
80+
elseif action == "cd" then
81+
if node.nodes ~= nil then
82+
require("nvim-tree.actions.change-dir").fn(lib.get_last_group_node(node).absolute_path)
83+
end
84+
return
85+
end
86+
87+
if node.link_to and not node.nodes then
88+
require("nvim-tree.actions.open-file").fn(action, node.link_to)
89+
elseif node.nodes ~= nil then
90+
lib.expand_or_collapse(node)
91+
else
92+
require("nvim-tree.actions.open-file").fn(action, node.absolute_path)
93+
end
94+
end
95+
96+
function M.setup(custom_keypress_funcs)
97+
M.custom_keypress_funcs = custom_keypress_funcs
98+
end
99+
100+
return M

lua/nvim-tree/actions/init.lua

Lines changed: 2 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
local a = vim.api
22

3-
local lib = require "nvim-tree.lib"
43
local log = require "nvim-tree.log"
54
local view = require "nvim-tree.view"
65
local util = require "nvim-tree.utils"
@@ -236,96 +235,6 @@ local M = {
236235
custom_keypress_funcs = {},
237236
}
238237

239-
local keypress_funcs = {
240-
close = view.close,
241-
close_node = require("nvim-tree.actions.movements").parent_node(true),
242-
collapse_all = require("nvim-tree.actions.collapse-all").fn,
243-
expand_all = require("nvim-tree.actions.expand-all").fn,
244-
copy_absolute_path = require("nvim-tree.actions.copy-paste").copy_absolute_path,
245-
copy_name = require("nvim-tree.actions.copy-paste").copy_filename,
246-
copy_path = require("nvim-tree.actions.copy-paste").copy_path,
247-
copy = require("nvim-tree.actions.copy-paste").copy,
248-
create = require("nvim-tree.actions.create-file").fn,
249-
cut = require("nvim-tree.actions.copy-paste").cut,
250-
dir_up = require("nvim-tree.actions.dir-up").fn,
251-
first_sibling = require("nvim-tree.actions.movements").sibling(-math.huge),
252-
full_rename = require("nvim-tree.actions.rename-file").fn(true),
253-
last_sibling = require("nvim-tree.actions.movements").sibling(math.huge),
254-
live_filter = require("nvim-tree.live-filter").start_filtering,
255-
clear_live_filter = require("nvim-tree.live-filter").clear_filter,
256-
next_diag_item = require("nvim-tree.actions.movements").find_item("next", "diag"),
257-
next_git_item = require("nvim-tree.actions.movements").find_item("next", "git"),
258-
next_sibling = require("nvim-tree.actions.movements").sibling(1),
259-
parent_node = require("nvim-tree.actions.movements").parent_node(false),
260-
paste = require("nvim-tree.actions.copy-paste").paste,
261-
prev_diag_item = require("nvim-tree.actions.movements").find_item("prev", "diag"),
262-
prev_git_item = require("nvim-tree.actions.movements").find_item("prev", "git"),
263-
prev_sibling = require("nvim-tree.actions.movements").sibling(-1),
264-
refresh = require("nvim-tree.actions.reloaders").reload_explorer,
265-
remove = require("nvim-tree.actions.remove-file").fn,
266-
rename = require("nvim-tree.actions.rename-file").fn(false),
267-
run_file_command = require("nvim-tree.actions.run-command").run_file_command,
268-
search_node = require("nvim-tree.actions.search-node").fn,
269-
toggle_file_info = require("nvim-tree.actions.file-popup").toggle_file_info,
270-
system_open = require("nvim-tree.actions.system-open").fn,
271-
toggle_dotfiles = require("nvim-tree.actions.toggles").dotfiles,
272-
toggle_help = require("nvim-tree.actions.toggles").help,
273-
toggle_custom = require("nvim-tree.actions.toggles").custom,
274-
toggle_git_ignored = require("nvim-tree.actions.toggles").git_ignored,
275-
trash = require("nvim-tree.actions.trash").fn,
276-
}
277-
278-
function M.on_keypress(action)
279-
if view.is_help_ui() and action == "close" then
280-
action = "toggle_help"
281-
end
282-
if view.is_help_ui() and action ~= "toggle_help" then
283-
return
284-
end
285-
286-
if action == "live_filter" or action == "clear_live_filter" then
287-
return keypress_funcs[action]()
288-
end
289-
290-
local node = lib.get_node_at_cursor()
291-
if not node then
292-
return
293-
end
294-
295-
local custom_function = M.custom_keypress_funcs[action]
296-
local default_function = keypress_funcs[action]
297-
298-
if type(custom_function) == "function" then
299-
return custom_function(node)
300-
elseif default_function then
301-
return default_function(node)
302-
end
303-
304-
if action == "preview" then
305-
if node.name == ".." then
306-
return
307-
end
308-
if not node.nodes then
309-
return require("nvim-tree.actions.open-file").fn("preview", node.absolute_path)
310-
end
311-
elseif node.name == ".." then
312-
return require("nvim-tree.actions.change-dir").fn ".."
313-
elseif action == "cd" then
314-
if node.nodes ~= nil then
315-
require("nvim-tree.actions.change-dir").fn(lib.get_last_group_node(node).absolute_path)
316-
end
317-
return
318-
end
319-
320-
if node.link_to and not node.nodes then
321-
require("nvim-tree.actions.open-file").fn(action, node.link_to)
322-
elseif node.nodes ~= nil then
323-
lib.expand_or_collapse(node)
324-
else
325-
require("nvim-tree.actions.open-file").fn(action, node.absolute_path)
326-
end
327-
end
328-
329238
function M.apply_mappings(bufnr)
330239
for _, b in pairs(M.mappings) do
331240
local mapping_rhs = b.cb or nvim_tree_callback(b.action)
@@ -454,6 +363,8 @@ function M.setup(opts)
454363
M.mappings = merge_mappings(options.list)
455364
end
456365

366+
require("nvim-tree.actions.dispatch").setup(M.custom_keypress_funcs)
367+
457368
log.line("config", "active mappings")
458369
log.raw("config", "%s\n", vim.inspect(M.mappings))
459370
end

lua/nvim-tree/config.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ local M = {}
44

55
-- TODO: remove this once the cb property is not supported in mappings
66
function M.nvim_tree_callback(callback_name)
7-
return string.format(":lua require'nvim-tree.actions'.on_keypress('%s')<CR>", callback_name)
7+
return string.format(":lua require'nvim-tree.actions.dispatch'.dispatch('%s')<CR>", callback_name)
88
end
99

1010
return M

0 commit comments

Comments
 (0)