Skip to content

Commit

Permalink
fix(command): allow reveal_file to be used with dir (#1649)
Browse files Browse the repository at this point in the history
When `reveal_file` and `dir` are specified, `dir` will now always be changed to,
unless `reveal_force_cwd` was set.

Closes #1500, #834
Related to #1501
Co-authored-by: pynappo <[email protected]>
  • Loading branch information
alanoliveira authored Jan 4, 2025
1 parent 2a0b2c5 commit e6645ec
Showing 1 changed file with 32 additions and 24 deletions.
56 changes: 32 additions & 24 deletions lua/neo-tree/command/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,6 @@ M.execute = function(args)
args.dir = args.dir:sub(1, -2)
end
path_changed = state.path ~= args.dir
else
args.dir = state.path
end

-- Handle setting git ref
Expand All @@ -156,11 +154,13 @@ M.execute = function(args)
-- manager.close(args.source)
--end
if do_reveal then
args.reveal_file = utils.normalize_path(args.reveal_file)
handle_reveal(args, state)
else
do_show_or_focus(args, state, force_navigate)
return
end
if not args.dir then
args.dir = state.path
end
do_show_or_focus(args, state, force_navigate)
end

---Parses and executes the command line. Use execute(args) instead.
Expand Down Expand Up @@ -207,30 +207,38 @@ do_show_or_focus = function(args, state, force_navigate)
end

handle_reveal = function(args, state)
args.reveal_file = utils.normalize_path(args.reveal_file)
-- Deal with cwd if we need to
local cwd = state.path
local path = args.reveal_file
if cwd == nil then
cwd = manager.get_cwd(state)
end
if args.reveal_force_cwd and not utils.is_subpath(cwd, path) then
args.dir, _ = utils.split_path(path)
local cwd = args.dir or state.path or manager.get_cwd(state)
if utils.is_subpath(cwd, args.reveal_file) then
args.dir = cwd
do_show_or_focus(args, state, true)
return
elseif not utils.is_subpath(cwd, path) then
-- force was not specified, so we need to ask the user
cwd, _ = utils.split_path(path)
inputs.confirm("File not in cwd. Change cwd to " .. cwd .. "?", function(response)
if response == true then
args.dir = cwd
else
args.reveal_file = nil
end
do_show_or_focus(args, state, true)
end)
end

local reveal_file_parent, _ = utils.split_path(args.reveal_file) --[[@as string]]
if args.reveal_force_cwd then
args.dir = reveal_file_parent
do_show_or_focus(args, state, true)
return
else
end

-- if dir doesn't have the reveal_file, ignore the reveal_file
if args.dir then
args.reveal_file = nil
do_show_or_focus(args, state, true)
return
end

-- force was not specified and the file does not belong to cwd, so we need to ask the user
inputs.confirm("File not in cwd. Change cwd to " .. reveal_file_parent .. "?", function(response)
if response == true then
args.dir = reveal_file_parent
else
args.reveal_file = nil
end
do_show_or_focus(args, state, true)
end)
end

return M

0 comments on commit e6645ec

Please sign in to comment.