Skip to content

Commit e6645ec

Browse files
authored
fix(command): allow reveal_file to be used with dir (#1649)
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]>
1 parent 2a0b2c5 commit e6645ec

File tree

1 file changed

+32
-24
lines changed

1 file changed

+32
-24
lines changed

lua/neo-tree/command/init.lua

+32-24
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,6 @@ M.execute = function(args)
129129
args.dir = args.dir:sub(1, -2)
130130
end
131131
path_changed = state.path ~= args.dir
132-
else
133-
args.dir = state.path
134132
end
135133

136134
-- Handle setting git ref
@@ -156,11 +154,13 @@ M.execute = function(args)
156154
-- manager.close(args.source)
157155
--end
158156
if do_reveal then
159-
args.reveal_file = utils.normalize_path(args.reveal_file)
160157
handle_reveal(args, state)
161-
else
162-
do_show_or_focus(args, state, force_navigate)
158+
return
159+
end
160+
if not args.dir then
161+
args.dir = state.path
163162
end
163+
do_show_or_focus(args, state, force_navigate)
164164
end
165165

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

209209
handle_reveal = function(args, state)
210+
args.reveal_file = utils.normalize_path(args.reveal_file)
210211
-- Deal with cwd if we need to
211-
local cwd = state.path
212-
local path = args.reveal_file
213-
if cwd == nil then
214-
cwd = manager.get_cwd(state)
215-
end
216-
if args.reveal_force_cwd and not utils.is_subpath(cwd, path) then
217-
args.dir, _ = utils.split_path(path)
212+
local cwd = args.dir or state.path or manager.get_cwd(state)
213+
if utils.is_subpath(cwd, args.reveal_file) then
214+
args.dir = cwd
218215
do_show_or_focus(args, state, true)
219216
return
220-
elseif not utils.is_subpath(cwd, path) then
221-
-- force was not specified, so we need to ask the user
222-
cwd, _ = utils.split_path(path)
223-
inputs.confirm("File not in cwd. Change cwd to " .. cwd .. "?", function(response)
224-
if response == true then
225-
args.dir = cwd
226-
else
227-
args.reveal_file = nil
228-
end
229-
do_show_or_focus(args, state, true)
230-
end)
217+
end
218+
219+
local reveal_file_parent, _ = utils.split_path(args.reveal_file) --[[@as string]]
220+
if args.reveal_force_cwd then
221+
args.dir = reveal_file_parent
222+
do_show_or_focus(args, state, true)
231223
return
232-
else
224+
end
225+
226+
-- if dir doesn't have the reveal_file, ignore the reveal_file
227+
if args.dir then
228+
args.reveal_file = nil
233229
do_show_or_focus(args, state, true)
230+
return
234231
end
232+
233+
-- force was not specified and the file does not belong to cwd, so we need to ask the user
234+
inputs.confirm("File not in cwd. Change cwd to " .. reveal_file_parent .. "?", function(response)
235+
if response == true then
236+
args.dir = reveal_file_parent
237+
else
238+
args.reveal_file = nil
239+
end
240+
do_show_or_focus(args, state, true)
241+
end)
235242
end
243+
236244
return M

0 commit comments

Comments
 (0)