diff --git a/README.md b/README.md index 845be4f..efeb7b0 100644 --- a/README.md +++ b/README.md @@ -130,7 +130,8 @@ conditions = { }, write_all_buffers = 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. @@ -154,7 +155,8 @@ autosave.setup( }, write_all_buffers = false, 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, on_off_commands = true, - clean_command_line_interval = 2500 + clean_command_line_interval = 2500, + debounce_delay = 140 } ) 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`. + `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`. ++ `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 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. diff --git a/lua/autosave/config.lua b/lua/autosave/config.lua index 2a18283..9d0ab62 100644 --- a/lua/autosave/config.lua +++ b/lua/autosave/config.lua @@ -11,7 +11,8 @@ config.options = { }, write_all_buffers = false, on_off_commands = false, - clean_command_line_interval = 0 + clean_command_line_interval = 0, + debounce_delay = 140 } function config.set_options(opts) diff --git a/lua/autosave/modules/autocmds.lua b/lua/autosave/modules/autocmds.lua index 89bcaba..b40247e 100644 --- a/lua/autosave/modules/autocmds.lua +++ b/lua/autosave/modules/autocmds.lua @@ -34,6 +34,8 @@ local function actual_save() if (get_modified() == nil or get_modified() == false) then set_modified(true) end + + M.message_and_interval() end end @@ -94,9 +96,28 @@ function M.message_and_interval() 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() if (assert_return(assert_user_conditions(), true)) then - actual_save() + M.debounced_save() end end @@ -127,6 +148,12 @@ local function parse_events() end 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 api.nvim_exec( [[ @@ -134,7 +161,7 @@ function M.load_autocommands() au! au ]] .. 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 ]], false @@ -149,7 +176,7 @@ function M.load_autocommands() parse_events() .. [[ * if !exists("g:autosave_changed") | let g:autosave_changed="t" | doautoall autosave_save ]] .. 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 ]], false