Skip to content

Commit 3ff8ae2

Browse files
authored
Merge pull request #28 from GoldenKoopa/master
feat: apply token replacement to all headers
2 parents c966351 + a0af0c9 commit 3ff8ae2

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

lua/header/core.lua

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ function M.add_headers(header)
4343

4444
-- prepare headers (inline version)
4545
local function prepare(cb)
46+
if header.config.author_from_git then
47+
local gitname = vim.fn.systemlist("git config user.name")
48+
if vim.v.shell_error == 0 and #gitname > 0 then
49+
header.config.author = gitname[1]
50+
end
51+
end
52+
4653
if header.config.license_from_file then
4754
local files = license.scan_license_files()
4855
if #files == 0 then
@@ -65,12 +72,6 @@ function M.add_headers(header)
6572

6673
local file = vim.fn.expand("%:t")
6774
local created = os.date(header.config.date_created_fmt, vim.fn.getftime(vim.fn.expand("%")))
68-
if header.config.author_from_git then
69-
local gitname = vim.fn.systemlist("git config user.name")
70-
if vim.v.shell_error == 0 and #gitname > 0 then
71-
header.config.author = gitname[1]
72-
end
73-
end
7475

7576
local hdrs = {}
7677
if header.config.file_name then
@@ -105,8 +106,14 @@ function M.add_headers(header)
105106
if not hdrs then
106107
return
107108
end
109+
110+
local new_hrds = {}
111+
for i, line in ipairs(hdrs) do
112+
new_hrds[i] = util.replace_all_tokens(line, header)
113+
end
114+
108115
remove_old_headers(comments_table)
109-
local commented = comments.comment_headers(hdrs, comments_table, header.config.use_block_header)
116+
local commented = comments.comment_headers(new_hrds, comments_table, header.config.use_block_header)
110117
vim.api.nvim_buf_set_lines(0, 0, 0, false, commented)
111118
end)
112119
end
@@ -119,9 +126,7 @@ function M.add_license_header(header, opts)
119126
end
120127

121128
local license_text = require("header.licenses." .. string.lower(opts))
122-
license_text = util.replace_token(license_text, "project", header.config.project)
123-
license_text = util.replace_token(license_text, "organization", header.config.author)
124-
license_text = util.replace_token(license_text, "year", os.date("%Y"))
129+
license_text = util.replace_all_tokens(license_text, header)
125130

126131
local license_table = util.string_to_table(license_text)
127132
local comments_table = comments_fn()

lua/header/util.lua

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
local M = {}
22

3-
function M.replace_token(str, token, value)
3+
local function replace_token(str, token, value)
44
local pattern = "{{%s*" .. token .. "%s*}}"
55
return string.gsub(str, pattern, value or "")
66
end
@@ -13,6 +13,13 @@ function M.string_to_table(str)
1313
return lines
1414
end
1515

16+
function M.replace_all_tokens(license_text, header)
17+
license_text = replace_token(license_text, "project", header.config.project)
18+
license_text = replace_token(license_text, "organization", header.config.author)
19+
license_text = replace_token(license_text, "year", os.date("%Y"))
20+
return license_text
21+
end
22+
1623
function M.escape_special_characters(pattern)
1724
if not pattern then
1825
return ""

0 commit comments

Comments
 (0)