diff --git a/README.md b/README.md index 5ff468f..a05a43b 100644 --- a/README.md +++ b/README.md @@ -111,11 +111,10 @@ EOF write_all_buffers = false, -- write all buffers when the current one meets `condition` debounce_delay = 1000, -- delay after which a pending save is executed callbacks = { -- functions to be executed at different intervals - enabling = nil, -- ran when enabling auto-save - disabling = nil, -- ran when disabling auto-save before_saving = nil, -- ran before doing the actual save - after_saving = nil -- ran after doing the actual save - } + }, + -- log debug messages to 'auto-save.log' file in neovim cache directory, set to `true` to enable + debug = false, } ``` diff --git a/lua/auto-save/config.lua b/lua/auto-save/config.lua index 6f9ff6e..ce95514 100644 --- a/lua/auto-save/config.lua +++ b/lua/auto-save/config.lua @@ -22,11 +22,10 @@ Config = { write_all_buffers = false, -- write all buffers when the current one meets `condition` debounce_delay = 1000, -- delay after which a pending save is executed callbacks = { -- functions to be executed at different intervals - enabling = nil, -- ran when enabling auto-save - disabling = nil, -- ran when disabling auto-save before_saving = nil, -- ran before doing the actual save - after_saving = nil, -- ran after doing the actual save }, + -- log debug messages to 'auto-save.log' file in neovim cache directory, set to `true` to enable + debug = false, -- print debug messages, set to `true` to enable }, } diff --git a/lua/auto-save/init.lua b/lua/auto-save/init.lua index e825751..d0cd0a1 100644 --- a/lua/auto-save/init.lua +++ b/lua/auto-save/init.lua @@ -4,6 +4,7 @@ local cnf = require("auto-save.config") local callback = require("auto-save.utils.data").do_callback local colors = require("auto-save.utils.colors") local echo = require("auto-save.utils.echo") +local logger local autosave_running local api = vim.api local g = vim.g @@ -25,6 +26,8 @@ local function cancel_timer(buf) if timer ~= nil then timer:close() timers_by_buffer[buf] = nil + + logger.log(buf, "Timer canceled") end end @@ -37,6 +40,8 @@ local function debounce(lfn, duration) timers_by_buffer[buf] = nil end, duration) timers_by_buffer[buf] = timer + + logger.log(buf, "Timer started") end return inner_debounce end @@ -64,11 +69,15 @@ local function should_be_saved(buf) return cnf.opts.condition(buf) end + logger.log(buf, "Should save buffer") + return true end local function save(buf) if not api.nvim_buf_get_option(buf, "modified") then + logger.log(buf, "Abort saving buffer") + return end @@ -88,7 +97,7 @@ local function save(buf) end) end - callback("after_saving") + logger.log(buf, "Saved buffer") if cnf.opts.execution_message.enabled == true then echo_execution_message() @@ -169,7 +178,6 @@ function M.on() group = "AutoSave", }) - callback("enabling") autosave_running = true end @@ -178,7 +186,6 @@ function M.off() clear = true, }) - callback("disabling") autosave_running = false end @@ -194,6 +201,7 @@ end function M.setup(custom_opts) cnf:set_options(custom_opts) + logger = require("auto-save.utils.logging").new(cnf:get_options()) end return M diff --git a/lua/auto-save/utils/logging.lua b/lua/auto-save/utils/logging.lua new file mode 100644 index 0000000..8ffa16b --- /dev/null +++ b/lua/auto-save/utils/logging.lua @@ -0,0 +1,42 @@ +-- inspired from https://github.com/nvim-lua/plenary.nvim/blob/master/lua/plenary/log.lua + +local M = {} + +local outfile = string.format("%s/auto-save.log", vim.api.nvim_call_function("stdpath", { "cache" })) + +-- it could be that the directory of the file does not exist +-- this would require further checks, see https://github.com/nvim-lua/plenary.nvim/blob/master/lua/plenary/log.lua#L138 +--- @param message string +local write_to_outfile = function(message) + local f = assert(io.open(outfile, "a")) + f:write(message) + f:close() +end + +M.new = function(options) + local enabled = options.debug + + --- @param buf number | nil + --- @param message string + local log = function(buf, message) + if not enabled then + return + end + + local log_message + if buf ~= nil then + local filename = vim.api.nvim_buf_get_name(buf) + log_message = string.format("[%s] [%s] - %s\n", os.date(), filename, message) + else + log_message = string.format("[%s] - %s\n", os.date(), message) + end + + write_to_outfile(log_message) + end + + return { + log = log, + } +end + +return M