Skip to content
Open
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
13 changes: 1 addition & 12 deletions lua/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -141,17 +141,6 @@ local function manage_netrw()
end
end

---@param name string|nil
function M.change_dir(name)
if name then
explorer_fn("change_dir", name)
end

if config.g.update_focused_file.update_root.enable then
actions.tree.find_file.fn()
end
end

local function setup_autocommands()
local augroup_id = vim.api.nvim_create_augroup("NvimTree", { clear = true })
local function create_nvim_tree_autocmd(name, custom_opts)
Expand Down Expand Up @@ -180,7 +169,7 @@ local function setup_autocommands()
if config.g.sync_root_with_cwd then
create_nvim_tree_autocmd("DirChanged", {
callback = function()
M.change_dir(vim.loop.cwd())
actions.tree.change_dir.fn(vim.loop.cwd())
end,
})
end
Expand Down
137 changes: 77 additions & 60 deletions lua/nvim-tree/actions/fs/rename-file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -97,81 +97,98 @@ function M.rename(node, to)
end
end

---@param default_modifier string|nil
---@return fun(node: Node, modifier: string)
function M.fn(default_modifier)
default_modifier = default_modifier or ":t"

return function(node, modifier)
local explorer = core.get_explorer()
if not explorer then
return
end
---@param node Node
---@param modifier? string
local function prompt_to_rename(node, modifier)
if not modifier or type(modifier) ~= "string" then
modifier = ":t"
end

if type(node) ~= "table" then
node = explorer:get_node_at_cursor()
end
if not node then
local explorer = core.get_explorer()
if not explorer then
return
end

if type(node) ~= "table" then
local node_at_cursor = explorer:get_node_at_cursor()
if not node_at_cursor then
return
end
node = node_at_cursor
end

if type(modifier) ~= "string" then
modifier = default_modifier
end
-- support for only specific modifiers have been implemented
if not ALLOWED_MODIFIERS[modifier] then
notify.warn("Modifier " .. vim.inspect(modifier) .. " is not in allowed list : " .. table.concat(ALLOWED_MODIFIERS, ","))
return
end

-- support for only specific modifiers have been implemented
if not ALLOWED_MODIFIERS[modifier] then
notify.warn("Modifier " .. vim.inspect(modifier) .. " is not in allowed list : " .. table.concat(ALLOWED_MODIFIERS, ","))
return
end
local dir = node:as(DirectoryNode)
if dir then
node = dir:last_group_node()
end
if node.name == ".." then
return
end

local dir = node:as(DirectoryNode)
if dir then
node = dir:last_group_node()
end
if node.name == ".." then
local namelen = node.name:len()
local directory = node.absolute_path:sub(0, namelen * -1 - 1)
local default_path
local prepend = ""
local append = ""
default_path = vim.fn.fnamemodify(node.absolute_path, modifier)
if modifier:sub(0, 2) == ":t" then
prepend = directory
end
if modifier == ":t:r" then
local extension = vim.fn.fnamemodify(node.name, ":e")
append = extension:len() == 0 and "" or "." .. extension
end
if modifier == ":p:h" then
default_path = default_path .. "/"
end

local input_opts = {
prompt = "Rename to ",
default = default_path,
completion = "file",
}

vim.ui.input(input_opts, function(new_file_path)
utils.clear_prompt()
if not new_file_path then
return
end

local namelen = node.name:len()
local directory = node.absolute_path:sub(0, namelen * -1 - 1)
local default_path
local prepend = ""
local append = ""
default_path = vim.fn.fnamemodify(node.absolute_path, modifier)
if modifier:sub(0, 2) == ":t" then
prepend = directory
end
if modifier == ":t:r" then
local extension = vim.fn.fnamemodify(node.name, ":e")
append = extension:len() == 0 and "" or "." .. extension
end
if modifier == ":p:h" then
default_path = default_path .. "/"
local full_new_path = prepend .. new_file_path .. append

M.rename(node, full_new_path)
if not M.config.filesystem_watchers.enable then
explorer:reload_explorer()
end

local input_opts = {
prompt = "Rename to ",
default = default_path,
completion = "file",
}
find_file(utils.path_remove_trailing(full_new_path))
end)
end

vim.ui.input(input_opts, function(new_file_path)
utils.clear_prompt()
if not new_file_path then
return
end
---@param node Node
function M.rename_node(node)
prompt_to_rename(node, ":t")
end

local full_new_path = prepend .. new_file_path .. append
---@param node Node
function M.rename_sub(node)
prompt_to_rename(node, ":p:h")
end

M.rename(node, full_new_path)
if not M.config.filesystem_watchers.enable then
explorer:reload_explorer()
end
---@param node Node
function M.rename_basename(node)
prompt_to_rename(node, ":t:r")
end

find_file(utils.path_remove_trailing(full_new_path))
end)
end
---@param node Node
function M.rename_full(node)
prompt_to_rename(node, ":p")
end

