Skip to content

Commit 8af176e

Browse files
authored
feat: add same_level config option for add file/dir commands (#522)
1 parent cff1adb commit 8af176e

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

lua/neo-tree/defaults.lua

+8-4
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,10 @@ local config = {
298298
-- you can also specify border here, if you want a different setting from
299299
-- the global popup_border_style.
300300
},
301+
same_level = false, -- Create and paste/move files/directories on the same level as the directory under cursor (as opposed to within the directory under cursor).
302+
insert_as = "child", -- Affects how nodes get inserted into the tree during creation/pasting/moving of files if the node under the cursor is a directory:
303+
-- "child": Insert nodes as children of the directory under cursor.
304+
-- "sibling": Insert nodes as siblings of the directory under cursor.
301305
-- Mappings for tree window. See `:h neo-tree-mappings` for a list of built-in commands.
302306
-- You can also create your own commands by providing a function instead of a string.
303307
mapping_options = {
@@ -330,17 +334,17 @@ local config = {
330334
"add",
331335
-- some commands may take optional config options, see `:h neo-tree-mappings` for details
332336
config = {
333-
show_path = "none" -- "none", "relative", "absolute"
337+
show_path = "none", -- "none", "relative", "absolute"
334338
}
335339
},
336-
["A"] = "add_directory", -- also accepts the config.show_path option.
340+
["A"] = "add_directory", -- also accepts the config.show_path and config.insert_as options.
337341
["d"] = "delete",
338342
["r"] = "rename",
339343
["y"] = "copy_to_clipboard",
340344
["x"] = "cut_to_clipboard",
341345
["p"] = "paste_from_clipboard",
342-
["c"] = "copy", -- takes text input for destination, also accepts the config.show_path option
343-
["m"] = "move", -- takes text input for destination, also accepts the config.show_path option
346+
["c"] = "copy", -- takes text input for destination, also accepts the config.show_path and config.insert_as options
347+
["m"] = "move", -- takes text input for destination, also accepts the config.show_path and config.insert_as options
344348
["e"] = "toggle_auto_expand_width",
345349
["q"] = "close_window",
346350
["?"] = "show_help",

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

+24-9
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,30 @@ local Preview = require("neo-tree.sources.common.preview")
1515
---@param tree table to look for nodes
1616
---@param node table to look for folder parent
1717
---@return table table
18-
local function get_folder_node(tree, node)
18+
local function get_folder_node(state, node)
19+
local tree = state.tree
1920
if not node then
2021
node = tree:get_node()
2122
end
23+
24+
local insert_as_local = state.config.insert_as
25+
local insert_as_global = require("neo-tree").config.window.insert_as
26+
local use_parent
27+
if insert_as_local then
28+
use_parent = insert_as_local == "sibling"
29+
else
30+
use_parent = insert_as_global == "sibling"
31+
end
32+
33+
local is_open_dir = node.type == "directory" and (node:is_expanded() or node.empty_expanded)
34+
if use_parent and not is_open_dir then
35+
return tree:get_node(node:get_parent_id())
36+
end
37+
2238
if node.type == "directory" then
2339
return node
2440
end
25-
return get_folder_node(tree, tree:get_node(node:get_parent_id()))
41+
return get_folder_node(state, tree:get_node(node:get_parent_id()))
2642
end
2743

2844
---The using_root_directory is used to decide what part of the filename to show
@@ -31,7 +47,7 @@ end
3147
---@return string The root path from which the relative source path should be taken
3248
local function get_using_root_directory(state)
3349
-- default to showing only the basename of the path
34-
local using_root_directory = get_folder_node(state.tree):get_id()
50+
local using_root_directory = get_folder_node(state):get_id()
3551
local show_path = state.config.show_path
3652
if show_path == "absolute" then
3753
using_root_directory = ""
@@ -65,8 +81,7 @@ end
6581
---@param state table The state of the source
6682
---@param callback function The callback to call when the command is done. Called with the parent node as the argument.
6783
M.add = function(state, callback)
68-
local tree = state.tree
69-
local node = get_folder_node(tree)
84+
local node = get_folder_node(state)
7085
local in_directory = node:get_id()
7186
local using_root_directory = get_using_root_directory(state)
7287
fs_actions.create_node(in_directory, callback, using_root_directory)
@@ -76,8 +91,7 @@ end
7691
---@param state table The state of the source
7792
---@param callback function The callback to call when the command is done. Called with the parent node as the argument.
7893
M.add_directory = function(state, callback)
79-
local tree = state.tree
80-
local node = get_folder_node(tree)
94+
local node = get_folder_node(state)
8195
local in_directory = node:get_id()
8296
local using_root_directory = get_using_root_directory(state)
8397
fs_actions.create_directory(in_directory, callback, using_root_directory)
@@ -373,7 +387,7 @@ end
373387
---@param callback function The callback to call when the command is done. Called with the parent node as the argument.
374388
M.paste_from_clipboard = function(state, callback)
375389
if state.clipboard then
376-
local folder = get_folder_node(state.tree):get_id()
390+
local folder = get_folder_node(state):get_id()
377391
-- Convert to list so to make it easier to pop items from the stack.
378392
local clipboard_list = {}
379393
for _, item in pairs(state.clipboard) do
@@ -384,8 +398,9 @@ M.paste_from_clipboard = function(state, callback)
384398

385399
paste_complete = function(source, destination)
386400
if callback then
401+
local insert_as = require("neo-tree").config.window.insert_as
387402
-- open the folder so the user can see the new files
388-
local node = state.tree:get_node(folder)
403+
local node = insert_as == "sibling" and state.tree:get_node() or state.tree:get_node(folder)
389404
if not node then
390405
log.warn("Could not find node for " .. folder)
391406
end

0 commit comments

Comments
 (0)