The provided code is a Lua configuration snippet for the Neovim text editor, specifically for the monaqa/dial.nvim plugin. This plugin is used for incrementing and decrementing numbers, dates, and other elements in a text file. Here's a refactored version of the code with some improvements and explanations:
return {
{
"monaqa/dial.nvim",
lazy = true,
keys = {
{ "<M-+>", require("utils.lua_helper_funcs").dial_in, silent = true, noremap = true },
{ "<M-->", require("utils.lua_helper_funcs").dial_dn, silent = true, noremap = true },
{ "<M-+>", require("utils.lua_helper_funcs").dial_iv, mode = "x", silent = true, noremap = true },
{ "<M-->", require("utils.lua_helper_funcs").dial_dv, mode = "x", silent = true, noremap = true },
{ "g<M-+>", require("utils.lua_helper_funcs").dial_ign, silent = true, noremap = true },
{ "g<M-->", require("utils.lua_helper_funcs").dial_dgn, silent = true, noremap = true },
{ "g<M-+>", require("utils.lua_helper_funcs").dial_igv, mode = "x", silent = true, noremap = true },
{ "g<M-->", require("utils.lua_helper_funcs").dial_dgv, mode = "x", silent = true, noremap = true },
},
config = function()
local augend = require("dial.augend")
local helper = require("utils.lua_helper_funcs")
-- Define default augends
local default_augends = {
augend.date.alias["%d/%m/%Y"],
augend.date.alias["%d.%m.%Y"],
augend.date.alias["%Y-%m-%d"],
augend.date.alias["%H:%M"],
augend.integer.alias.decimal,
augend.integer.alias.decimal_int,
augend.constant.alias.bool,
augend.constant.alias.de_weekday,
augend.constant.alias.de_weekday_full,
}
-- Insert custom augends for Lua and LaTeX
local function insert_table(base_table, add_table)
for _, elements in ipairs(add_table) do
table.insert(
base_table,
augend.constant.new({
elements = elements,
word = false,
cyclic = true,
})
)
end
end
insert_table(default_augends, helper.dial_lua)
local tex_augends = vim.deepcopy(default_augends)
insert_table(tex_augends, helper.dial_tex)
-- Register augends for specific filetypes
require("dial.config").augends:on_filetype({
default = default_augends,
lua = default_augends,
tex = tex_augends,
markdown = tex_augends,
})
end,
},
}
Changes and Improvements:
- Simplified the code structure by removing unnecessary nesting.
- Used
vim.deepcopy to create a deep copy of default_augends when creating tex_augends. This ensures that tex_augends is a separate table and not just a reference to default_augends.
- Improved code readability by using whitespace and blank lines to separate logical sections.