Skip to content

Commit 6550d16

Browse files
committed
feat(files): load sync from user command, and async on refresh.
1 parent b1e09ab commit 6550d16

File tree

4 files changed

+26
-16
lines changed

4 files changed

+26
-16
lines changed

lua/neo-tree/command/init.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,15 +161,15 @@ do_show_or_focus = function(args, state, force_navigate)
161161
manager.navigate(state, args.dir, args.reveal_file, function()
162162
-- navigate changes the window to neo-tree, so just quickly hop back to the original window
163163
vim.api.nvim_set_current_win(current_win)
164-
end)
164+
end, false)
165165
elseif args.action == "focus" then
166166
-- "focus" mean open and jump to the window if closed, and just focus it if already opened
167167
if window_exists then
168168
vim.api.nvim_set_current_win(state.winid)
169169
end
170170
if force_navigate or not window_exists then
171171
close_other_sources()
172-
manager.navigate(state, args.dir, args.reveal_file)
172+
manager.navigate(state, args.dir, args.reveal_file, nil, false)
173173
end
174174
end
175175
end

lua/neo-tree/defaults.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ local config = {
211211
["."] = "set_root",
212212
}
213213
},
214-
async_directory_scan = true,
214+
async_directory_scan = true, -- only applies to refreshes, it's alwsays sync when called from the Neotree commands.
215215
bind_to_cwd = true, -- true creates a 2-way binding between vim's cwd and neo-tree's root
216216
-- The renderer section provides the renderers that will be used to render the tree.
217217
-- The first level is the node type.

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

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ M.reveal_current_file = function()
2727
return manager.reveal_current_file(M.name)
2828
end
2929

30-
local follow_internal = function(callback, force_show)
30+
local follow_internal = function(callback, force_show, async)
3131
log.trace("follow called")
3232
if vim.bo.filetype == "neo-tree" or vim.bo.filetype == "neo-tree-popup" then
3333
return
@@ -97,7 +97,7 @@ local follow_internal = function(callback, force_show)
9797
if type(callback) == "function" then
9898
callback()
9999
end
100-
end)
100+
end, async)
101101
return true
102102
end
103103

@@ -110,7 +110,7 @@ M.follow = function(callback, force_show)
110110
end, 100, utils.debounce_strategy.CALL_LAST_ONLY)
111111
end
112112

113-
M._navigate_internal = function(state, path, path_to_reveal, callback)
113+
M._navigate_internal = function(state, path, path_to_reveal, callback, async)
114114
log.trace("navigate_internal", state.current_position, path, path_to_reveal)
115115
state.dirty = false
116116
local is_search = utils.truthy(state.search_pattern)
@@ -135,14 +135,14 @@ M._navigate_internal = function(state, path, path_to_reveal, callback)
135135
)
136136
fs_scan.get_items(state, nil, path_to_reveal, callback)
137137
else
138-
local is_split = state.current_position == "current"
138+
local is_current = state.current_position == "current"
139139
local follow_file = state.follow_current_file
140140
and not is_search
141-
and not is_split
141+
and not is_current
142142
and manager.get_path_to_reveal()
143143
local handled = false
144144
if utils.truthy(follow_file) then
145-
handled = follow_internal(callback, true)
145+
handled = follow_internal(callback, true, async)
146146
end
147147
if not handled then
148148
local success, msg = pcall(renderer.position.save, state)
@@ -151,7 +151,7 @@ M._navigate_internal = function(state, path, path_to_reveal, callback)
151151
else
152152
log.trace("navigate_internal: FAILED to save position: ", msg)
153153
end
154-
fs_scan.get_items(state, nil, nil, callback)
154+
fs_scan.get_items(state, nil, nil, callback, async)
155155
end
156156
end
157157

@@ -168,10 +168,10 @@ end
168168
---@param path string Path to navigate to. If empty, will navigate to the cwd.
169169
---@param path_to_reveal string Node to focus after the items are loaded.
170170
---@param callback function Callback to call after the items are loaded.
171-
M.navigate = function(state, path, path_to_reveal, callback)
172-
log.trace("navigate", path, path_to_reveal)
171+
M.navigate = function(state, path, path_to_reveal, callback, async)
172+
log.trace("navigate", path, path_to_reveal, async)
173173
utils.debounce("filesystem_navigate", function()
174-
M._navigate_internal(state, path, path_to_reveal, callback)
174+
M._navigate_internal(state, path, path_to_reveal, callback, async)
175175
end, utils.debounce_strategy.CALL_FIRST_AND_LAST, 100)
176176
end
177177

@@ -340,7 +340,16 @@ M.setup = function(config, global_config)
340340
-- Update the "modified" component
341341
manager.subscribe(M.name, {
342342
event = events.VIM_BUFFER_MODIFIED_SET,
343-
handler = wrap(manager.redraw)
343+
handler = function (arg)
344+
local afile = arg.afile or ""
345+
local source = afile:match("^neo%-tree ([%l%-]+) %[%d+%]")
346+
if source then
347+
log.trace("Ignoring vim_modified_set event from " .. source)
348+
return
349+
end
350+
log.trace("refreshing due to vim_modified_set event: ", afile)
351+
manager.redraw(M.name)
352+
end
344353
})
345354
end
346355

lua/neo-tree/sources/manager.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,8 @@ end
348348
---@param path string Path to navigate to. If empty, will navigate to the cwd.
349349
---@param path_to_reveal string Node to focus after the items are loaded.
350350
---@param callback function Callback to call after the items are loaded.
351-
M.navigate = function(state_or_source_name, path, path_to_reveal, callback)
351+
---@param async boolean Whether to load the items asynchronously, may not be respected by all sources.
352+
M.navigate = function(state_or_source_name, path, path_to_reveal, callback, async)
352353
local state, source_name
353354
if type(state_or_source_name) == "string" then
354355
state = M.get_state(state_or_source_name)
@@ -360,7 +361,7 @@ M.navigate = function(state_or_source_name, path, path_to_reveal, callback)
360361
log.error("navigate: state_or_source_name must be a string or a table")
361362
end
362363
log.trace("navigate", source_name, path, path_to_reveal)
363-
require("neo-tree.sources." .. source_name).navigate(state, path, path_to_reveal, callback)
364+
require("neo-tree.sources." .. source_name).navigate(state, path, path_to_reveal, callback, async)
364365
end
365366

366367
---Redraws the tree without scanning the filesystem again. Use this after

0 commit comments

Comments
 (0)