From 343bbfa98ac7f28a01ea4486465b5a4212247246 Mon Sep 17 00:00:00 2001 From: Oula Kuuva Date: Tue, 21 Mar 2023 08:44:30 +0200 Subject: [PATCH] style: autoformat all the things Namely: applied stylua to the whole repo and ran marksman on README.md. --- README.md | 138 ++++++++++---------- lua/auto-save/config.lua | 70 +++++------ lua/auto-save/init.lua | 222 ++++++++++++++++----------------- lua/auto-save/utils/colors.lua | 84 ++++++------- lua/auto-save/utils/data.lua | 24 ++-- lua/auto-save/utils/echo.lua | 38 +++--- plugin/auto-save.lua | 6 +- 7 files changed, 291 insertions(+), 291 deletions(-) diff --git a/README.md b/README.md index 5ef5185..4bfe935 100644 --- a/README.md +++ b/README.md @@ -1,18 +1,18 @@

-

🧶 auto-save.nvim

+

🧶 auto-save.nvim

- Automatically save your changes in NeoVim + Automatically save your changes in NeoVim

- - Stars - - Issues - - Repo Size + + Stars + + Issues + + Repo Size

  @@ -21,9 +21,9 @@ - automatically save your changes so the world doesn't collapse - highly customizable: - - conditionals to assert whether to save or not - - execution message (it can be dimmed and personalized) - - events that trigger auto-save + - conditionals to assert whether to save or not + - execution message (it can be dimmed and personalized) + - events that trigger auto-save - debounce the save with a delay - multiple callbacks - automatically clean the message area @@ -41,49 +41,49 @@ Install the plugin with your favourite package manager:
