From 1d2c03cbd06d97fd2c9bb33981f0de7983aef029 Mon Sep 17 00:00:00 2001 From: Pocco81 Date: Sat, 6 Aug 2022 13:02:17 -0500 Subject: [PATCH] fix: use object as config handler --- lua/auto-save/config.lua | 73 +++++++++++++++++++----------------- lua/auto-save/init.lua | 24 ++++++------ lua/auto-save/utils/data.lua | 2 +- plugin/auto-save.lua | 2 +- 4 files changed, 52 insertions(+), 49 deletions(-) diff --git a/lua/auto-save/config.lua b/lua/auto-save/config.lua index 758291f..2dfcc73 100644 --- a/lua/auto-save/config.lua +++ b/lua/auto-save/config.lua @@ -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 diff --git a/lua/auto-save/init.lua b/lua/auto-save/init.lua index d0d7d5f..71ae9c1 100644 --- a/lua/auto-save/init.lua +++ b/lua/auto-save/init.lua @@ -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 diff --git a/lua/auto-save/utils/data.lua b/lua/auto-save/utils/data.lua index 0e91e4e..943db60 100644 --- a/lua/auto-save/utils/data.lua +++ b/lua/auto-save/utils/data.lua @@ -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 = {} diff --git a/plugin/auto-save.lua b/plugin/auto-save.lua index 8a05953..af43703 100644 --- a/plugin/auto-save.lua +++ b/plugin/auto-save.lua @@ -4,7 +4,7 @@ end vim.g.loaded_auto_save = true 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() require("auto-save").toggle()