style: autoformat all the things

Namely: applied stylua to the whole repo and ran marksman on README.md.
This commit is contained in:
Oula Kuuva
2023-03-21 08:44:30 +02:00
committed by okuuva
parent 14bc188b5e
commit 343bbfa98a
7 changed files with 291 additions and 291 deletions

138
README.md
View File

@@ -1,18 +1,18 @@
<p align="center"> <p align="center">
<h2 align="center">🧶 auto-save.nvim</h2> <h2 align="center">🧶 auto-save.nvim</h2>
</p> </p>
<p align="center"> <p align="center">
Automatically save your changes in NeoVim Automatically save your changes in NeoVim
</p> </p>
<p align="center"> <p align="center">
<a href="https://github.com/okuuva/auto-save.nvim/stargazers"> <a href="https://github.com/okuuva/auto-save.nvim/stargazers">
<img alt="Stars" src="https://img.shields.io/github/stars/okuuva/auto-save.nvim?style=for-the-badge&logo=starship&color=C9CBFF&logoColor=D9E0EE&labelColor=302D41"></a> <img alt="Stars" src="https://img.shields.io/github/stars/okuuva/auto-save.nvim?style=for-the-badge&logo=starship&color=C9CBFF&logoColor=D9E0EE&labelColor=302D41"></a>
<a href="https://github.com/okuuva/auto-save.nvim/issues"> <a href="https://github.com/okuuva/auto-save.nvim/issues">
<img alt="Issues" src="https://img.shields.io/github/issues/okuuva/auto-save.nvim?style=for-the-badge&logo=bilibili&color=F5E0DC&logoColor=D9E0EE&labelColor=302D41"></a> <img alt="Issues" src="https://img.shields.io/github/issues/okuuva/auto-save.nvim?style=for-the-badge&logo=bilibili&color=F5E0DC&logoColor=D9E0EE&labelColor=302D41"></a>
<a href="https://github.com/okuuva/auto-save.nvim"> <a href="https://github.com/okuuva/auto-save.nvim">
<img alt="Repo Size" src="https://img.shields.io/github/repo-size/okuuva/auto-save.nvim?color=%23DDB6F2&label=SIZE&logo=codesandbox&style=for-the-badge&logoColor=D9E0EE&labelColor=302D41"/></a> <img alt="Repo Size" src="https://img.shields.io/github/repo-size/okuuva/auto-save.nvim?color=%23DDB6F2&label=SIZE&logo=codesandbox&style=for-the-badge&logoColor=D9E0EE&labelColor=302D41"/></a>
</p> </p>
&nbsp; &nbsp;
@@ -21,9 +21,9 @@
- automatically save your changes so the world doesn't collapse - automatically save your changes so the world doesn't collapse
- highly customizable: - highly customizable:
- conditionals to assert whether to save or not - conditionals to assert whether to save or not
- execution message (it can be dimmed and personalized) - execution message (it can be dimmed and personalized)
- events that trigger auto-save - events that trigger auto-save
- debounce the save with a delay - debounce the save with a delay
- multiple callbacks - multiple callbacks
- automatically clean the message area - automatically clean the message area
@@ -41,49 +41,49 @@
Install the plugin with your favourite package manager: Install the plugin with your favourite package manager:
<details> <details>
<summary><a href="https://github.com/folke/lazy.nvim">Lazy.nvim</a></summary> <summary><a href="https://github.com/folke/lazy.nvim">Lazy.nvim</a></summary>
```lua ```lua
{ {
"okuuva/auto-save.nvim", "okuuva/auto-save.nvim",
cmd = "ASToggle", -- optional for lazy loading on command cmd = "ASToggle", -- optional for lazy loading on command
event = { "InsertLeave", "TextChanged" } -- optional for lazy loading on trigger events event = { "InsertLeave", "TextChanged" } -- optional for lazy loading on trigger events
opts = { opts = {
-- your config goes here -- your config goes here
-- or just leave it empty :) -- or just leave it empty :)
}, },
}, },
``` ```
</details> </details>
<details> <details>
<summary><a href="https://github.com/wbthomason/packer.nvim">Packer.nvim</a></summary> <summary><a href="https://github.com/wbthomason/packer.nvim">Packer.nvim</a></summary>
```lua ```lua
use({ use({
"okuuva/auto-save.nvim", "okuuva/auto-save.nvim",
config = function() config = function()
require("auto-save").setup { require("auto-save").setup {
-- your config goes here -- your config goes here
-- or just leave it empty :) -- or just leave it empty :)
} }
end, end,
}) })
``` ```
</details> </details>
<details> <details>
<summary><a href="https://github.com/junegunn/vim-plug">vim-plug</a></summary> <summary><a href="https://github.com/junegunn/vim-plug">vim-plug</a></summary>
```vim ```vim
Plug 'okuuva/auto-save.nvim' Plug 'okuuva/auto-save.nvim'
lua << EOF lua << EOF
require("auto-save").setup { require("auto-save").setup {
-- your config goes here -- your config goes here
-- or just leave it empty :) -- or just leave it empty :)
} }
EOF EOF
``` ```
@@ -97,39 +97,39 @@ EOF
```lua ```lua
{ {
enabled = true, -- start auto-save when the plugin is loaded (i.e. when your package manager loads it) enabled = true, -- start auto-save when the plugin is loaded (i.e. when your package manager loads it)
execution_message = { execution_message = {
enabled = true, enabled = true,
message = function() -- message to print on save message = function() -- message to print on save
return ("AutoSave: saved at " .. vim.fn.strftime("%H:%M:%S")) return ("AutoSave: saved at " .. vim.fn.strftime("%H:%M:%S"))
end, end,
dim = 0.18, -- dim the color of `message` dim = 0.18, -- dim the color of `message`
cleaning_interval = 1250, -- (milliseconds) automatically clean MsgArea after displaying `message`. See :h MsgArea 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 trigger_events = {"InsertLeave", "TextChanged"}, -- vim events that trigger auto-save. See :h events
-- function that determines whether to save the current buffer or not -- function that determines whether to save the current buffer or not
-- return true: if buffer is ok to be saved -- return true: if buffer is ok to be saved
-- return false: if it's not ok to be saved -- return false: if it's not ok to be saved
condition = function(buf) condition = function(buf)
local fn = vim.fn local fn = vim.fn
local utils = require("auto-save.utils.data") local utils = require("auto-save.utils.data")
if if
fn.getbufvar(buf, "&modifiable") == 1 and fn.getbufvar(buf, "&modifiable") == 1 and
utils.not_in(fn.getbufvar(buf, "&filetype"), {}) then utils.not_in(fn.getbufvar(buf, "&filetype"), {}) then
return true -- met condition(s), can save return true -- met condition(s), can save
end end
return false -- can't save return false -- can't save
end, end,
write_all_buffers = false, -- write all buffers when the current one meets `condition` 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 debounce_delay = 135, -- saves the file at most every `debounce_delay` milliseconds
callbacks = { -- functions to be executed at different intervals callbacks = { -- functions to be executed at different intervals
enabling = nil, -- ran when enabling auto-save enabling = nil, -- ran when enabling auto-save
disabling = nil, -- ran when disabling auto-save disabling = nil, -- ran when disabling auto-save
before_asserting_save = nil, -- ran before checking `condition` before_asserting_save = nil, -- ran before checking `condition`
before_saving = nil, -- ran before doing the actual save before_saving = nil, -- ran before doing the actual save
after_saving = nil -- ran after 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 ```lua
{ {
"okuuva/auto-save.nvim", "okuuva/auto-save.nvim",
keys = { keys = {
{ "<leader>n", ":ASToggle<CR>", desc = "Toggle auto-save" }, { "<leader>n", ":ASToggle<CR>", desc = "Toggle auto-save" },
}, },
... ...
}, },
``` ```

View File

@@ -1,46 +1,46 @@
Config = { Config = {
opts = { opts = {
enabled = true, -- start auto-save when the plugin is loaded (i.e. when your package manager loads it) enabled = true, -- start auto-save when the plugin is loaded (i.e. when your package manager loads it)
execution_message = { execution_message = {
enabled = true, enabled = true,
message = function() -- message to print on save message = function() -- message to print on save
return ("AutoSave: saved at " .. vim.fn.strftime("%H:%M:%S")) return ("AutoSave: saved at " .. vim.fn.strftime("%H:%M:%S"))
end, end,
dim = 0.18, -- dim the color of `message` dim = 0.18, -- dim the color of `message`
cleaning_interval = 1250, -- (milliseconds) automatically clean MsgArea after displaying `message`. See :h MsgArea 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 trigger_events = { "InsertLeave", "TextChanged" }, -- vim events that trigger auto-save. See :h events
-- function that determines whether to save the current buffer or not -- function that determines whether to save the current buffer or not
-- return true: if buffer is ok to be saved -- return true: if buffer is ok to be saved
-- return false: if it's not ok to be saved -- return false: if it's not ok to be saved
condition = function(buf) condition = function(buf)
local fn = vim.fn local fn = vim.fn
local utils = require("auto-save.utils.data") local utils = require("auto-save.utils.data")
if fn.getbufvar(buf, "&modifiable") == 1 and utils.not_in(fn.getbufvar(buf, "&filetype"), {}) then if fn.getbufvar(buf, "&modifiable") == 1 and utils.not_in(fn.getbufvar(buf, "&filetype"), {}) then
return true -- met condition(s), can save return true -- met condition(s), can save
end end
return false -- can't save return false -- can't save
end, end,
write_all_buffers = false, -- write all buffers when the current one meets `condition` 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 debounce_delay = 135, -- saves the file at most every `debounce_delay` milliseconds
callbacks = { -- functions to be executed at different intervals callbacks = { -- functions to be executed at different intervals
enabling = nil, -- ran when enabling auto-save enabling = nil, -- ran when enabling auto-save
disabling = nil, -- ran when disabling auto-save disabling = nil, -- ran when disabling auto-save
before_asserting_save = nil, -- ran before checking `condition` before_asserting_save = nil, -- ran before checking `condition`
before_saving = nil, -- ran before doing the actual save before_saving = nil, -- ran before doing the actual save
after_saving = nil, -- ran after 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 {}
self.opts = vim.tbl_deep_extend("keep", opts, self.opts) self.opts = vim.tbl_deep_extend("keep", opts, self.opts)
end end
function Config:get_options() function Config:get_options()
return self.opts return self.opts
end end
return Config return Config

View File

@@ -15,164 +15,164 @@ local BLACK = "#000000"
local WHITE = "#ffffff" local WHITE = "#ffffff"
api.nvim_create_augroup("AutoSave", { api.nvim_create_augroup("AutoSave", {
clear = true, clear = true,
}) })
local global_vars = {} local global_vars = {}
local function set_buf_var(buf, name, value) local function set_buf_var(buf, name, value)
if buf == nil then if buf == nil then
global_vars[name] = value global_vars[name] = value
else else
if api.nvim_buf_is_valid(buf) then if api.nvim_buf_is_valid(buf) then
api.nvim_buf_set_var(buf, "autosave_" .. name, value) api.nvim_buf_set_var(buf, "autosave_" .. name, value)
end end
end end
end end
local function get_buf_var(buf, name) local function get_buf_var(buf, name)
if buf == nil then if buf == nil then
return global_vars[name] return global_vars[name]
end end
local success, mod = pcall(api.nvim_buf_get_var, buf, "autosave_" .. name) local success, mod = pcall(api.nvim_buf_get_var, buf, "autosave_" .. name)
return success and mod or nil return success and mod or nil
end end
local function debounce(lfn, duration) local function debounce(lfn, duration)
local function inner_debounce() local function inner_debounce()
local buf = api.nvim_get_current_buf() local buf = api.nvim_get_current_buf()
if not get_buf_var(buf, "queued") then if not get_buf_var(buf, "queued") then
vim.defer_fn(function() vim.defer_fn(function()
set_buf_var(buf, "queued", false) set_buf_var(buf, "queued", false)
lfn(buf) lfn(buf)
end, duration) end, duration)
set_buf_var(buf, "queued", true) set_buf_var(buf, "queued", true)
end end
end end
return inner_debounce return inner_debounce
end end
local function echo_execution_message() local function echo_execution_message()
local msg = type(cnf.opts.execution_message.message) == "function" and cnf.opts.execution_message.message() local msg = type(cnf.opts.execution_message.message) == "function" and cnf.opts.execution_message.message()
or cnf.opts.execution_message.message or cnf.opts.execution_message.message
api.nvim_echo({ { msg, AUTO_SAVE_COLOR } }, true, {}) api.nvim_echo({ { msg, AUTO_SAVE_COLOR } }, true, {})
if cnf.opts.execution_message.cleaning_interval > 0 then if cnf.opts.execution_message.cleaning_interval > 0 then
fn.timer_start(cnf.opts.execution_message.cleaning_interval, function() fn.timer_start(cnf.opts.execution_message.cleaning_interval, function()
cmd([[echon '']]) cmd([[echon '']])
end) end)
end end
end end
function M.save(buf) 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 if cnf.opts.condition(buf) == false then
return return
end end
if not api.nvim_buf_get_option(buf, "modified") then if not api.nvim_buf_get_option(buf, "modified") then
return return
end end
callback("before_saving") callback("before_saving")
if g.auto_save_abort == true then if g.auto_save_abort == true then
return return
end end
if cnf.opts.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() api.nvim_buf_call(buf, function()
cmd("silent! write") cmd("silent! write")
end) end)
end end
callback("after_saving") callback("after_saving")
if cnf.opts.execution_message.enabled == true then if cnf.opts.execution_message.enabled == true then
echo_execution_message() echo_execution_message()
end end
end end
local save_func = nil local save_func = nil
local function perform_save() local function perform_save()
g.auto_save_abort = false g.auto_save_abort = false
if save_func == nil then if save_func == nil then
save_func = (cnf.opts.debounce_delay > 0 and debounce(M.save, cnf.opts.debounce_delay) or M.save) save_func = (cnf.opts.debounce_delay > 0 and debounce(M.save, cnf.opts.debounce_delay) or M.save)
end end
save_func() save_func()
end end
function M.on() function M.on()
api.nvim_create_autocmd(cnf.opts.trigger_events, { api.nvim_create_autocmd(cnf.opts.trigger_events, {
callback = function() callback = function()
perform_save() perform_save()
end, end,
pattern = "*", pattern = "*",
group = "AutoSave", group = "AutoSave",
}) })
api.nvim_create_autocmd({ "VimEnter", "ColorScheme", "UIEnter" }, { api.nvim_create_autocmd({ "VimEnter", "ColorScheme", "UIEnter" }, {
callback = function() callback = function()
vim.schedule(function() vim.schedule(function()
if cnf.opts.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")
if MSG_AREA.foreground ~= nil then if MSG_AREA.foreground ~= nil then
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" o.background == "dark"
and colors.darken( and colors.darken(
(MSG_AREA.background or BLACK), (MSG_AREA.background or BLACK),
cnf.opts.execution_message.dim, cnf.opts.execution_message.dim,
MSG_AREA.foreground or BLACK MSG_AREA.foreground or BLACK
) )
or colors.lighten( or colors.lighten(
(MSG_AREA.background or WHITE), (MSG_AREA.background or WHITE),
cnf.opts.execution_message.dim, cnf.opts.execution_message.dim,
MSG_AREA.foreground or WHITE MSG_AREA.foreground or WHITE
) )
) )
colors.highlight("AutoSaveText", { fg = foreground }) colors.highlight("AutoSaveText", { fg = foreground })
AUTO_SAVE_COLOR = "AutoSaveText" AUTO_SAVE_COLOR = "AutoSaveText"
end end
end end
end) end)
end, end,
pattern = "*", pattern = "*",
group = "AutoSave", group = "AutoSave",
}) })
callback("enabling") callback("enabling")
autosave_running = true autosave_running = true
end end
function M.off() function M.off()
api.nvim_create_augroup("AutoSave", { api.nvim_create_augroup("AutoSave", {
clear = true, clear = true,
}) })
callback("disabling") callback("disabling")
autosave_running = false autosave_running = false
end end
function M.toggle() function M.toggle()
if autosave_running then if autosave_running then
M.off() M.off()
echo("off") echo("off")
else else
M.on() M.on()
echo("on") echo("on")
end end
end end
function M.setup(custom_opts) function M.setup(custom_opts)
cnf:set_options(custom_opts) cnf:set_options(custom_opts)
end end
return M return M

View File

@@ -1,70 +1,70 @@
local M = {} local M = {}
---@param hex_str string hexadecimal value of a color ---@param hex_str string hexadecimal value of a color
local hex_to_rgb = function(hex_str) local hex_to_rgb = function(hex_str)
local hex = "[abcdef0-9][abcdef0-9]" local hex = "[abcdef0-9][abcdef0-9]"
local pat = "^#(" .. hex .. ")(" .. hex .. ")(" .. hex .. ")$" local pat = "^#(" .. hex .. ")(" .. hex .. ")(" .. hex .. ")$"
hex_str = string.lower(hex_str) 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) local red, green, blue = string.match(hex_str, pat)
return { tonumber(red, 16), tonumber(green, 16), tonumber(blue, 16) } return { tonumber(red, 16), tonumber(green, 16), tonumber(blue, 16) }
end end
function M.highlight(group, color, force) function M.highlight(group, color, force)
if color.link then if color.link then
vim.api.nvim_set_hl(0, group, { vim.api.nvim_set_hl(0, group, {
link = color.link, link = color.link,
}) })
else else
if color.style then if color.style then
for _, style in ipairs(color.style) do for _, style in ipairs(color.style) do
color[style] = true color[style] = true
end end
end end
color.style = nil color.style = nil
if force then if force then
vim.cmd("hi " .. group .. " guifg=" .. (color.fg or "NONE") .. " guibg=" .. (color.bg or "NONE")) vim.cmd("hi " .. group .. " guifg=" .. (color.fg or "NONE") .. " guibg=" .. (color.bg or "NONE"))
return return
end end
vim.api.nvim_set_hl(0, group, color) vim.api.nvim_set_hl(0, group, color)
end end
end end
function M.get_hl(name) function M.get_hl(name)
local ok, hl = pcall(vim.api.nvim_get_hl_by_name, name, true) local ok, hl = pcall(vim.api.nvim_get_hl_by_name, name, true)
if not ok then if not ok then
return return
end end
for _, key in pairs({ "foreground", "background", "special" }) do for _, key in pairs({ "foreground", "background", "special" }) do
if hl[key] then if hl[key] then
hl[key] = string.format("#%06x", hl[key]) hl[key] = string.format("#%06x", hl[key])
end end
end end
return hl return hl
end end
---@param fg string forecrust color ---@param fg string forecrust color
---@param bg string background color ---@param bg string background color
---@param alpha number number between 0 and 1. 0 results in bg, 1 results in fg ---@param alpha number number between 0 and 1. 0 results in bg, 1 results in fg
function M.blend(fg, bg, alpha) function M.blend(fg, bg, alpha)
bg = hex_to_rgb(bg) bg = hex_to_rgb(bg)
fg = hex_to_rgb(fg) fg = hex_to_rgb(fg)
local blendChannel = function(i) local blendChannel = function(i)
local ret = (alpha * fg[i] + ((1 - alpha) * bg[i])) local ret = (alpha * fg[i] + ((1 - alpha) * bg[i]))
return math.floor(math.min(math.max(0, ret), 255) + 0.5) return math.floor(math.min(math.max(0, ret), 255) + 0.5)
end 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 end
function M.darken(hex, amount, bg) 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 end
function M.lighten(hex, amount, fg) 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 end
return M return M

View File

@@ -1,25 +1,25 @@
local M = {} local M = {}
function M.set_of(list) function M.set_of(list)
local set = {} local set = {}
for i = 1, #list do for i = 1, #list do
set[list[i]] = true set[list[i]] = true
end end
return set return set
end end
function M.not_in(var, arr) function M.not_in(var, arr)
if M.set_of(arr)[var] == nil then if M.set_of(arr)[var] == nil then
return true return true
end end
end end
function M.do_callback(callback_name) 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 if type(cnf.callbacks[callback_name]) == "function" then
cnf.callbacks[callback_name]() cnf.callbacks[callback_name]()
end end
end end
return M return M

View File

@@ -1,25 +1,25 @@
local TITLE = "auto-save" local TITLE = "auto-save"
return function(msg, kind) return function(msg, kind)
local has_notify_plugin = pcall(require, "notify") local has_notify_plugin = pcall(require, "notify")
local level = {} local level = {}
if kind == "error" then if kind == "error" then
level.log = vim.log.levels.ERROR level.log = vim.log.levels.ERROR
level.type = "error" level.type = "error"
elseif kind == "warn" then elseif kind == "warn" then
level.log = vim.log.levels.WARN level.log = vim.log.levels.WARN
level.type = "error" level.type = "error"
else else
level.log = kind or vim.log.levels.INFO level.log = kind or vim.log.levels.INFO
level.type = "info" level.type = "info"
end end
if has_notify_plugin then if has_notify_plugin then
vim.notify(msg, level.log, { vim.notify(msg, level.log, {
title = TITLE, title = TITLE,
}) })
else else
vim.notify(("%s: %s"):format(TITLE, msg), level.log) vim.notify(("%s: %s"):format(TITLE, msg), level.log)
end end
end end

View File

@@ -1,5 +1,5 @@
if vim.g.loaded_auto_save then if vim.g.loaded_auto_save then
return return
end end
vim.g.loaded_auto_save = true 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 local cnf = require("auto-save.config").opts
command("ASToggle", function() command("ASToggle", function()
require("auto-save").toggle() require("auto-save").toggle()
end, {}) end, {})
if cnf.enabled then if cnf.enabled then
require("auto-save").on() require("auto-save").on()
end end