Skip to content

Commit afb587b

Browse files
committed
feat: add add_directory command, closes #192
1 parent 7611094 commit afb587b

File tree

8 files changed

+57
-2
lines changed

8 files changed

+57
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ use {
133133
["f"] = "filter_on_submit",
134134
["<c-x>"] = "clear_filter",
135135
["a"] = "add",
136+
["A"] = "add_directory",
136137
["d"] = "delete",
137138
["r"] = "rename",
138139
["y"] = "copy_to_clipboard",

doc/neo-tree.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,11 @@ s = open_vsplit: Same as open, but opens in a vertical split
165165

166166

167167
FILE ACTIONS *neo-tree-file-actions*
168-
a = add: Create a new file or directory.
168+
a = add: Create a new file OR directory. Add a `/` to the
169+
end of the name to make a directory.
170+
171+
A = add_directory: Create a new directory, in this mode it does not
172+
need to end with a `/`.
169173

170174
d = delete: Delete the selected file or directory.
171175

lua/neo-tree/defaults.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ local config = {
118118
["."] = "set_root",
119119
["R"] = "refresh",
120120
["a"] = "add",
121+
["A"] = "add_directory",
121122
["d"] = "delete",
122123
["r"] = "rename",
123124
["y"] = "copy_to_clipboard",

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ M.add = function(state)
1515
cc.add(state, refresh)
1616
end
1717

18+
M.add_directory = function(state)
19+
cc.add_directory(state, refresh)
20+
end
21+
1822
M.buffer_delete = function(state)
1923
local node = state.tree:get_node()
2024
if node then

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ M.add = function(state, callback)
2929
fs_actions.create_node(node:get_id(), callback)
3030
end
3131

32+
---Add a new file or dir at the current node
33+
---@param state table The state of the source
34+
---@param callback function The callback to call when the command is done. Called with the parent node as the argument.
35+
M.add_directory = function(state, callback)
36+
local tree = state.tree
37+
local node = get_folder_node(tree, tree:get_node())
38+
39+
fs_actions.create_directory(node:get_id(), callback)
40+
end
41+
3242
M.close_all_nodes = function(state)
3343
renderer.collapse_all_nodes(state.tree)
3444
state.tree:render()

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ M.add = function(state)
1515
cc.add(state, utils.wrap(fs.show_new_children, state))
1616
end
1717

18+
M.add_directory = function(state)
19+
cc.add_directory(state, utils.wrap(fs.show_new_children, state))
20+
end
21+
1822
M.clear_filter = function(state)
1923
fs.reset_search(state, true)
2024
end

lua/neo-tree/sources/filesystem/lib/fs_actions.lua

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,34 @@ M.copy_node = function(source, _destination, callback)
117117
end, 'Copy "' .. name .. '" to:')
118118
end
119119

120-
-- Create Node
120+
--- Create a new directory
121+
M.create_directory = function(in_directory, callback)
122+
inputs.input('Enter name for new directory:', "", function(name)
123+
if not name or name == "" then
124+
return
125+
end
126+
local destination = in_directory .. utils.path_separator .. name
127+
if loop.fs_stat(destination) then
128+
log.warn("File already exists")
129+
return
130+
end
131+
132+
create_all_parents(destination)
133+
loop.fs_mkdir(destination, 493)
134+
135+
if callback then
136+
vim.schedule(function()
137+
events.fire_event(events.FILE_ADDED, destination)
138+
if callback then
139+
callback(destination)
140+
end
141+
end)
142+
end
143+
end)
144+
145+
end
146+
147+
--- Create Node
121148
M.create_node = function(in_directory, callback)
122149
inputs.input('Enter name for new file or directory (dirs end with a "/"):', "", function(name)
123150
if not name or name == "" then

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ M.add = function(state)
9494
cc.add(state, refresh)
9595
end
9696

97+
M.add_directory = function(state)
98+
cc.add_directory(state, refresh)
99+
end
100+
97101
M.close_node = cc.close_node
98102
M.close_all_nodes = cc.close_all_nodes
99103
M.close_window = cc.close_window

0 commit comments

Comments
 (0)