Files
nvim/nvim/lua/plugins/util/header.lua

70 lines
2.5 KiB
Lua

return {
"attilarepka/header.nvim",
config = function()
local header = require("header")
local augroup = vim.api.nvim_create_augroup
local autocmd = vim.api.nvim_create_autocmd
local opts = require("utils").opts
header.setup({
allow_autocmds = true,
file_name = true,
date_created = true,
date_created_fmt = "%Y-%m-%d %H:%M:%S",
date_modified = true,
date_modified_fmt = "%Y-%m-%d %H:%M:%S",
line_separator = "------",
use_block_header = false,
license_from_file = false,
author_from_git = true,
})
vim.keymap.set("n", "<leader><leader>h", function()
header.add_headers()
end, opts("Add headers to file"))
augroup("file-header", { clear = true })
autocmd("BufWritePre", {
pattern = "*",
callback = function()
if header and header.update_date_modified then
header.update_date_modified()
else
vim.notify_once("header.update_date_modified is not available", vim.log.levels.WARN)
end
end,
group = "file-header",
desc = "Update header's date modified",
})
autocmd({ "BufNewFile", "BufReadPost" }, {
pattern = "*",
callback = function()
if not header then
vim.notify_once(
"Could not automatically add header to new file: header module couldn't be found",
vim.log.levels.ERROR
)
return
end
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)
local is_empty = #lines == 1 and lines[1] == ""
if header.config.allow_autocmds and is_empty then
local original_fmt = header.config.date_created_fmt
local now = os.date(header.config.date_created_fmt, os.time())
-- force add_headers to use the current datetime, otherwise it will show 1970-01-01
header.config.date_created_fmt = now
header.add_headers()
header.config.date_created_fmt = original_fmt -- restore the original format
end
end,
group = "file-header",
desc = "Add copyright header to new/empty files",
})
end,
}