Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: Remove deprecated execution message (#54) #56

Merged
merged 2 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 7 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = {
Expand All @@ -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
Expand All @@ -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
Expand All @@ -93,18 +94,10 @@ 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`)
cancel_defered_save = { "InsertEnter" }, -- vim events that cancel a pending deferred save
cancel_deferred_save = { "InsertEnter" }, -- vim events that cancel a pending deferred save
},
-- function that takes the buffer handle and determines whether to save the current buffer or not
-- return true: if buffer is ok to be saved
Expand Down Expand Up @@ -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,
})
Expand Down
2 changes: 1 addition & 1 deletion doc/auto-save.nvim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ VIM-PLUG *auto-save.nvim-installation-vim-plug*
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`)
cancel_defered_save = { "InsertEnter" }, -- vim events that cancel a pending deferred save
cancel_deferred_save = { "InsertEnter" }, -- vim events that cancel a pending deferred save
},
-- function that takes the buffer handle and determines whether to save the current buffer or not
-- return true: if buffer is ok to be saved
Expand Down
41 changes: 28 additions & 13 deletions lua/auto-save/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,13 @@
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
--- @type TriggerEvent[]?
defer_save = { "InsertLeave", "TextChanged" }, -- vim events that trigger a deferred save (saves after `debounce_delay`)
--- @type TriggerEvent[]?
cancel_defered_save = { "InsertEnter" }, -- vim events that cancel a pending deferred save
cancel_deferred_save = { "InsertEnter" }, -- vim events that cancel a pending deferred save
},
-- function that takes the buffer handle and determines whether to save the current buffer or not
-- return true: if buffer is ok to be saved
Expand All @@ -34,9 +25,33 @@ 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

if custom_opts["trigger_events"] and custom_opts["trigger_events"]["cancel_defered_save"] then
vim.notify(
"The `cancel_defered_save` config option in the auto-save.nvim plugin has been renamed to `cancel_deferred_save`.",
vim.log.levels.WARN
)
custom_opts["trigger_events"]["cancel_deferred_save"] = custom_opts["trigger_events"]["cancel_defered_save"]
custom_opts["trigger_events"]["cancel_defered_save"] = 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()
Expand Down
37 changes: 1 addition & 36 deletions lua/auto-save/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -147,7 +129,7 @@ function M.on()
group = augroup,
desc = "Save a buffer after the `debounce_delay`",
})
autocmds.create_autocmd_for_trigger_events(events.cancel_defered_save, {
autocmds.create_autocmd_for_trigger_events(events.cancel_deferred_save, {
callback = function(opts)
if should_be_saved(opts.buf) then
cancel_timer(opts.buf)
Expand All @@ -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")
Expand Down
103 changes: 0 additions & 103 deletions lua/auto-save/utils/colors.lua

This file was deleted.

Loading