Skip to content

Commit bba9eba

Browse files
authored
Merge pull request #24 from attilarepka/feat/support-multiline-copyright
feat(header): support multiline `copyright_text` as string or array
2 parents 333bee9 + 38d7c24 commit bba9eba

File tree

3 files changed

+120
-26
lines changed

3 files changed

+120
-26
lines changed

README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ The script comes with the following defaults:
4545
date_created_fmt = "%Y-%m-%d %H:%M:%S",
4646
date_modified = true,
4747
date_modified_fmt = "%Y-%m-%d %H:%M:%S",
48-
line_separator = "------",
48+
line_separator = nil,
4949
use_block_header = true,
5050
copyright_text = nil,
5151
license_from_file = false,
@@ -76,7 +76,11 @@ require("header").setup({
7676
date_modified_fmt = "%Y-%m-%d %H:%M:%S",
7777
line_separator = "------",
7878
use_block_header = false,
79-
copyright_text = "Copyright 2023",
79+
"copyright_text": [
80+
"Copyright (c) 2023 Your Name",
81+
"Your Company",
82+
"All rights reserved."
83+
],
8084
license_from_file = false,
8185
})
8286
```
@@ -130,7 +134,11 @@ The default configuration can be overwritten by a local project `.header.nvim` f
130134
"date_modified_fmt": "%Y-%m-%d %H:%M:%S",
131135
"line_separator": "------",
132136
"use_block_header": true,
133-
"copyright_text": "Copyright (c) 2023 Your Name"
137+
"copyright_text": [
138+
"Copyright (c) 2023 Your Name",
139+
"Your Company",
140+
"All rights reserved."
141+
]
134142
}
135143
```
136144

