fix: use object as config handler

This commit is contained in:
Pocco81
2022-08-06 13:02:17 -05:00
parent d37a766858
commit 1d2c03cbd0
4 changed files with 52 additions and 49 deletions
+38 -35
View File
@@ -1,43 +1,46 @@
local config = {} Config = {
opts = {
enabled = true, -- start auto-save when the plugin is loaded (i.e. when your package manager loads it)
execution_message = {
message = function() -- message to print on save
return ("AutoSave: saved at " .. vim.fn.strftime("%H:%M:%S"))
end,
dim = 0.18, -- dim the color of `message`
cleaning_interval = 1250, -- (milliseconds) automatically clean MsgArea after displaying `message`. See :h MsgArea
},
trigger_events = { "InsertLeave", "TextChanged" }, -- vim events that trigger auto-save. See :h events
-- function that determines whether to save the current buffer or not
-- return true: if buffer is ok to be saved
-- return false: if it's not ok to be saved
condition = function(buf)
local fn = vim.fn
local utils = require("auto-save.utils.data")
config.options = { if fn.getbufvar(buf, "&modifiable") == 1 and
enabled = true, -- start auto-save when the plugin is loaded (i.e. when your package manager loads it) utils.not_in(fn.getbufvar(buf, "&filetype"), {}) then
execution_message = { return true -- met condition(s), can save
message = function() -- message to print on save end
return ("AutoSave: saved at " .. vim.fn.strftime("%H:%M:%S")) return false -- can't save
end, end,
dim = 0.18, -- dim the color of `message` write_all_buffers = false, -- write all buffers when the current one meets `condition`
cleaning_interval = 1250, -- (milliseconds) automatically clean MsgArea after displaying `message`. See :h MsgArea debounce_delay = 135, -- saves the file at most every `debounce_delay` milliseconds
callbacks = { -- functions to be executed at different intervals
enabling = nil, -- ran when enabling auto-save
disabling = nil, -- ran when disabling auto-save
before_asserting_save = nil, -- ran before checking `condition`
before_saving = nil, -- ran before doing the actual save
after_saving = nil, -- ran after doing the actual save
},
}, },
trigger_events = {"InsertLeave", "TextChanged"}, -- vim events that trigger auto-save. See :h events
-- function that determines whether to save the current buffer or not
-- return true: if buffer is ok to be saved
-- return false: if it's not ok to be saved
condition = function(buf)
local fn = vim.fn
local utils = require("auto-save.utils.data")
if
fn.getbufvar(buf, "&modifiable") == 1 or
utils.not_in(fn.getbufvar(buf, "&filetype"), {}) then
return true -- met condition(s), can save
end
return false -- can't save
end,
write_all_buffers = false, -- write all buffers when the current one meets `condition`
debounce_delay = 135, -- saves the file at most every `debounce_delay` milliseconds
callbacks = { -- functions to be executed at different intervals
enabling = nil, -- ran when enabling auto-save
disabling = nil, -- ran when disabling auto-save
before_asserting_save = nil, -- ran before checking `condition`
before_saving = nil, -- ran before doing the actual save
after_saving = nil -- ran after doing the actual save
}
} }
function config.set_options(opts) function Config:set_options(opts)
opts = opts or {} opts = opts or {}
config.options = vim.tbl_deep_extend("keep", opts, config.options) self.opts = vim.tbl_deep_extend("keep", opts, self.opts)
end end
return config function Config:get_options()
return self.opts
end
return Config
+12 -12
View File
@@ -1,6 +1,6 @@
local M = {} local M = {}
local cnf = require("auto-save.config").options local cnf = require("auto-save.config")
local callback = require("auto-save.utils.data").do_callback local callback = require("auto-save.utils.data").do_callback
local colors = require("auto-save.utils.colors") local colors = require("auto-save.utils.colors")
local echo = require("auto-save.utils.echo") local echo = require("auto-save.utils.echo")
@@ -54,7 +54,7 @@ function M.save(buf)
callback("before_asserting_save") callback("before_asserting_save")
if cnf.condition(buf) == false then if cnf.opts.condition(buf) == false then
return return
end end
@@ -68,7 +68,7 @@ function M.save(buf)
return return
end end
if cnf.write_all_buffers then if cnf.opts.write_all_buffers then
cmd("silent! wall") cmd("silent! wall")
else else
api.nvim_buf_call(buf, function () cmd("silent! write") end) api.nvim_buf_call(buf, function () cmd("silent! write") end)
@@ -76,10 +76,10 @@ function M.save(buf)
callback("after_saving") callback("after_saving")
api.nvim_echo({ { (type(cnf.execution_message.message) == "function" and cnf.execution_message.message() or cnf.execution_message.message), AUTO_SAVE_COLOR } }, true, {}) api.nvim_echo({ { (type(cnf.opts.execution_message.message) == "function" and cnf.opts.execution_message.message() or cnf.opts.execution_message.message), AUTO_SAVE_COLOR } }, true, {})
if cnf.execution_message.cleaning_interval > 0 then if cnf.opts.execution_message.cleaning_interval > 0 then
fn.timer_start( fn.timer_start(
cnf.execution_message.cleaning_interval, cnf.opts.execution_message.cleaning_interval,
function() function()
cmd([[echon '']]) cmd([[echon '']])
end end
@@ -87,7 +87,7 @@ function M.save(buf)
end end
end end
local save_func = (cnf.debounce_delay > 0 and debounce(M.save, cnf.debounce_delay) or M.save) local save_func = (cnf.opts.debounce_delay > 0 and debounce(M.save, cnf.opts.debounce_delay) or M.save)
local function perform_save() local function perform_save()
g.auto_save_abort = false g.auto_save_abort = false
@@ -95,7 +95,7 @@ local function perform_save()
end end
function M.on() function M.on()
api.nvim_create_autocmd(cnf.trigger_events, { api.nvim_create_autocmd(cnf.opts.trigger_events, {
callback = function() callback = function()
perform_save() perform_save()
end, end,
@@ -106,13 +106,13 @@ function M.on()
api.nvim_create_autocmd({"VimEnter", "ColorScheme"}, { api.nvim_create_autocmd({"VimEnter", "ColorScheme"}, {
callback = function() callback = function()
vim.schedule(function() vim.schedule(function()
if cnf.execution_message.dim > 0 then if cnf.opts.execution_message.dim > 0 then
MSG_AREA = colors.get_hl("MsgArea") MSG_AREA = colors.get_hl("MsgArea")
MSG_AREA.background = (MSG_AREA.background or colors.get_hl("Normal")["background"]) MSG_AREA.background = (MSG_AREA.background or colors.get_hl("Normal")["background"])
local foreground = ( local foreground = (
o.background == "dark" and o.background == "dark" and
colors.darken((MSG_AREA.background or "#000000"), cnf.execution_message.dim, MSG_AREA.foreground) or colors.darken((MSG_AREA.background or "#000000"), cnf.opts.execution_message.dim, MSG_AREA.foreground) or
colors.lighten((MSG_AREA.background or "#ffffff"), cnf.execution_message.dim, MSG_AREA.foreground) colors.lighten((MSG_AREA.background or "#ffffff"), cnf.opts.execution_message.dim, MSG_AREA.foreground)
) )
colors.highlight("AutoSaveText", { fg = foreground }) colors.highlight("AutoSaveText", { fg = foreground })
@@ -149,7 +149,7 @@ function M.toggle()
end end
function M.setup(custom_opts) function M.setup(custom_opts)
require("auto-save.config").set_options(custom_opts) cnf:set_options(custom_opts)
end end
return M return M
+1 -1
View File
@@ -1,6 +1,6 @@
local M = {} local M = {}
local cnf = require("auto-save.config").options local cnf = require("auto-save.config").opts
function M.set_of(list) function M.set_of(list)
local set = {} local set = {}
+1 -1
View File
@@ -4,7 +4,7 @@ end
vim.g.loaded_auto_save = true vim.g.loaded_auto_save = true
local command = vim.api.nvim_create_user_command local command = vim.api.nvim_create_user_command
local cnf = require("auto-save.config").options local cnf = require("auto-save.config").opts
command("ASToggle", function() command("ASToggle", function()
require("auto-save").toggle() require("auto-save").toggle()