Skip to content

Commit 26987d1

Browse files
committed
fix: fixes #79, cleanup follow logic when window is closed (#80)
1 parent 44bbec2 commit 26987d1

File tree

3 files changed

+27
-16
lines changed

3 files changed

+27
-16
lines changed

lua/neo-tree/log.lua

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,10 @@ log.new = function(config, standalone)
8686

8787
obj.set_level = function(level)
8888
if levels[level] then
89-
config.level = level
90-
print("[neo-tree] Log level set to " .. level)
89+
if config.level ~= level then
90+
config.level = level
91+
print("[neo-tree] Log level set to " .. level)
92+
end
9193
else
9294
print("[neo-tree] Invalid log level: " .. level)
9395
end
@@ -108,6 +110,9 @@ log.new = function(config, standalone)
108110
x = tostring(round(x, config.float_precision))
109111
elseif type(x) == "table" then
110112
x = vim.inspect(x)
113+
if #x > 200 then
114+
x = x:sub(1, 200) .. "..."
115+
end
111116
else
112117
x = tostring(x)
113118
end

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -146,19 +146,26 @@ M.float = function()
146146
M.navigate(state.path, path_to_reveal)
147147
end
148148

149-
M.follow = function(callback)
149+
M.follow = function(callback, force_show)
150150
log.trace("follow called")
151151
local path_to_reveal = get_path_to_reveal()
152152
if not utils.truthy(path_to_reveal) then
153-
return
153+
return false
154154
end
155155
local state = get_state()
156-
if not renderer.window_exists(state) then
157-
return
156+
if not force_show and not renderer.window_exists(state) then
157+
return false
158158
end
159159
local is_in_path = path_to_reveal:sub(1, #state.path) == state.path
160160
if not is_in_path then
161-
return
161+
return false
162+
end
163+
local node = state.tree and state.tree:get_node()
164+
if node then
165+
if node:get_id() == path_to_reveal then
166+
-- already focused
167+
return false
168+
end
162169
end
163170

164171
log.debug("follow file: ", path_to_reveal)
@@ -208,6 +215,7 @@ M.follow = function(callback)
208215

209216
events.subscribe(event)
210217
end)
218+
return true
211219
end
212220

213221
---Focus the window, opening it if it is not already open.
@@ -251,12 +259,11 @@ local navigate_internal = function(path, path_to_reveal, callback)
251259
local follow_file = state.follow_current_file and get_path_to_reveal()
252260
if utils.truthy(follow_file) then
253261
M.follow(function()
254-
renderer.focus_node(state, follow_file)
255262
if callback then
256263
callback()
257264
end
258265
state.in_navigate = false
259-
end)
266+
end, true)
260267
else
261268
local previously_focused = nil
262269
if state.tree and renderer.is_window_valid(state.winid) then
@@ -283,9 +290,6 @@ local navigate_internal = function(path, path_to_reveal, callback)
283290
end
284291

285292
if path_changed then
286-
if state.follow_current_file then
287-
M.follow()
288-
end
289293
if state.bind_to_cwd then
290294
vim.api.nvim_command("tcd " .. path)
291295
end

lua/neo-tree/ui/renderer.lua

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,10 +198,12 @@ M.focus_node = function(state, id, do_not_focus_window)
198198
if success then
199199
-- make sure we are not scrolled down if it can all fit on the screen
200200
local win_height = vim.api.nvim_win_get_height(state.winid)
201-
if win_height > linenr then
202-
vim.cmd("normal! zb")
203-
elseif linenr < (win_height / 2) then
204-
vim.cmd("normal! zz")
201+
if vim.api.nvim_get_current_win() == state.winid then
202+
if win_height > linenr then
203+
vim.cmd("normal! zb")
204+
elseif linenr < (win_height / 2) then
205+
vim.cmd("normal! zz")
206+
end
205207
end
206208
else
207209
log.warn("Failed to set cursor: " .. err)

0 commit comments

Comments
 (0)