Skip to content

Commit 8197313

Browse files
committed
fix: correct state issues after PackerCompile, fixes #105
1 parent f04d156 commit 8197313

File tree

3 files changed

+59
-36
lines changed

3 files changed

+59
-36
lines changed

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,13 @@ M._navigate_internal = function(state, path, path_to_reveal, callback)
123123
end
124124

125125
if path_to_reveal then
126-
state.position.set(path_to_reveal)
126+
renderer.position.set(state, path_to_reveal)
127+
log.debug(
128+
"navigate_internal: in path_to_reveal, state.position is ",
129+
state.position.node_id,
130+
", restorable = ",
131+
state.position.is.restorable
132+
)
127133
fs_scan.get_items_async(state, nil, path_to_reveal, callback)
128134
else
129135
local is_search = utils.truthy(state.search_pattern)
@@ -133,7 +139,7 @@ M._navigate_internal = function(state, path, path_to_reveal, callback)
133139
handled = follow_internal(callback, true)
134140
end
135141
if not handled then
136-
local success, msg = pcall(state.position.save)
142+
local success, msg = pcall(renderer.position.save, state)
137143
if success then
138144
log.trace("navigate_internal: position saved")
139145
else
@@ -176,7 +182,7 @@ M.reset_search = function(state, refresh, open_current_node)
176182
local success, node = pcall(state.tree.get_node, state.tree)
177183
if success and node then
178184
local path = node:get_id()
179-
state.position.set(path)
185+
renderer.position.set(state, path)
180186
if node.type == "directory" then
181187
log.trace("opening directory from search: ", path)
182188
M.navigate(state, nil, path, function()

lua/neo-tree/sources/manager.lua

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -39,35 +39,7 @@ local function create_state(tabnr, sd, winid)
3939
state.id = winid or tabnr
4040
state.dirty = true
4141
state.position = {
42-
save = function()
43-
if state.tree and renderer.window_exists(state) then
44-
local node = state.tree:get_node()
45-
if node then
46-
state.position.node_id = node:get_id()
47-
end
48-
end
49-
-- Only need to restore the cursor state once per save, comes
50-
-- into play when some actions fire multiple times per "iteration"
51-
-- within the scope of where we need to perform the restore operation
52-
state.position.is.restorable = true
53-
end,
54-
set = function(node_id)
55-
if not type(node_id) == "string" and node_id > "" then
56-
return
57-
end
58-
state.position.node_id = node_id
59-
state.position.is.restorable = true
60-
end,
61-
restore = function()
62-
if not state.position.node_id then
63-
return
64-
end
65-
if state.position.is.restorable then
66-
renderer.focus_node(state, state.position.node_id, true)
67-
end
68-
state.position.is.restorable = false
69-
end,
70-
is = { restorable = true },
42+
is = { restorable = false },
7143
}
7244
return state
7345
end

lua/neo-tree/ui/renderer.lua

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ M.close = function(state)
2626
window_existed = true
2727
if state.current_position == "split" then
2828
-- we are going to hide the buffer instead of closing the window
29-
state.position.save()
29+
M.position.save(state)
3030
local new_buf = vim.fn.bufnr("#")
3131
if new_buf < 1 then
3232
new_buf = vim.api.nvim_create_buf(true, false)
@@ -171,26 +171,31 @@ end
171171
---otherwise.
172172
M.focus_node = function(state, id, do_not_focus_window, relative_movement, bottom_scroll_padding)
173173
if not id and not relative_movement then
174+
log.debug("focus_node called with no id and no relative movement")
174175
return nil
175176
end
176177
relative_movement = relative_movement or 0
177178
bottom_scroll_padding = bottom_scroll_padding or 0
178179

179180
local tree = state.tree
180181
if not tree then
182+
log.debug("focus_node called with no tree")
181183
return false
182184
end
183185
local node = tree:get_node(id)
184186
if not node then
187+
log.debug("focus_node cannot find node with id ", id)
185188
return false
186189
end
187190
id = node:get_id() -- in case nil was passed in for id, meaning current node
188191

189192
local bufnr = utils.get_value(state, "bufnr", 0, true)
190193
if bufnr == 0 then
194+
log.debug("focus_node: state has no bufnr ", state.bufnr, " / ", state.winid)
191195
return false
192196
end
193197
if not vim.api.nvim_buf_is_valid(bufnr) then
198+
log.debug("focus_node: bufnr is not valid")
194199
return false
195200
end
196201
local lines = vim.api.nvim_buf_line_count(state.bufnr)
@@ -241,11 +246,13 @@ M.focus_node = function(state, id, do_not_focus_window, relative_movement, botto
241246
end
242247
return success
243248
else
249+
log.debug("focus_node: window does not exist")
244250
return false
245251
end
246252
end
247253
else
248254
--must be out of nodes
255+
log.debug("focus_node: node not found")
249256
return false
250257
end
251258
end
@@ -312,6 +319,43 @@ M.collapse_all_nodes = function(tree)
312319
root:expand()
313320
end
314321

322+
---Functions to save and restore the focused node.
323+
M.position = {
324+
save = function(state)
325+
if state.tree and M.window_exists(state) then
326+
local node = state.tree:get_node()
327+
if node then
328+
state.position.node_id = node:get_id()
329+
end
330+
end
331+
-- Only need to restore the cursor state once per save, comes
332+
-- into play when some actions fire multiple times per "iteration"
333+
-- within the scope of where we need to perform the restore operation
334+
state.position.is.restorable = true
335+
end,
336+
set = function(state, node_id)
337+
if not type(node_id) == "string" and node_id > "" then
338+
return
339+
end
340+
state.position.node_id = node_id
341+
state.position.is.restorable = true
342+
end,
343+
restore = function(state)
344+
if not state.position.node_id then
345+
log.debug("No node_id to restore to")
346+
return
347+
end
348+
if state.position.is.restorable then
349+
log.debug("Restoring position to node_id: " .. state.position.node_id)
350+
M.focus_node(state, state.position.node_id, true)
351+
else
352+
log.debug("Position is not restorable")
353+
end
354+
state.position.is.restorable = false
355+
end,
356+
is = { restorable = true },
357+
}
358+
315359
---Visits all nodes in the tree and returns a list of all nodes that match the
316360
---given predicate.
317361
---@param tree table The NuiTree to search.
@@ -414,6 +458,7 @@ create_window = function(state)
414458
end, { once = true })
415459
state.winid = win.winid
416460
state.bufnr = win.bufnr
461+
log.debug("Created floating window with winid: ", win.winid, " and bufnr: ", win.bufnr)
417462
vim.api.nvim_buf_set_name(state.bufnr, bufname)
418463

419464
-- why is this necessary?
@@ -449,7 +494,7 @@ create_window = function(state)
449494

450495
if win == nil then
451496
autocmd.buf.define(state.bufnr, "WinLeave", function()
452-
state.position.save()
497+
M.position.save(state)
453498
end)
454499
else
455500
-- Used to track the position of the cursor within the tree as it gains and loses focus
@@ -459,7 +504,7 @@ create_window = function(state)
459504
-- to mention that it would be too late to register `WinEnter` here for the first
460505
-- iteration of that event on the tree window)
461506
win:on({ "WinLeave" }, function()
462-
state.position.save()
507+
M.position.save(state)
463508
end)
464509

465510
win:on({ "BufDelete" }, function()
@@ -589,7 +634,7 @@ draw = function(nodes, state, parent_id)
589634

590635
-- Restore the cursor position/focused node in the tree based on the state
591636
-- when it was last closed
592-
state.position.restore()
637+
M.position.restore(state)
593638
end
594639

595640
---Shows the given items as a tree.

0 commit comments

Comments
 (0)