Skip to content

Commit f3e8633

Browse files
authored
fix(fs_actions): disallow deletion of root nodes (#1793)
1 parent 8bfcc95 commit f3e8633

File tree

3 files changed

+52
-22
lines changed

3 files changed

+52
-22
lines changed

lua/neo-tree/sources/common/commands.lua

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -645,16 +645,35 @@ end
645645
M.delete = function(state, callback)
646646
local tree = state.tree
647647
local node = tree:get_node()
648-
if node.type == "file" or node.type == "directory" then
649-
fs_actions.delete_node(node.path, callback)
650-
else
648+
if node.type ~= "file" and node.type ~= "directory" then
651649
log.warn("The `delete` command can only be used on files and directories")
650+
return
651+
end
652+
if node:get_depth() == 1 then
653+
log.error(
654+
"Will not delete root node "
655+
.. node.path
656+
.. ", please back out of the current directory if you want to delete the root node."
657+
)
658+
return
652659
end
660+
fs_actions.delete_node(node.path, callback)
653661
end
654662

663+
---@param selected_nodes NuiTree.Node[]
664+
---@param callback function
655665
M.delete_visual = function(state, selected_nodes, callback)
656666
local paths_to_delete = {}
657667
for _, node_to_delete in pairs(selected_nodes) do
668+
if node_to_delete:get_depth() == 1 then
669+
log.error(
670+
"Will not delete root node "
671+
.. node_to_delete.path
672+
.. ", please back out of the current directory if you want to delete the root node."
673+
)
674+
return
675+
end
676+
658677
if node_to_delete.type == "file" or node_to_delete.type == "directory" then
659678
table.insert(paths_to_delete, node_to_delete.path)
660679
end

lua/neo-tree/ui/inputs.lua

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -76,25 +76,33 @@ M.input = function(message, default_value, callback, options, completion)
7676
end
7777
end
7878

79+
---Blocks if callback is omitted
80+
---@param message string
81+
---@param callback? fun(confirmed: boolean)
82+
---@return boolean? confirmed_if_no_callback
7983
M.confirm = function(message, callback)
80-
if nt.config.use_popups_for_input then
81-
local popup_options = popups.popup_options(message, 10)
84+
if callback then
85+
if nt.config.use_popups_for_input then
86+
local popup_options = popups.popup_options(message, 10)
8287

83-
---@class NuiInput
84-
local input = NuiInput(popup_options, {
85-
prompt = " y/n: ",
86-
on_close = function()
87-
callback(false)
88-
end,
89-
on_submit = function(value)
90-
callback(value == "y" or value == "Y")
91-
end,
92-
})
88+
---@class NuiInput
89+
local input = NuiInput(popup_options, {
90+
prompt = " y/n: ",
91+
on_close = function()
92+
callback(false)
93+
end,
94+
on_submit = function(value)
95+
callback(value == "y" or value == "Y")
96+
end,
97+
})
9398

94-
input.prompt_type = "confirm"
95-
M.show_input(input)
99+
input.prompt_type = "confirm"
100+
M.show_input(input)
101+
else
102+
callback(vim.fn.confirm(message, "&Yes\n&No") == 1)
103+
end
96104
else
97-
callback(vim.fn.confirm(message, "&Yes\n&No") == 1)
105+
return vim.fn.confirm(message, "&Yes\n&No") == 1
98106
end
99107
end
100108

lua/neo-tree/ui/renderer.lua

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1259,13 +1259,16 @@ local function group_empty_dirs(node)
12591259
end
12601260

12611261
---Shows the given items as a tree.
1262-
--@param sourceItems table The list of items to transform.
1263-
--@param state table The current state of the plugin.
1264-
--@param parentId string Optional. The id of the parent node to display these nodes
1265-
--at; defaults to nil.
1262+
---@param sourceItems table? The list of items to transform.
1263+
---@param state table The current state of the plugin.
1264+
---@param parentId string? The id of the parent node to display these nodes at
1265+
---@param callback function? The id of the parent node to display these nodes at
12661266
M.show_nodes = function(sourceItems, state, parentId, callback)
12671267
--local id = string.format("show_nodes %s:%s [%s]", state.name, state.force_float, state.tabid)
12681268
--utils.debounce(id, function()
1269+
if not sourceItems then
1270+
return
1271+
end
12691272
events.fire_event(events.BEFORE_RENDER, state)
12701273
state.longest_width_exact = 0
12711274
local parent

0 commit comments

Comments
 (0)