lua/header.lua

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ header.config = {
1414
date_created_fmt = "%Y-%m-%d %H:%M:%S",
1515
date_modified = true,
1616
date_modified_fmt = "%Y-%m-%d %H:%M:%S",
17-
line_separator = "------",
17+
line_separator = nil,
1818
use_block_header = true,
1919
copyright_text = nil,
2020
license_from_file = false,
@@ -354,7 +354,12 @@ local function prepare_headers(callback)
354354
table.insert(headers, header.config.line_separator)
355355
end
356356
if header.config.copyright_text ~= nil then
357-
table.insert(headers, header.config.copyright_text)
357+
if type(header.config.copyright_text) == "string" then
358+
local copyright_lines = string_to_table(header.config.copyright_text)
359+
vim.list_extend(headers, copyright_lines)
360+
elseif type(header.config.copyright_text) == "table" then
361+
vim.list_extend(headers, header.config.copyright_text)
362+
end
358363
end
359364

360365
callback(headers)

tests/header/header_spec.lua

Lines changed: 102 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -71,29 +71,42 @@ local function build_extended_expected_comments(file_name, comments, constants,
7171
style = comments.block or comments.line
7272
end
7373

74-
local result = {
75-
style.line .. " " .. constants.file_name .. " " .. file_name,
76-
style.line .. " " .. constants.project .. " " .. config.project,
77-
style.line .. " " .. constants.author .. " " .. config.author,
78-
style.line .. " " .. config.line_separator,
79-
style.line .. " " .. config.copyright_text,
80-
"",
81-
file_name,
82-
}
74+
local result = {}
8375

84-
if style.start and style["end"] then
85-
result = {
86-
style.start,
87-
style.line .. " " .. constants.file_name .. " " .. file_name,
88-
style.line .. " " .. constants.project .. " " .. config.project,
89-
style.line .. " " .. constants.author .. " " .. config.author,
90-
style.line .. " " .. config.line_separator,
91-
style.line .. " " .. config.copyright_text,
92-
style["end"],
93-
"",
94-
file_name,
95-
}
76+
local function append_copyright_lines(text)
77+
if not text then
78+
return
79+
end
80+
if type(text) == "string" then
81+
-- split on \n if any
82+
for line in text:gmatch("[^\r\n]+") do
83+
table.insert(result, style.line .. " " .. line)
84+
end
85+
elseif type(text) == "table" then
86+
for _, line in ipairs(text) do
87+
table.insert(result, style.line .. " " .. line)
88+
end
89+
end
90+
end
91+
92+
if style.start then
93+
table.insert(result, style.start)
9694
end
95+
96+
table.insert(result, style.line .. " " .. constants.file_name .. " " .. file_name)
97+
table.insert(result, style.line .. " " .. constants.project .. " " .. config.project)
98+
table.insert(result, style.line .. " " .. constants.author .. " " .. config.author)
99+
table.insert(result, style.line .. " " .. config.line_separator)
100+
append_copyright_lines(config.copyright_text)
101+
102+
if style["end"] then
103+
table.insert(result, style["end"])
104+
end
105+
106+
-- extra empty line and file name
107+
table.insert(result, "")
108+
table.insert(result, file_name)
109+
97110
return result
98111
end
99112

@@ -315,6 +328,74 @@ describe("add_headers", function()
315328
end
316329
end
317330
end)
331+
it("should support multiline copyright text as an array", function()
332+
local filetypes = require("filetypes")
333+
for k, v in pairs(filetypes) do
334+
vim.api.nvim_buf_set_lines(0, 0, -1, false, {})
335+
local file_name = "main." .. k
336+
vim.fn.setline(1, file_name)
337+
vim.api.nvim_buf_set_name(0, file_name)
338+
339+
local config = {
340+
file_name = true,
341+
author = "test_author_name",
342+
project = "test_project_name",
343+
date_created = true,
344+
date_created_fmt = "%Y-%m-%d %H:%M:%S",
345+
date_modified = true,
346+
date_modified_fmt = "%Y-%m-%d %H:%M:%S",
347+
line_separator = "------",
348+
use_block_header = true,
349+
copyright_text = {
350+
"Copyright (c) 2023 Your Name",
351+
"Your Company",
352+
"All rights reserved."
353+
},
354+
}
355+
header.setup(config)
356+
357+
header.add_headers()
358+
359+
local buffer = vim.api.nvim_buf_get_lines(0, 0, -1, false)
360+
local comments = v()
361+
local expected = build_extended_expected_comments(file_name, comments, header.constants, config)
362+
local buffer_without_date = get_buffer_without_date(buffer, comments, header.constants)
363+
364+
assert.are.same(expected, buffer_without_date)
365+
end
366+
end)
367+
it("should support multiline copyright text as a string with '\\n' separators", function()
368+
local filetypes = require("filetypes")
369+
for k, v in pairs(filetypes) do
370+
vim.api.nvim_buf_set_lines(0, 0, -1, false, {})
371+
local file_name = "main." .. k
372+
vim.fn.setline(1, file_name)
373+
vim.api.nvim_buf_set_name(0, file_name)
374+
375+
local config = {
376+
file_name = true,
377+
author = "test_author_name",
378+
project = "test_project_name",
379+
date_created = true,
380+
date_created_fmt = "%Y-%m-%d %H:%M:%S",
381+
date_modified = true,
382+
date_modified_fmt = "%Y-%m-%d %H:%M:%S",
383+
line_separator = "------",
384+
use_block_header = true,
385+
copyright_text = "Copyright (c) 2023 Your Name\nYour Company\nAll rights reserved."
386+
}
387+
388+
header.setup(config)
389+
header.add_headers()
390+
391+
local buffer = vim.api.nvim_buf_get_lines(0, 0, -1, false)
392+
local comments = v()
393+
local expected = build_extended_expected_comments(file_name, comments, header.constants, config)
394+
local buffer_without_date = get_buffer_without_date(buffer, comments, header.constants)
395+
396+
assert.are.same(expected, buffer_without_date)
397+
end
398+
end)
318399
end)
319400

320401
describe("update_date_modified", function()

0 commit comments

Comments
 (0)