From 28fe002ea0b963dc00f1a20b1531bafc05ea0ce6 Mon Sep 17 00:00:00 2001 From: Janis Hutz Date: Thu, 23 Oct 2025 16:21:04 +0200 Subject: [PATCH] [Header] Add --- nvim/lua/plugins/util/header.lua | 71 ++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 nvim/lua/plugins/util/header.lua diff --git a/nvim/lua/plugins/util/header.lua b/nvim/lua/plugins/util/header.lua new file mode 100644 index 0000000..8c7ea14 --- /dev/null +++ b/nvim/lua/plugins/util/header.lua @@ -0,0 +1,71 @@ +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", "h", function() + header.add_headers() + end, opts("Add headers to file")) + + augroup("file-header", { clear = true }) + autocmd("BufWritePre", { + pattern = "*", + callback = function() + local header = require("header") + 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() + local header = require("header") + 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, +}