style: add and apply stylua config

Made it compatible with the .editorconfig already present and didn't
introduce any bells and whistles beyond that. Fixed the typo in
.editorconfig indent_style while I was at it.
This commit is contained in:
Oula Kuuva
2023-02-12 17:34:08 +02:00
committed by okuuva
parent 979b6c82f6
commit fc7c13d755
5 changed files with 134 additions and 140 deletions

View File

@@ -10,7 +10,7 @@ end_of_line = lf
charset = utf-8 charset = utf-8
trim_trailing_whitespace = true trim_trailing_whitespace = true
insert_final_newline = true insert_final_newline = true
indent_style = tabs indent_style = tab
indent_size = 4 indent_size = 4
[*.txt] [*.txt]

View File

@@ -16,8 +16,7 @@ Config = {
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 if 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

View File

@@ -15,185 +15,175 @@ 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
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")
api.nvim_echo( api.nvim_echo({
{ {
{ (
( type(cnf.opts.execution_message.message) == "function" and cnf.opts.execution_message.message()
type(cnf.opts.execution_message.message) == "function" or cnf.opts.execution_message.message
and cnf.opts.execution_message.message() ),
or cnf.opts.execution_message.message AUTO_SAVE_COLOR,
), },
AUTO_SAVE_COLOR, }, true, {})
}, if cnf.opts.execution_message.cleaning_interval > 0 then
}, fn.timer_start(cnf.opts.execution_message.cleaning_interval, function()
true, cmd([[echon '']])
{} end)
) end
if cnf.opts.execution_message.cleaning_interval > 0 then api.nvim_echo({
fn.timer_start(cnf.opts.execution_message.cleaning_interval, function() {
cmd([[echon '']]) (
end) type(cnf.opts.execution_message.message) == "function" and cnf.opts.execution_message.message()
end or cnf.opts.execution_message.message
api.nvim_echo({ ),
{ AUTO_SAVE_COLOR,
( },
type(cnf.opts.execution_message.message) == "function" }, true, {})
and cnf.opts.execution_message.message() if cnf.opts.execution_message.cleaning_interval > 0 then
or cnf.opts.execution_message.message fn.timer_start(cnf.opts.execution_message.cleaning_interval, function()
), cmd([[echon '']])
AUTO_SAVE_COLOR, end)
}, end
}, 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 end
local save_func = ( local save_func = (cnf.opts.debounce_delay > 0 and debounce(M.save, cnf.opts.debounce_delay) or M.save)
cnf.opts.debounce_delay > 0 and debounce(M.save, cnf.opts.debounce_delay) or M.save
)
local function perform_save() local function perform_save()
g.auto_save_abort = false g.auto_save_abort = false
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 = (MSG_AREA.background or colors.get_hl("Normal")["background"])
MSG_AREA.background or colors.get_hl("Normal")["background"] local foreground = (
) o.background == "dark"
local foreground = ( and colors.darken(
o.background == "dark" (MSG_AREA.background or BLACK),
and colors.darken( cnf.opts.execution_message.dim,
(MSG_AREA.background or BLACK), MSG_AREA.foreground or BLACK
cnf.opts.execution_message.dim, )
MSG_AREA.foreground or BLACK or colors.lighten(
) (MSG_AREA.background or WHITE),
or colors.lighten( cnf.opts.execution_message.dim,
(MSG_AREA.background or WHITE), MSG_AREA.foreground or WHITE
cnf.opts.execution_message.dim, )
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,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

5
stylua.toml Normal file
View File

@@ -0,0 +1,5 @@
line_endings = "Unix"
indent_type = "Tabs"
indent_width = 4
quote_style = "AutoPreferDouble"
call_parentheses = "Always"