Skip to content

feat: add same_level config option #522

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions lua/neo-tree/defaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,10 @@ local config = {
-- you can also specify border here, if you want a different setting from
-- the global popup_border_style.
},
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).
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:
-- "child": Insert nodes as children of the directory under cursor.
-- "sibling": Insert nodes as siblings of the directory under cursor.
-- Mappings for tree window. See `:h neo-tree-mappings` for a list of built-in commands.
-- You can also create your own commands by providing a function instead of a string.
mapping_options = {
Expand Down Expand Up @@ -330,17 +334,17 @@ local config = {
"add",
-- some commands may take optional config options, see `:h neo-tree-mappings` for details
config = {
show_path = "none" -- "none", "relative", "absolute"
show_path = "none", -- "none", "relative", "absolute"
}
},
["A"] = "add_directory", -- also accepts the config.show_path option.
["A"] = "add_directory", -- also accepts the config.show_path and config.insert_as options.
["d"] = "delete",
["r"] = "rename",
["y"] = "copy_to_clipboard",
["x"] = "cut_to_clipboard",
["p"] = "paste_from_clipboard",
["c"] = "copy", -- takes text input for destination, also accepts the config.show_path option
["m"] = "move", -- takes text input for destination, also accepts the config.show_path option
["c"] = "copy", -- takes text input for destination, also accepts the config.show_path and config.insert_as options
["m"] = "move", -- takes text input for destination, also accepts the config.show_path and config.insert_as options
["e"] = "toggle_auto_expand_width",
["q"] = "close_window",
["?"] = "show_help",
Expand Down
33 changes: 24 additions & 9 deletions lua/neo-tree/sources/common/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,30 @@ local Preview = require("neo-tree.sources.common.preview")
---@param tree table to look for nodes
---@param node table to look for folder parent
---@return table table
local function get_folder_node(tree, node)
local function get_folder_node(state, node)
local tree = state.tree
if not node then
node = tree:get_node()
end

local insert_as_local = state.config.insert_as
local insert_as_global = require("neo-tree").config.window.insert_as
local use_parent
if insert_as_local then
use_parent = insert_as_local == "sibling"
else
use_parent = insert_as_global == "sibling"
end

local is_open_dir = node.type == "directory" and (node:is_expanded() or node.empty_expanded)
if use_parent and not is_open_dir then
return tree:get_node(node:get_parent_id())
end

if node.type == "directory" then
return node
end
return get_folder_node(tree, tree:get_node(node:get_parent_id()))
return get_folder_node(state, tree:get_node(node:get_parent_id()))
end

---The using_root_directory is used to decide what part of the filename to show
Expand All @@ -31,7 +47,7 @@ end
---@return string The root path from which the relative source path should be taken
local function get_using_root_directory(state)
-- default to showing only the basename of the path
local using_root_directory = get_folder_node(state.tree):get_id()
local using_root_directory = get_folder_node(state):get_id()
local show_path = state.config.show_path
if show_path == "absolute" then
using_root_directory = ""
Expand Down Expand Up @@ -65,8 +81,7 @@ end
---@param state table The state of the source
---@param callback function The callback to call when the command is done. Called with the parent node as the argument.
M.add = function(state, callback)
local tree = state.tree
local node = get_folder_node(tree)
local node = get_folder_node(state)
local in_directory = node:get_id()
local using_root_directory = get_using_root_directory(state)
fs_actions.create_node(in_directory, callback, using_root_directory)
Expand All @@ -76,8 +91,7 @@ end
---@param state table The state of the source
---@param callback function The callback to call when the command is done. Called with the parent node as the argument.
M.add_directory = function(state, callback)
local tree = state.tree
local node = get_folder_node(tree)
local node = get_folder_node(state)
local in_directory = node:get_id()
local using_root_directory = get_using_root_directory(state)
fs_actions.create_directory(in_directory, callback, using_root_directory)
Expand Down Expand Up @@ -373,7 +387,7 @@ end
---@param callback function The callback to call when the command is done. Called with the parent node as the argument.
M.paste_from_clipboard = function(state, callback)
if state.clipboard then
local folder = get_folder_node(state.tree):get_id()
local folder = get_folder_node(state):get_id()
-- Convert to list so to make it easier to pop items from the stack.
local clipboard_list = {}
for _, item in pairs(state.clipboard) do
Expand All @@ -384,8 +398,9 @@ M.paste_from_clipboard = function(state, callback)

paste_complete = function(source, destination)
if callback then
local insert_as = require("neo-tree").config.window.insert_as
-- open the folder so the user can see the new files
local node = state.tree:get_node(folder)
local node = insert_as == "sibling" and state.tree:get_node() or state.tree:get_node(folder)
if not node then
log.warn("Could not find node for " .. folder)
end
Expand Down