@@ -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
98111end
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\n Your Company\n All 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 )
318399end )
319400
320401describe (" update_date_modified" , function ()
0 commit comments