function M.setup(opts)
Expand Down
87 changes: 66 additions & 21 deletions lua/nvim-tree/actions/moves/item.lua
Original file line number Diff line number Diff line change
Expand Up @@ -215,32 +215,77 @@ end
---@field recurse boolean?

---@param opts NavigationItemOpts
---@return fun()
function M.fn(opts)
return function()
local explorer = core.get_explorer()
if not explorer then
return
end
local function item(opts)
local explorer = core.get_explorer()
if not explorer then
return
end

local recurse = false
local recurse = false

-- recurse only valid for git and diag moves.
if (opts.what == "git" or opts.what == "diag") and opts.recurse ~= nil then
recurse = opts.recurse
end
-- recurse only valid for git and diag moves.
if (opts.what == "git" or opts.what == "diag") and opts.recurse ~= nil then
recurse = opts.recurse
end

if not recurse then
move(explorer, opts.where, opts.what, opts.skip_gitignored)
return
end
if not recurse then
move(explorer, opts.where, opts.what, opts.skip_gitignored)
return
end

if opts.where == "next" then
move_next_recursive(explorer, opts.what, opts.skip_gitignored)
elseif opts.where == "prev" then
move_prev_recursive(explorer, opts.what, opts.skip_gitignored)
end
if opts.where == "next" then
move_next_recursive(explorer, opts.what, opts.skip_gitignored)
elseif opts.where == "prev" then
move_prev_recursive(explorer, opts.what, opts.skip_gitignored)
end
end

function M.git_next()
item({ where = "next", what = "git" })
end

function M.git_next_skip_gitignored()
item({ where = "next", what = "git", skip_gitignored = true })
end

function M.git_next_recursive()
item({ where = "next", what = "git", recurse = true })
end

function M.git_prev()
item({ where = "prev", what = "git" })
end

function M.git_prev_skip_gitignored()
item({ where = "prev", what = "git", skip_gitignored = true })
end

function M.git_prev_recursive()
item({ where = "prev", what = "git", recurse = true })
end

function M.diagnostics_next()
item({ where = "next", what = "diag" })
end

function M.diagnostics_next_recursive()
item({ where = "next", what = "diag", recurse = true })
end

function M.diagnostics_prev()
item({ where = "prev", what = "diag" })
end

function M.diagnostics_prev_recursive()
item({ where = "prev", what = "diag", recurse = true })
end

function M.opened_next()
item({ where = "next", what = "opened" })
end

function M.opened_prev()
item({ where = "prev", what = "opened" })
end

return M
61 changes: 33 additions & 28 deletions lua/nvim-tree/actions/moves/parent.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,45 @@ local DirectoryNode = require("nvim-tree.node.directory")

local M = {}

---@param should_close boolean|nil
---@return fun(node: Node): boolean|nil
function M.fn(should_close)
should_close = should_close or false

---@param node Node
return function(node)
local dir = node:as(DirectoryNode)
if dir then
dir = dir:last_group_node()
if should_close and dir.open then
dir.open = false
dir.explorer.renderer:draw()
return
end
---@param node Node
---@param should_close boolean
local function move(node, should_close)
local dir = node:as(DirectoryNode)
if dir then
dir = dir:last_group_node()
if should_close and dir.open then
dir.open = false
dir.explorer.renderer:draw()
return
end
end

local parent = (node:get_parent_of_group() or node).parent
local parent = (node:get_parent_of_group() or node).parent

if not parent or not parent.parent then
view.set_cursor({ 1, 0 })
return
end
if not parent or not parent.parent then
view.set_cursor({ 1, 0 })
return
end

local _, line = parent.explorer:find_node(function(n)
return n.absolute_path == parent.absolute_path
end)
local _, line = parent.explorer:find_node(function(n)
return n.absolute_path == parent.absolute_path
end)

view.set_cursor({ line + 1, 0 })
if should_close then
parent.open = false
parent.explorer.renderer:draw()
end
view.set_cursor({ line + 1, 0 })
if should_close then
parent.open = false
parent.explorer.renderer:draw()
end
end

---@param node Node
function M.move(node)
move(node, false)
end

---@param node Node
function M.move_close(node)
move(node, true)
end

return M
Loading
Loading