Skip to content

Commit 76ef029

Browse files
committed
refactor(files): utilize state.position methods for reveal and restore previosuly_focused
1 parent 023bab6 commit 76ef029

File tree

2 files changed

+13
-34
lines changed

2 files changed

+13
-34
lines changed

lua/neo-tree/sources/filesystem/init.lua

+6-34
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ M.follow = function(callback, force_show)
8181
end
8282
end
8383

84+
state.position.is.restorable = false -- we will handle setting cursor position here
8485
fs_scan.get_items_async(state, nil, path_to_reveal, function()
8586
local event = {
8687
event = events.AFTER_RENDER,
@@ -119,44 +120,15 @@ local navigate_internal = function(path, path_to_reveal, callback)
119120
end
120121

121122
if path_to_reveal then
122-
fs_scan.get_items_async(state, nil, path_to_reveal, function()
123-
renderer.focus_node(state, path_to_reveal)
124-
if callback then
125-
callback()
126-
end
127-
state.in_navigate = false
128-
end)
123+
state.position.set(path_to_reveal)
124+
fs_scan.get_items_async(state, nil, path_to_reveal, callback)
129125
else
130126
local follow_file = state.follow_current_file and manager.get_path_to_reveal()
131127
if utils.truthy(follow_file) then
132-
M.follow(function()
133-
if callback then
134-
callback()
135-
end
136-
state.in_navigate = false
137-
end, true)
128+
M.follow(callback, true)
138129
else
139-
local previously_focused = nil
140-
if state.tree and renderer.is_window_valid(state.winid) then
141-
local node = state.tree:get_node()
142-
if node then
143-
-- keep the current node selected
144-
previously_focused = node:get_id()
145-
end
146-
end
147-
fs_scan.get_items_async(state, nil, nil, function()
148-
local current_winid = vim.api.nvim_get_current_win()
149-
if path_changed and current_winid == state.winid and previously_focused then
150-
local currently_focused = state.tree:get_node():get_id()
151-
if currently_focused ~= previously_focused then
152-
renderer.focus_node(state, previously_focused, false)
153-
end
154-
end
155-
if callback then
156-
callback()
157-
end
158-
state.in_navigate = false
159-
end)
130+
state.position.save()
131+
fs_scan.get_items_async(state, nil, nil, callback)
160132
end
161133
end
162134

lua/neo-tree/sources/manager.lua

+7
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ local function create_state(tabnr, sd)
4747
-- within the scope of where we need to perform the restore operation
4848
state.position.is.restorable = true
4949
end,
50+
set = function(node_id)
51+
if not type(node_id) == "string" and node_id > "" then
52+
return
53+
end
54+
state.position.node_id = node_id
55+
state.position.is.restorable = true
56+
end,
5057
restore = function()
5158
if not state.position.node_id then
5259
return

0 commit comments

Comments
 (0)