Skip to content

perf: refresh less #1774

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions lua/neo-tree/events/queue.lua
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,19 @@ end
---@param event string
---@param args any?
M.fire_event = function(event, args)
local freq = utils.get_value(event_definitions, event .. ".debounce_frequency", 0, true)
local strategy = utils.get_value(event_definitions, event .. ".debounce_strategy", 0, true)
log.trace("Firing event: ", event, " with args: ", args)
local ev = event_definitions[event]
if not event_definitions[event] then
return
end
local freq = ev.debounce_frequency or 0
local strategy = ev.debounce_strategy or 0
if freq > 0 then
utils.debounce("EVENT_FIRED: " .. event, function()
utils.debounce(event, function()
log.trace("Firing debounced event: ", event, " with args: ", args)
fire_event_internal(event, args or {})
end, freq, strategy)
else
log.trace("Firing event: ", event, " with args: ", args)
return fire_event_internal(event, args or {})
end
end
Expand Down
6 changes: 3 additions & 3 deletions lua/neo-tree/setup/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ local define_events = function()
end

events.define_event(events.FS_EVENT, {
debounce_frequency = 100,
debounce_strategy = utils.debounce_strategy.CALL_LAST_ONLY,
debounce_frequency = 200,
debounce_strategy = utils.debounce_strategy.CALL_FIRST_AND_LAST,
})

local v = vim.version()
Expand Down Expand Up @@ -79,7 +79,7 @@ local define_events = function()
events.define_autocmd_event(events.VIM_WIN_CLOSED, { "WinClosed" })
events.define_autocmd_event(events.VIM_WIN_ENTER, { "WinEnter" }, 0, nil, true)

events.define_autocmd_event(events.GIT_EVENT, { "User FugitiveChanged" }, 100)
events.define_autocmd_event(events.GIT_EVENT, { "User FugitiveChanged" }, 200)
events.define_event(events.GIT_STATUS_CHANGED, { debounce_frequency = 0 })
events_setup = true

Expand Down
8 changes: 2 additions & 6 deletions lua/neo-tree/sources/filesystem/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,7 @@ M.follow = function(callback, force_show)
if utils.is_floating() then
return false
end
utils.debounce("neo-tree-follow", function()
return follow_internal(callback, force_show)
end, 100, utils.debounce_strategy.CALL_LAST_ONLY)
return follow_internal(callback, force_show)
end

local fs_stat = (vim.uv or vim.loop).fs_stat
Expand Down Expand Up @@ -189,9 +187,7 @@ end
M.navigate = function(state, path, path_to_reveal, callback, async)
state._ready = false
log.trace("navigate", path, path_to_reveal, async)
utils.debounce("filesystem_navigate", function()
M._navigate_internal(state, path, path_to_reveal, callback, async)
end, 100, utils.debounce_strategy.CALL_FIRST_AND_LAST)
M._navigate_internal(state, path, path_to_reveal, callback, async)
end

M.reset_search = function(state, refresh, open_current_node)
Expand Down
8 changes: 5 additions & 3 deletions lua/neo-tree/sources/filesystem/lib/fs_scan.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ local on_directory_loaded = function(context, dir_path)
local root = context.folders[dir_path]
if root then
local target_path = root.is_link and root.link_to or root.path
local fs_watch_callback = vim.schedule_wrap(function(err, fname)
local fs_watch_callback = function(err, fname, ev)
if err then
log.error("file_event_callback: ", err)
return
Expand All @@ -36,9 +36,11 @@ local on_directory_loaded = function(context, dir_path)
-- don't fire events for nodes that are designated as "never show"
return
else
events.fire_event(events.FS_EVENT, { afile = target_path })
vim.schedule(function()
events.fire_event(events.FS_EVENT, { afile = target_path, events = ev })
end)
end
end)
end

log.trace("Adding fs watcher for ", target_path)
fs_watch.watch_folder(target_path, fs_watch_callback)
Expand Down
14 changes: 8 additions & 6 deletions lua/neo-tree/sources/filesystem/lib/fs_watch.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ end

---Watch a directory for changes to it's children. Not recursive.
---@param path string The directory to watch.
---@param custom_callback? function The callback to call when a change is detected.
---@param custom_callback? uv.fs_event_start.callback The callback to call when a change is detected.
---@param allow_git_watch? boolean Allow watching of git folders.
M.watch_folder = function(path, custom_callback, allow_git_watch)
if not allow_git_watch then
Expand All @@ -64,7 +64,7 @@ M.watch_folder = function(path, custom_callback, allow_git_watch)
if h == nil then
log.trace("Starting new fs watch on: ", path)
local callback = custom_callback
or vim.schedule_wrap(function(err, fname)
or vim.schedule_wrap(function(err, fname, ev)
if fname and fname:match("^%.null[-]ls_.+") then
-- null-ls temp file: https://github.com/jose-elias-alvarez/null-ls.nvim/pull/1075
return
Expand All @@ -73,7 +73,7 @@ M.watch_folder = function(path, custom_callback, allow_git_watch)
log.error("file_event_callback: ", err)
return
end
events.fire_event(events.FS_EVENT, { afile = path })
events.fire_event(events.FS_EVENT, { afile = path, events = ev })
end)
h = {
handle = uv.new_fs_event(),
Expand All @@ -93,7 +93,7 @@ end
M.watch_git_index = function(path, async)
local function watch_git_folder(git_folder, git_root)
if git_folder then
local git_event_callback = vim.schedule_wrap(function(err, fname)
local git_event_callback = function(err, fname, ev)
if fname and fname:match("^.+%.lock$") then
return
end
Expand All @@ -105,8 +105,10 @@ M.watch_git_index = function(path, async)
log.error("git_event_callback: ", err)
return
end
events.fire_event(events.GIT_EVENT, { path = fname, repository = git_root })
end)
vim.schedule(function()
events.fire_event(events.GIT_EVENT, { path = fname, repository = git_root, events = ev })
end)
end

M.watch_folder(git_folder, git_event_callback, true)
end
Expand Down
12 changes: 7 additions & 5 deletions lua/neo-tree/sources/manager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -514,14 +514,16 @@ M.refresh = function(source_name, callback)
callback = nil
end
local current_tabid = vim.api.nvim_get_current_tabpage()
log.trace(source_name, "refresh")
for i = 1, #all_states, 1 do
local state = all_states[i]
if state.tabid == current_tabid and state.path and renderer.window_exists(state) then
local success, err = pcall(M.navigate, state, state.path, nil, callback)
if not success then
log.error(err)
end
utils.debounce("refresh " .. source_name, function()
log.trace(source_name, "refresh")
local success, err = pcall(M.navigate, state, state.path, nil, callback)
if not success then
log.error(err)
end
end, 800, utils.debounce_strategy.CALL_FIRST_AND_LAST)
else
state.dirty = true
end
Expand Down
Loading