Skip to content

Commit f466ab0

Browse files
committed
refactor: closes #69, additional cleanup on source manager refactor
1 parent 17236fd commit f466ab0

File tree

5 files changed

+39
-24
lines changed

5 files changed

+39
-24
lines changed

lua/neo-tree.lua

+11-6
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ M.float = function(source_name, toggle_if_open)
125125
manager.float(source_name)
126126
end
127127

128+
--TODO: Remove the close_others option in 2.0
128129
M.focus = function(source_name, close_others, toggle_if_open)
129130
source_name = check_source(source_name)
130131
if toggle_if_open then
@@ -143,7 +144,13 @@ M.focus = function(source_name, close_others, toggle_if_open)
143144
end
144145

145146
M.reveal_current_file = function(source_name, toggle_if_open)
146-
manager.reveal_current_file(source_name, toggle_if_open)
147+
if toggle_if_open then
148+
if manager.close(source_name) then
149+
-- It was open, and now it's not.
150+
return
151+
end
152+
end
153+
manager.reveal_current_file(source_name)
147154
end
148155

149156
M.get_prior_window = function()
@@ -201,6 +208,7 @@ M.win_enter_event = function()
201208
end
202209
end
203210

211+
--TODO: Remove the do_not_focus and close_others options in 2.0
204212
M.show = function(source_name, do_not_focus, close_others, toggle_if_open)
205213
source_name = check_source(source_name)
206214
if toggle_if_open then
@@ -216,12 +224,9 @@ M.show = function(source_name, do_not_focus, close_others, toggle_if_open)
216224
M.close_all_except(source_name)
217225
end
218226
if do_not_focus then
219-
local current_win = vim.api.nvim_get_current_win()
220-
manager.show(source_name, function()
221-
vim.api.nvim_set_current_win(current_win)
222-
end)
223-
else
224227
manager.show(source_name)
228+
else
229+
manager.focus(source_name)
225230
end
226231
end
227232

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

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ end
3636
---@param path string Path to navigate to. If empty, will navigate to the cwd.
3737
M.navigate = function(path)
3838
local state = get_state()
39+
state.dirty = false
3940
local path_changed = false
4041
if path == nil then
4142
path = vim.fn.getcwd()

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ end
102102
local navigate_internal = function(path, path_to_reveal, callback)
103103
log.trace("navigate_internal", path, path_to_reveal)
104104
local state = get_state()
105-
print(state.filters)
105+
state.dirty = false
106106
local path_changed = false
107107
if path == nil then
108108
path = vim.fn.getcwd()

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

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ end
2222
---@param path string Path to navigate to. If empty, will navigate to the cwd.
2323
M.navigate = function(path)
2424
local state = get_state()
25+
state.dirty = false
2526
items.get_git_status(state)
2627
end
2728

lua/neo-tree/sources/manager.lua

+25-17
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ M.get_state = function(source_name, tabnr)
5353
end
5454
state = utils.table_copy(sd.default_config)
5555
state.tabnr = tabnr
56+
state.dirty = true
5657
sd.state_by_tab[tabnr] = state
5758
return state
5859
end
@@ -146,6 +147,9 @@ M.dir_changed = function(source_name)
146147
end
147148
if state.path and renderer.window_exists(state) then
148149
M.navigate(source_name, cwd)
150+
else
151+
state.path = cwd
152+
state.dirty = true
149153
end
150154
end
151155

@@ -183,7 +187,7 @@ M.focus = function(source_name, path_to_reveal, callback)
183187
if path_to_reveal then
184188
M.navigate(source_name, state.path, path_to_reveal, callback)
185189
else
186-
if renderer.window_exists(state) then
190+
if not state.dirty and renderer.window_exists(state) then
187191
vim.api.nvim_set_current_win(state.winid)
188192
else
189193
M.navigate(source_name, state.path, nil, callback)
@@ -200,14 +204,8 @@ M.navigate = function(source_name, path, path_to_reveal, callback)
200204
require("neo-tree.sources." .. source_name).navigate(path, path_to_reveal, callback)
201205
end
202206

203-
M.reveal_current_file = function(source_name, toggle_if_open)
207+
M.reveal_current_file = function(source_name)
204208
log.trace("Revealing current file")
205-
if toggle_if_open then
206-
if M.close() then
207-
-- It was open, and now it's not.
208-
return
209-
end
210-
end
211209
local state = M.get_state(source_name)
212210
require("neo-tree").close_all_except(source_name)
213211
local path = M.get_path_to_reveal()
@@ -225,6 +223,8 @@ M.reveal_current_file = function(source_name, toggle_if_open)
225223
if response == true then
226224
state.path = cwd
227225
M.focus(path)
226+
else
227+
M.focus()
228228
end
229229
end)
230230
return
@@ -249,12 +249,16 @@ end
249249
---Refreshes the tree by scanning the filesystem again.
250250
M.refresh = function(source_name, callback)
251251
log.trace(source_name, " refresh")
252-
local state = M.get_state(source_name)
253-
if state.path and renderer.window_exists(state) then
254-
if type(callback) ~= "function" then
255-
callback = nil
252+
local sd = get_source_data(source_name)
253+
for _, state in pairs(sd.state_by_tab) do
254+
if state.path and renderer.window_exists(state) then
255+
if type(callback) ~= "function" then
256+
callback = nil
257+
end
258+
M.navigate(source_name, state.path, nil, callback)
259+
else
260+
state.dirty = true
256261
end
257-
M.navigate(source_name, state.path, nil, callback)
258262
end
259263
end
260264

@@ -309,11 +313,15 @@ M.setup = function(source_name, config, global_config)
309313
end
310314
end
311315

312-
---Opens the tree and displays the current path or cwd.
313-
---@param callback function Callback to call after the items are loaded.
314-
M.show = function(source_name, callback)
316+
---Opens the tree and displays the current path or cwd, without focusing it.
317+
M.show = function(source_name)
315318
local state = M.get_state(source_name)
316-
M.navigate(source_name, state.path, nil, callback)
319+
if not renderer.window_exists(state) then
320+
local current_win = vim.api.nvim_get_current_win()
321+
M.navigate(source_name, state.path, nil, function()
322+
vim.api.nvim_set_current_win(current_win)
323+
end)
324+
end
317325
end
318326

319327
return M

0 commit comments

Comments
 (0)