Skip to content

Commit 1e7019f

Browse files
committed
refactor(dispatch): cleanup dispatch logic
1 parent 63831d5 commit 1e7019f

File tree

1 file changed

+49
-31
lines changed

1 file changed

+49
-31
lines changed

lua/nvim-tree/actions/dispatch.lua

Lines changed: 49 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ local lib = require "nvim-tree.lib"
33

44
local M = {}
55

6-
local keypress_funcs = {
6+
local Actions = {
77
close = view.close,
88
close_node = require("nvim-tree.actions.movements").parent_node(true),
99
collapse_all = require("nvim-tree.actions.collapse-all").fn,
@@ -18,8 +18,6 @@ local keypress_funcs = {
1818
first_sibling = require("nvim-tree.actions.movements").sibling(-math.huge),
1919
full_rename = require("nvim-tree.actions.rename-file").fn(true),
2020
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,
2321
next_diag_item = require("nvim-tree.actions.movements").find_item("next", "diag"),
2422
next_git_item = require("nvim-tree.actions.movements").find_item("next", "git"),
2523
next_sibling = require("nvim-tree.actions.movements").sibling(1),
@@ -36,60 +34,80 @@ local keypress_funcs = {
3634
toggle_file_info = require("nvim-tree.actions.file-popup").toggle_file_info,
3735
system_open = require("nvim-tree.actions.system-open").fn,
3836
toggle_dotfiles = require("nvim-tree.actions.toggles").dotfiles,
39-
toggle_help = require("nvim-tree.actions.toggles").help,
4037
toggle_custom = require("nvim-tree.actions.toggles").custom,
4138
toggle_git_ignored = require("nvim-tree.actions.toggles").git_ignored,
4239
trash = require("nvim-tree.actions.trash").fn,
4340
}
4441

45-
function M.dispatch(action)
46-
if view.is_help_ui() and action == "close" then
47-
action = "toggle_help"
42+
local function handle_action_on_help_ui(action)
43+
if action == "close" or action == "toggle_help" then
44+
require("nvim-tree.actions.toggles").help()
4845
end
49-
if view.is_help_ui() and action ~= "toggle_help" then
50-
return
46+
end
47+
48+
local function handle_filter_actions(action)
49+
if action == "live_filter" then
50+
require("nvim-tree.live-filter").start_filtering()
51+
elseif action == "clear_live_filter" then
52+
require("nvim-tree.live-filter").clear_filter()
5153
end
54+
end
5255

53-
if action == "live_filter" or action == "clear_live_filter" then
54-
return keypress_funcs[action]()
56+
local function change_dir_action(node)
57+
if node.name == ".." then
58+
require("nvim-tree.actions.change-dir").fn ".."
59+
elseif node.nodes ~= nil then
60+
require("nvim-tree.actions.change-dir").fn(lib.get_last_group_node(node).absolute_path)
5561
end
62+
end
63+
64+
local function open_file(action, node)
65+
local path = node.absolute_path
66+
if node.link_to and not node.nodes then
67+
path = node.link_to
68+
end
69+
require("nvim-tree.actions.open-file").fn(action, path)
70+
end
5671

72+
local function handle_tree_actions(action)
5773
local node = lib.get_node_at_cursor()
5874
if not node then
5975
return
6076
end
6177

6278
local custom_function = M.custom_keypress_funcs[action]
63-
local default_function = keypress_funcs[action]
79+
local defined_action = Actions[action]
6480

6581
if type(custom_function) == "function" then
6682
return custom_function(node)
67-
elseif default_function then
68-
return default_function(node)
83+
elseif defined_action then
84+
return defined_action(node)
6985
end
7086

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
87+
local is_parent = node.name == ".."
88+
89+
if action == "preview" and is_parent then
8490
return
8591
end
8692

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
93+
if action == "cd" or is_parent then
94+
return change_dir_action(node)
95+
end
96+
97+
if node.nodes then
9098
lib.expand_or_collapse(node)
9199
else
92-
require("nvim-tree.actions.open-file").fn(action, node.absolute_path)
100+
open_file(action, node)
101+
end
102+
end
103+
104+
function M.dispatch(action)
105+
if view.is_help_ui() then
106+
handle_action_on_help_ui(action)
107+
elseif action:match "live" ~= nil then
108+
handle_filter_actions(action)
109+
else
110+
handle_tree_actions(action)
93111
end
94112
end
95113

0 commit comments

Comments
 (0)