- Lazy.nvim + Lazy.nvim ```lua { - "okuuva/auto-save.nvim", - cmd = "ASToggle", -- optional for lazy loading on command - event = { "InsertLeave", "TextChanged" } -- optional for lazy loading on trigger events - opts = { - -- your config goes here - -- or just leave it empty :) - }, + "okuuva/auto-save.nvim", + cmd = "ASToggle", -- optional for lazy loading on command + event = { "InsertLeave", "TextChanged" } -- optional for lazy loading on trigger events + opts = { + -- your config goes here + -- or just leave it empty :) + }, }, ```
- Packer.nvim + Packer.nvim ```lua use({ - "okuuva/auto-save.nvim", - config = function() - require("auto-save").setup { - -- your config goes here - -- or just leave it empty :) - } - end, + "okuuva/auto-save.nvim", + config = function() + require("auto-save").setup { + -- your config goes here + -- or just leave it empty :) + } + end, }) ```
- vim-plug + vim-plug ```vim Plug 'okuuva/auto-save.nvim' lua << EOF - require("auto-save").setup { - -- your config goes here - -- or just leave it empty :) - } + require("auto-save").setup { + -- your config goes here + -- or just leave it empty :) + } EOF ``` @@ -97,39 +97,39 @@ EOF ```lua { - enabled = true, -- start auto-save when the plugin is loaded (i.e. when your package manager loads it) - execution_message = { - enabled = true, - 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") + enabled = true, -- start auto-save when the plugin is loaded (i.e. when your package manager loads it) + execution_message = { + enabled = true, + 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") - 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, - 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 - } + 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, + 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 + } } ``` @@ -143,11 +143,11 @@ or as part of the `lazy.nvim` plugin spec: ```lua { - "okuuva/auto-save.nvim", - keys = { - { "n", ":ASToggle", desc = "Toggle auto-save" }, - }, - ... + "okuuva/auto-save.nvim", + keys = { + { "n", ":ASToggle", desc = "Toggle auto-save" }, + }, + ... }, ``` diff --git a/lua/auto-save/config.lua b/lua/auto-save/config.lua index 8fa5c88..bbfdbaf 100644 --- a/lua/auto-save/config.lua +++ b/lua/auto-save/config.lua @@ -1,46 +1,46 @@ Config = { - opts = { - enabled = true, -- start auto-save when the plugin is loaded (i.e. when your package manager loads it) - execution_message = { - enabled = true, - 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") + opts = { + enabled = true, -- start auto-save when the plugin is loaded (i.e. when your package manager loads it) + execution_message = { + enabled = true, + 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") - 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, - 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 - }, - }, + 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, + 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) - opts = opts or {} - self.opts = vim.tbl_deep_extend("keep", opts, self.opts) + opts = opts or {} + self.opts = vim.tbl_deep_extend("keep", opts, self.opts) end function Config:get_options() - return self.opts + return self.opts end return Config diff --git a/lua/auto-save/init.lua b/lua/auto-save/init.lua index bc07447..1445fa7 100644 --- a/lua/auto-save/init.lua +++ b/lua/auto-save/init.lua @@ -15,164 +15,164 @@ local BLACK = "#000000" local WHITE = "#ffffff" api.nvim_create_augroup("AutoSave", { - clear = true, + clear = true, }) local global_vars = {} local function set_buf_var(buf, name, value) - if buf == nil then - global_vars[name] = value - else - if api.nvim_buf_is_valid(buf) then - api.nvim_buf_set_var(buf, "autosave_" .. name, value) - end - end + if buf == nil then + global_vars[name] = value + else + if api.nvim_buf_is_valid(buf) then + api.nvim_buf_set_var(buf, "autosave_" .. name, value) + end + end end local function get_buf_var(buf, name) - if buf == nil then - return global_vars[name] - end - local success, mod = pcall(api.nvim_buf_get_var, buf, "autosave_" .. name) - return success and mod or nil + if buf == nil then + return global_vars[name] + end + local success, mod = pcall(api.nvim_buf_get_var, buf, "autosave_" .. name) + return success and mod or nil end local function debounce(lfn, duration) - local function inner_debounce() - local buf = api.nvim_get_current_buf() - if not get_buf_var(buf, "queued") then - vim.defer_fn(function() - set_buf_var(buf, "queued", false) - lfn(buf) - end, duration) - set_buf_var(buf, "queued", true) - end - end + local function inner_debounce() + local buf = api.nvim_get_current_buf() + if not get_buf_var(buf, "queued") then + vim.defer_fn(function() + set_buf_var(buf, "queued", false) + lfn(buf) + end, duration) + set_buf_var(buf, "queued", true) + end + end - return inner_debounce + return inner_debounce end local function echo_execution_message() - local msg = type(cnf.opts.execution_message.message) == "function" and cnf.opts.execution_message.message() - or cnf.opts.execution_message.message - api.nvim_echo({ { msg, AUTO_SAVE_COLOR } }, true, {}) - if cnf.opts.execution_message.cleaning_interval > 0 then - fn.timer_start(cnf.opts.execution_message.cleaning_interval, function() - cmd([[echon '']]) - end) - end + local msg = type(cnf.opts.execution_message.message) == "function" and cnf.opts.execution_message.message() + or cnf.opts.execution_message.message + api.nvim_echo({ { msg, AUTO_SAVE_COLOR } }, true, {}) + if cnf.opts.execution_message.cleaning_interval > 0 then + fn.timer_start(cnf.opts.execution_message.cleaning_interval, function() + cmd([[echon '']]) + end) + end end function M.save(buf) - buf = buf or api.nvim_get_current_buf() + buf = buf or api.nvim_get_current_buf() - callback("before_asserting_save") + callback("before_asserting_save") - if cnf.opts.condition(buf) == false then - return - end + if cnf.opts.condition(buf) == false then + return + end - if not api.nvim_buf_get_option(buf, "modified") then - return - end + if not api.nvim_buf_get_option(buf, "modified") then + return + end - callback("before_saving") + callback("before_saving") - if g.auto_save_abort == true then - return - end + if g.auto_save_abort == true then + return + end - if cnf.opts.write_all_buffers then - cmd("silent! wall") - else - api.nvim_buf_call(buf, function() - cmd("silent! write") - end) - end + if cnf.opts.write_all_buffers then + cmd("silent! wall") + else + api.nvim_buf_call(buf, function() + cmd("silent! write") + end) + end - callback("after_saving") + callback("after_saving") - if cnf.opts.execution_message.enabled == true then - echo_execution_message() - end + if cnf.opts.execution_message.enabled == true then + echo_execution_message() + end end local save_func = nil local function perform_save() - g.auto_save_abort = false - if save_func == nil then - save_func = (cnf.opts.debounce_delay > 0 and debounce(M.save, cnf.opts.debounce_delay) or M.save) - end - save_func() + g.auto_save_abort = false + if save_func == nil then + save_func = (cnf.opts.debounce_delay > 0 and debounce(M.save, cnf.opts.debounce_delay) or M.save) + end + save_func() end function M.on() - api.nvim_create_autocmd(cnf.opts.trigger_events, { - callback = function() - perform_save() - end, - pattern = "*", - group = "AutoSave", - }) + api.nvim_create_autocmd(cnf.opts.trigger_events, { + callback = function() + perform_save() + end, + pattern = "*", + group = "AutoSave", + }) - api.nvim_create_autocmd({ "VimEnter", "ColorScheme", "UIEnter" }, { - callback = function() - vim.schedule(function() - if cnf.opts.execution_message.dim > 0 then - MSG_AREA = colors.get_hl("MsgArea") - if MSG_AREA.foreground ~= nil then - MSG_AREA.background = (MSG_AREA.background or colors.get_hl("Normal")["background"]) - local foreground = ( - o.background == "dark" - and colors.darken( - (MSG_AREA.background or BLACK), - cnf.opts.execution_message.dim, - MSG_AREA.foreground or BLACK - ) - or colors.lighten( - (MSG_AREA.background or WHITE), - cnf.opts.execution_message.dim, - MSG_AREA.foreground or WHITE - ) - ) + api.nvim_create_autocmd({ "VimEnter", "ColorScheme", "UIEnter" }, { + callback = function() + vim.schedule(function() + if cnf.opts.execution_message.dim > 0 then + MSG_AREA = colors.get_hl("MsgArea") + if MSG_AREA.foreground ~= nil then + MSG_AREA.background = (MSG_AREA.background or colors.get_hl("Normal")["background"]) + local foreground = ( + o.background == "dark" + and colors.darken( + (MSG_AREA.background or BLACK), + cnf.opts.execution_message.dim, + MSG_AREA.foreground or BLACK + ) + or colors.lighten( + (MSG_AREA.background or WHITE), + cnf.opts.execution_message.dim, + MSG_AREA.foreground or WHITE + ) + ) - colors.highlight("AutoSaveText", { fg = foreground }) - AUTO_SAVE_COLOR = "AutoSaveText" - end - end - end) - end, - pattern = "*", - group = "AutoSave", - }) + colors.highlight("AutoSaveText", { fg = foreground }) + AUTO_SAVE_COLOR = "AutoSaveText" + end + end + end) + end, + pattern = "*", + group = "AutoSave", + }) - callback("enabling") - autosave_running = true + callback("enabling") + autosave_running = true end function M.off() - api.nvim_create_augroup("AutoSave", { - clear = true, - }) + api.nvim_create_augroup("AutoSave", { + clear = true, + }) - callback("disabling") - autosave_running = false + callback("disabling") + autosave_running = false end function M.toggle() - if autosave_running then - M.off() - echo("off") - else - M.on() - echo("on") - end + if autosave_running then + M.off() + echo("off") + else + M.on() + echo("on") + end end function M.setup(custom_opts) - cnf:set_options(custom_opts) + cnf:set_options(custom_opts) end return M diff --git a/lua/auto-save/utils/colors.lua b/lua/auto-save/utils/colors.lua index d73c30c..827eeea 100644 --- a/lua/auto-save/utils/colors.lua +++ b/lua/auto-save/utils/colors.lua @@ -1,70 +1,70 @@ local M = {} ---@param hex_str string hexadecimal value of a color local hex_to_rgb = function(hex_str) - local hex = "[abcdef0-9][abcdef0-9]" - local pat = "^#(" .. hex .. ")(" .. hex .. ")(" .. hex .. ")$" - hex_str = string.lower(hex_str) + local hex = "[abcdef0-9][abcdef0-9]" + local pat = "^#(" .. hex .. ")(" .. hex .. ")(" .. hex .. ")$" + hex_str = string.lower(hex_str) - assert(string.find(hex_str, pat) ~= nil, "hex_to_rgb: invalid hex_str: " .. tostring(hex_str)) + assert(string.find(hex_str, pat) ~= nil, "hex_to_rgb: invalid hex_str: " .. tostring(hex_str)) - local red, green, blue = string.match(hex_str, pat) - return { tonumber(red, 16), tonumber(green, 16), tonumber(blue, 16) } + local red, green, blue = string.match(hex_str, pat) + return { tonumber(red, 16), tonumber(green, 16), tonumber(blue, 16) } end function M.highlight(group, color, force) - if color.link then - vim.api.nvim_set_hl(0, group, { - link = color.link, - }) - else - if color.style then - for _, style in ipairs(color.style) do - color[style] = true - end - end - color.style = nil - if force then - vim.cmd("hi " .. group .. " guifg=" .. (color.fg or "NONE") .. " guibg=" .. (color.bg or "NONE")) - return - end - vim.api.nvim_set_hl(0, group, color) - end + if color.link then + vim.api.nvim_set_hl(0, group, { + link = color.link, + }) + else + if color.style then + for _, style in ipairs(color.style) do + color[style] = true + end + end + color.style = nil + if force then + vim.cmd("hi " .. group .. " guifg=" .. (color.fg or "NONE") .. " guibg=" .. (color.bg or "NONE")) + return + end + vim.api.nvim_set_hl(0, group, color) + end end function M.get_hl(name) - local ok, hl = pcall(vim.api.nvim_get_hl_by_name, name, true) - if not ok then - return - end - for _, key in pairs({ "foreground", "background", "special" }) do - if hl[key] then - hl[key] = string.format("#%06x", hl[key]) - end - end - return hl + local ok, hl = pcall(vim.api.nvim_get_hl_by_name, name, true) + if not ok then + return + end + for _, key in pairs({ "foreground", "background", "special" }) do + if hl[key] then + hl[key] = string.format("#%06x", hl[key]) + end + end + return hl end ---@param fg string forecrust color ---@param bg string background color ---@param alpha number number between 0 and 1. 0 results in bg, 1 results in fg function M.blend(fg, bg, alpha) - bg = hex_to_rgb(bg) - fg = hex_to_rgb(fg) + bg = hex_to_rgb(bg) + fg = hex_to_rgb(fg) - local blendChannel = function(i) - local ret = (alpha * fg[i] + ((1 - alpha) * bg[i])) - return math.floor(math.min(math.max(0, ret), 255) + 0.5) - end + local blendChannel = function(i) + local ret = (alpha * fg[i] + ((1 - alpha) * bg[i])) + return math.floor(math.min(math.max(0, ret), 255) + 0.5) + end - return string.format("#%02X%02X%02X", blendChannel(1), blendChannel(2), blendChannel(3)) + return string.format("#%02X%02X%02X", blendChannel(1), blendChannel(2), blendChannel(3)) end function M.darken(hex, amount, bg) - return M.blend(hex, bg or M.bg, math.abs(amount)) + return M.blend(hex, bg or M.bg, math.abs(amount)) end function M.lighten(hex, amount, fg) - return M.blend(hex, fg or M.fg, math.abs(amount)) + return M.blend(hex, fg or M.fg, math.abs(amount)) end return M diff --git a/lua/auto-save/utils/data.lua b/lua/auto-save/utils/data.lua index c211195..68579bc 100644 --- a/lua/auto-save/utils/data.lua +++ b/lua/auto-save/utils/data.lua @@ -1,25 +1,25 @@ local M = {} function M.set_of(list) - local set = {} - for i = 1, #list do - set[list[i]] = true - end - return set + local set = {} + for i = 1, #list do + set[list[i]] = true + end + return set end function M.not_in(var, arr) - if M.set_of(arr)[var] == nil then - return true - end + if M.set_of(arr)[var] == nil then + return true + end end function M.do_callback(callback_name) - local cnf = require("auto-save.config").opts + local cnf = require("auto-save.config").opts - if type(cnf.callbacks[callback_name]) == "function" then - cnf.callbacks[callback_name]() - end + if type(cnf.callbacks[callback_name]) == "function" then + cnf.callbacks[callback_name]() + end end return M diff --git a/lua/auto-save/utils/echo.lua b/lua/auto-save/utils/echo.lua index 0d1bbc2..c6e03a2 100644 --- a/lua/auto-save/utils/echo.lua +++ b/lua/auto-save/utils/echo.lua @@ -1,25 +1,25 @@ local TITLE = "auto-save" return function(msg, kind) - local has_notify_plugin = pcall(require, "notify") - local level = {} + local has_notify_plugin = pcall(require, "notify") + local level = {} - if kind == "error" then - level.log = vim.log.levels.ERROR - level.type = "error" - elseif kind == "warn" then - level.log = vim.log.levels.WARN - level.type = "error" - else - level.log = kind or vim.log.levels.INFO - level.type = "info" - end + if kind == "error" then + level.log = vim.log.levels.ERROR + level.type = "error" + elseif kind == "warn" then + level.log = vim.log.levels.WARN + level.type = "error" + else + level.log = kind or vim.log.levels.INFO + level.type = "info" + end - if has_notify_plugin then - vim.notify(msg, level.log, { - title = TITLE, - }) - else - vim.notify(("%s: %s"):format(TITLE, msg), level.log) - end + if has_notify_plugin then + vim.notify(msg, level.log, { + title = TITLE, + }) + else + vim.notify(("%s: %s"):format(TITLE, msg), level.log) + end end diff --git a/plugin/auto-save.lua b/plugin/auto-save.lua index a4dbf27..003d8ae 100644 --- a/plugin/auto-save.lua +++ b/plugin/auto-save.lua @@ -1,5 +1,5 @@ if vim.g.loaded_auto_save then - return + return end vim.g.loaded_auto_save = true @@ -7,9 +7,9 @@ local command = vim.api.nvim_create_user_command local cnf = require("auto-save.config").opts command("ASToggle", function() - require("auto-save").toggle() + require("auto-save").toggle() end, {}) if cnf.enabled then - require("auto-save").on() + require("auto-save").on() end