mirror of
https://github.com/zoriya/auto-save.nvim.git
synced 2025-12-06 06:36:11 +00:00
feat!: Remove deprecated execution message (#54)
This removes the execution message and adds a comment to the README on how to readd them via Autocommands. Also I implemented a function to handle breaking changes. It can later be used for https://github.com/okuuva/auto-save.nvim/pull/48 This merges in a feature branch. I would like to collect all potential breaking changes there until we can release it all together in a version 1.0.0 --------- Co-authored-by: okuuva <okuuva@users.noreply.github.com>
This commit is contained in:
19
README.md
19
README.md
@@ -30,7 +30,6 @@
|
||||
- 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
|
||||
- debounce the save with a delay
|
||||
- hook into the lifecycle with autocommands
|
||||
@@ -49,6 +48,7 @@ Install the plugin with your favourite package manager:
|
||||
```lua
|
||||
{
|
||||
"okuuva/auto-save.nvim",
|
||||
version = '^1.0.0', -- see https://devhints.io/semver, alternatively use '*' to use the latest tagged release
|
||||
cmd = "ASToggle", -- optional for lazy loading on command
|
||||
event = { "InsertLeave", "TextChanged" }, -- optional for lazy loading on trigger events
|
||||
opts = {
|
||||
@@ -63,6 +63,7 @@ Install the plugin with your favourite package manager:
|
||||
```lua
|
||||
use({
|
||||
"okuuva/auto-save.nvim",
|
||||
tag = 'v1*',
|
||||
config = function()
|
||||
require("auto-save").setup {
|
||||
-- your config goes here
|
||||
@@ -75,7 +76,7 @@ use({
|
||||
### [vim-plug]("https://github.com/junegunn/vim-plug")
|
||||
|
||||
```vim
|
||||
Plug 'okuuva/auto-save.nvim'
|
||||
Plug 'okuuva/auto-save.nvim', { 'tag': 'v1*' }
|
||||
lua << EOF
|
||||
require("auto-save").setup {
|
||||
-- your config goes here
|
||||
@@ -93,14 +94,6 @@ 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 = { -- See :h events
|
||||
immediate_save = { "BufLeave", "FocusLost" }, -- vim events that trigger an immediate save
|
||||
defer_save = { "InsertLeave", "TextChanged" }, -- vim events that trigger a deferred save (saves after `debounce_delay`)
|
||||
@@ -212,18 +205,18 @@ The plugin fires events at various points during its lifecycle which users can h
|
||||
|
||||
It will always supply the current buffer in the `data.saved_buffer`
|
||||
|
||||
An example to always print the file name before a file is getting saved (use `:messages` if the execution message swallows the print):
|
||||
An example to print a message with the file name after a file got saved:
|
||||
|
||||
```lua
|
||||
local group = vim.api.nvim_create_augroup('autosave', {})
|
||||
|
||||
vim.api.nvim_create_autocmd('User', {
|
||||
pattern = 'AutoSaveWritePre',
|
||||
pattern = 'AutoSaveWritePost',
|
||||
group = group,
|
||||
callback = function(opts)
|
||||
if opts.data.saved_buffer ~= nil then
|
||||
local filename = vim.api.nvim_buf_get_name(opts.data.saved_buffer)
|
||||
print('We are about to save ' .. filename .. ' get ready captain!')
|
||||
print('AutoSave: saved ' .. filename .. ' at ' .. vim.fn.strftime('%H:%M:%S'))
|
||||
end
|
||||
end,
|
||||
})
|
||||
|
||||
@@ -2,15 +2,6 @@
|
||||
Config = {
|
||||
opts = {
|
||||
enabled = true, -- start auto-save when the plugin is loaded (i.e. when your package manager loads it)
|
||||
execution_message = {
|
||||
enabled = true,
|
||||
--- @type string|fun(): string
|
||||
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 = { -- See :h events
|
||||
--- @type TriggerEvent[]?
|
||||
immediate_save = { "BufLeave", "FocusLost" }, -- vim events that trigger an immediate save
|
||||
@@ -34,9 +25,24 @@ Config = {
|
||||
},
|
||||
}
|
||||
|
||||
function Config:set_options(opts)
|
||||
opts = opts or {}
|
||||
self.opts = vim.tbl_deep_extend("keep", opts, self.opts)
|
||||
function Config:handle_deprecations(custom_opts)
|
||||
if custom_opts["execution_message"] then
|
||||
vim.notify(
|
||||
"The `execution_message` has been removed from the auto-save.nvim plugin. Check the Readme on how to add it yourself.",
|
||||
vim.log.levels.WARN
|
||||
)
|
||||
custom_opts["execution_message"] = nil
|
||||
end
|
||||
|
||||
return custom_opts
|
||||
end
|
||||
|
||||
function Config:set_options(custom_opts)
|
||||
custom_opts = custom_opts or {}
|
||||
|
||||
custom_opts = self.handle_deprecations(custom_opts)
|
||||
|
||||
self.opts = vim.tbl_deep_extend("keep", custom_opts, self.opts)
|
||||
end
|
||||
|
||||
function Config:get_options()
|
||||
|
||||
@@ -2,14 +2,12 @@ local M = {}
|
||||
|
||||
--- @class Config
|
||||
local cnf = require("auto-save.config")
|
||||
local colors = require("auto-save.utils.colors")
|
||||
local echo = require("auto-save.utils.echo")
|
||||
local autocmds = require("auto-save.utils.autocommands")
|
||||
|
||||
local api = vim.api
|
||||
local fn = vim.fn
|
||||
local cmd = vim.cmd
|
||||
local schedule = vim.schedule
|
||||
|
||||
local logger
|
||||
local autosave_running = nil
|
||||
@@ -47,18 +45,6 @@ local function debounce(lfn, duration)
|
||||
return inner_debounce
|
||||
end
|
||||
|
||||
local function echo_execution_message()
|
||||
local message = cnf.opts.execution_message.message
|
||||
local msg = type(message) == "function" and message() or message
|
||||
---@diagnostic disable-next-line: deprecated
|
||||
colors.echo_with_highlight(msg --[[@as string]])
|
||||
if cnf.opts.execution_message.cleaning_interval > 0 then
|
||||
fn.timer_start(cnf.opts.execution_message.cleaning_interval, function()
|
||||
cmd([[echon '']])
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
||||
--- Determines if the given buffer is modifiable and if the condition from the config yields true for it
|
||||
--- @param buf number
|
||||
--- @return boolean
|
||||
@@ -102,10 +88,6 @@ local function save(buf)
|
||||
|
||||
autocmds.exec_autocmd("AutoSaveWritePost", { saved_buffer = buf })
|
||||
logger.log(buf, "Saved buffer")
|
||||
|
||||
if cnf.opts.execution_message.enabled == true then
|
||||
echo_execution_message()
|
||||
end
|
||||
end
|
||||
|
||||
--- @param buf number
|
||||
@@ -157,23 +139,6 @@ function M.on()
|
||||
desc = "Cancel a pending save timer for a buffer",
|
||||
})
|
||||
|
||||
local function setup_dimming()
|
||||
if cnf.opts.execution_message.enabled then
|
||||
schedule(function()
|
||||
---@diagnostic disable-next-line: deprecated
|
||||
colors.apply_colors(cnf.opts.execution_message.dim)
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
||||
setup_dimming()
|
||||
api.nvim_create_autocmd("ColorScheme", {
|
||||
callback = function()
|
||||
setup_dimming()
|
||||
end,
|
||||
group = augroup,
|
||||
})
|
||||
|
||||
autosave_running = true
|
||||
|
||||
autocmds.exec_autocmd("AutoSaveEnable")
|
||||
|
||||
@@ -1,103 +0,0 @@
|
||||
--- This file is deprecated and should be removed in the future.
|
||||
--- It is still in use but the functionality does not belong in the scope of this plugin
|
||||
|
||||
local o = vim.o
|
||||
local api = vim.api
|
||||
|
||||
local BLACK = "#000000"
|
||||
local WHITE = "#ffffff"
|
||||
local auto_save_hl_group = "MsgArea"
|
||||
|
||||
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)
|
||||
|
||||
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) }
|
||||
end
|
||||
|
||||
--- @param group string
|
||||
--- @param color table
|
||||
--- @param force? boolean
|
||||
local function 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
|
||||
end
|
||||
|
||||
local function 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
|
||||
end
|
||||
|
||||
---@param fg string foreground color
|
||||
---@param bg string background color
|
||||
---@param alpha number number between 0 and 1. 0 results in bg, 1 results in fg
|
||||
local function blend(fg, bg, alpha)
|
||||
local bg_hex = hex_to_rgb(bg)
|
||||
local fg_hex = hex_to_rgb(fg)
|
||||
|
||||
local blendChannel = function(i)
|
||||
local ret = (alpha * fg_hex[i] + ((1 - alpha) * bg_hex[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))
|
||||
end
|
||||
|
||||
--- This function is still in use, but should be removed in the future.
|
||||
--- The dimming should be done by the colorscheme or an UI Plugin.
|
||||
--- @deprecated
|
||||
--- @param dim_value number
|
||||
M.apply_colors = function(dim_value)
|
||||
if dim_value > 0 then
|
||||
MSG_AREA = get_hl("MsgArea")
|
||||
if MSG_AREA.foreground ~= nil then
|
||||
MSG_AREA.background = (MSG_AREA.background or get_hl("Normal")["background"])
|
||||
local foreground = (
|
||||
o.background == "dark" and blend(MSG_AREA.background or BLACK, MSG_AREA.foreground or BLACK, dim_value)
|
||||
or blend(MSG_AREA.background or WHITE, MSG_AREA.foreground or WHITE, dim_value)
|
||||
)
|
||||
|
||||
highlight("AutoSaveText", { fg = foreground })
|
||||
auto_save_hl_group = "AutoSaveText"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
--- @deprecated
|
||||
--- @see M.apply_colors
|
||||
--- @param message string
|
||||
M.echo_with_highlight = function(message)
|
||||
api.nvim_echo({ { message, auto_save_hl_group } }, true, {})
|
||||
end
|
||||
|
||||
return M
|
||||
Reference in New Issue
Block a user