debounce write to improve responsiveness

This commit is contained in:
Tim Macfarlane
2021-07-15 10:30:27 +02:00
parent de73572d4e
commit 5a32f8ec48
3 changed files with 39 additions and 7 deletions
+7 -3
View File
@@ -130,7 +130,8 @@ conditions = {
}, },
write_all_buffers = false, write_all_buffers = false,
on_off_commands = false, on_off_commands = false,
clean_command_line_interval = 0 clean_command_line_interval = 0,
debounce_delay = 140
``` ```
The way you setup the settings on your config varies on whether you are using vimL for this or Lua. The way you setup the settings on your config varies on whether you are using vimL for this or Lua.
@@ -154,7 +155,8 @@ autosave.setup(
}, },
write_all_buffers = false, write_all_buffers = false,
on_off_commands = true, on_off_commands = true,
clean_command_line_interval = 2500 clean_command_line_interval = 2500,
debounce_delay = 140
} }
) )
``` ```
@@ -182,7 +184,8 @@ autosave.setup(
}, },
write_all_buffers = false, write_all_buffers = false,
on_off_commands = true, on_off_commands = true,
clean_command_line_interval = 2500 clean_command_line_interval = 2500,
debounce_delay = 140
} }
) )
EOF EOF
@@ -218,6 +221,7 @@ Although settings already have self-explanatory names, here is where you can fin
+ `write_all_buffers`: (Boolean) if true, writes to all modifiable buffers that meet the `conditions`. + `write_all_buffers`: (Boolean) if true, writes to all modifiable buffers that meet the `conditions`.
+ `on_off_commands`: (Boolean) if true, enables extra commands for toggling the plugin on and off (`:ASOn` and `:ASOff`). + `on_off_commands`: (Boolean) if true, enables extra commands for toggling the plugin on and off (`:ASOn` and `:ASOff`).
+ `clean_command_line_interval` (Integer) if greater than 0, cleans the command line after *x* amount of milliseconds after printing the `execution_message`. + `clean_command_line_interval` (Integer) if greater than 0, cleans the command line after *x* amount of milliseconds after printing the `execution_message`.
+ `debounce_delay` (Integer) if greater than 0, saves the file at most every `debounce_delay` milliseconds. This can improve editing performance. If 0 then saves are performed immediately on each edit. Default `140`, which is just long enough to reduce unnecessary saves, but short enough that you don't notice the delay.
## Conditions ## Conditions
These are the conditions that every file must meet so that it can be saved. If every file to be auto-saved doesn't meet all of the conditions it won't be saved. These are the conditions that every file must meet so that it can be saved. If every file to be auto-saved doesn't meet all of the conditions it won't be saved.
+2 -1
View File
@@ -11,7 +11,8 @@ config.options = {
}, },
write_all_buffers = false, write_all_buffers = false,
on_off_commands = false, on_off_commands = false,
clean_command_line_interval = 0 clean_command_line_interval = 0,
debounce_delay = 140
} }
function config.set_options(opts) function config.set_options(opts)
+30 -3
View File
@@ -34,6 +34,8 @@ local function actual_save()
if (get_modified() == nil or get_modified() == false) then if (get_modified() == nil or get_modified() == false) then
set_modified(true) set_modified(true)
end end
M.message_and_interval()
end end
end end
@@ -94,9 +96,28 @@ function M.message_and_interval()
end end
end end
local function debounce(fn, duration)
local queued = false
local function inner_debounce()
if not queued then
vim.defer_fn(
function()
queued = false
fn()
end,
duration
)
queued = true
end
end
return inner_debounce
end
function M.do_save() function M.do_save()
if (assert_return(assert_user_conditions(), true)) then if (assert_return(assert_user_conditions(), true)) then
actual_save() M.debounced_save()
end end
end end
@@ -127,6 +148,12 @@ local function parse_events()
end end
function M.load_autocommands() function M.load_autocommands()
if opts["debounce_delay"] == false then
M.debounced_save = actual_save
else
M.debounced_save = debounce(actual_save, opts["debounce_delay"])
end
if (opts["write_all_buffers"] == false) then if (opts["write_all_buffers"] == false) then
api.nvim_exec( api.nvim_exec(
[[ [[
@@ -134,7 +161,7 @@ function M.load_autocommands()
au! au!
au ]] .. au ]] ..
parse_events() .. parse_events() ..
[[ * execute "lua require'autosave.modules.autocmds'.save()" | execute "lua require'autosave.modules.autocmds'.message_and_interval()" [[ * execute "lua require'autosave.modules.autocmds'.save()"
aug END aug END
]], ]],
false false
@@ -149,7 +176,7 @@ function M.load_autocommands()
parse_events() .. parse_events() ..
[[ * if !exists("g:autosave_changed") | let g:autosave_changed="t" | doautoall autosave_save ]] .. [[ * if !exists("g:autosave_changed") | let g:autosave_changed="t" | doautoall autosave_save ]] ..
event_1 .. event_1 ..
[[ | unlet g:autosave_changed | execute "lua require'autosave.modules.autocmds'.message_and_interval()" | else | execute "lua require'autosave.modules.autocmds'.save()" | endif [[ | unlet g:autosave_changed | else | execute "lua require'autosave.modules.autocmds'.save()" | endif
aug END aug END
]], ]],
false false