mirror of
https://github.com/zoriya/auto-save.nvim.git
synced 2025-12-06 06:36:11 +00:00
fix: use object as config handler
This commit is contained in:
@@ -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 = {
|
||||
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"))
|
||||
if fn.getbufvar(buf, "&modifiable") == 1 and
|
||||
utils.not_in(fn.getbufvar(buf, "&filetype"), {}) then
|
||||
return true -- met condition(s), can save
|
||||
end
|
||||
return false -- can't save
|
||||
end,
|
||||
dim = 0.18, -- dim the color of `message`
|
||||
cleaning_interval = 1250, -- (milliseconds) automatically clean MsgArea after displaying `message`. See :h MsgArea
|
||||
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
|
||||
},
|
||||
},
|
||||
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 {}
|
||||
config.options = vim.tbl_deep_extend("keep", opts, config.options)
|
||||
self.opts = vim.tbl_deep_extend("keep", opts, self.opts)
|
||||
end
|
||||
|
||||
return config
|
||||
function Config:get_options()
|
||||
return self.opts
|
||||
end
|
||||
|
||||
return Config
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
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 colors = require("auto-save.utils.colors")
|
||||
local echo = require("auto-save.utils.echo")
|
||||
@@ -54,7 +54,7 @@ function M.save(buf)
|
||||
|
||||
callback("before_asserting_save")
|
||||
|
||||
if cnf.condition(buf) == false then
|
||||
if cnf.opts.condition(buf) == false then
|
||||
return
|
||||
end
|
||||
|
||||
@@ -68,7 +68,7 @@ function M.save(buf)
|
||||
return
|
||||
end
|
||||
|
||||
if cnf.write_all_buffers then
|
||||
if cnf.opts.write_all_buffers then
|
||||
cmd("silent! wall")
|
||||
else
|
||||
api.nvim_buf_call(buf, function () cmd("silent! write") end)
|
||||
@@ -76,10 +76,10 @@ function M.save(buf)
|
||||
|
||||
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, {})
|
||||
if cnf.execution_message.cleaning_interval > 0 then
|
||||
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.opts.execution_message.cleaning_interval > 0 then
|
||||
fn.timer_start(
|
||||
cnf.execution_message.cleaning_interval,
|
||||
cnf.opts.execution_message.cleaning_interval,
|
||||
function()
|
||||
cmd([[echon '']])
|
||||
end
|
||||
@@ -87,7 +87,7 @@ function M.save(buf)
|
||||
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()
|
||||
g.auto_save_abort = false
|
||||
@@ -95,7 +95,7 @@ local function perform_save()
|
||||
end
|
||||
|
||||
function M.on()
|
||||
api.nvim_create_autocmd(cnf.trigger_events, {
|
||||
api.nvim_create_autocmd(cnf.opts.trigger_events, {
|
||||
callback = function()
|
||||
perform_save()
|
||||
end,
|
||||
@@ -106,13 +106,13 @@ function M.on()
|
||||
api.nvim_create_autocmd({"VimEnter", "ColorScheme"}, {
|
||||
callback = 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.background = (MSG_AREA.background or colors.get_hl("Normal")["background"])
|
||||
local foreground = (
|
||||
o.background == "dark" and
|
||||
colors.darken((MSG_AREA.background or "#000000"), cnf.execution_message.dim, MSG_AREA.foreground) or
|
||||
colors.lighten((MSG_AREA.background or "#ffffff"), cnf.execution_message.dim, MSG_AREA.foreground)
|
||||
colors.darken((MSG_AREA.background or "#000000"), cnf.opts.execution_message.dim, MSG_AREA.foreground) or
|
||||
colors.lighten((MSG_AREA.background or "#ffffff"), cnf.opts.execution_message.dim, MSG_AREA.foreground)
|
||||
)
|
||||
|
||||
colors.highlight("AutoSaveText", { fg = foreground })
|
||||
@@ -149,7 +149,7 @@ function M.toggle()
|
||||
end
|
||||
|
||||
function M.setup(custom_opts)
|
||||
require("auto-save.config").set_options(custom_opts)
|
||||
cnf:set_options(custom_opts)
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
local M = {}
|
||||
|
||||
local cnf = require("auto-save.config").options
|
||||
local cnf = require("auto-save.config").opts
|
||||
|
||||
function M.set_of(list)
|
||||
local set = {}
|
||||
|
||||
Reference in New Issue
Block a user