From e682494f1324297c613edc68ad4eef1932dc9533 Mon Sep 17 00:00:00 2001 From: danymat Date: Sat, 22 Jan 2022 20:31:33 +0100 Subject: [PATCH] fix: Better use of nested template --- lua/neogen/generators/default.lua | 35 ++++++++++++++----------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/lua/neogen/generators/default.lua b/lua/neogen/generators/default.lua index 1f05feb..e5e9a87 100644 --- a/lua/neogen/generators/default.lua +++ b/lua/neogen/generators/default.lua @@ -127,7 +127,7 @@ neogen.default_generator = function(parent, data, template, required_type) local inserted = conditional_prefix_inserter(prefix, formatted_string:format(value)) table.insert(result, inserted) end - elseif type(inserted_type) == "table" and #inserted_type >= 1 then + elseif type(inserted_type) == "table" and data[opts.required] then -- First item in the template item can be a table. -- in this case, the template will use provided types to generate the line. -- e.g {{ "type", "parameter"}, "* @type {%s} %s"} @@ -135,26 +135,23 @@ neogen.default_generator = function(parent, data, template, required_type) -- If one item is missing, it'll use the required option to iterate -- and will replace the missing item with default jump_text - if data[opts.required] or (data[inserted_type[1]] and opts.required == nil) then - local count = opts.required and #data[opts.required] or #data[inserted_type[1]] - for i = 1, count do - local _values = {} - for _, v in ipairs(inserted_type) do - if data[v] and data[v][i] then - table.insert(_values, data[v][i]) - else - local jump_text = neogen.configuration.jump_text - table.insert(_values, jump_text) - end - end - if not vim.tbl_isempty(_values) then - local inserted = conditional_prefix_inserter( - prefix, - formatted_string:format(unpack(_values)) - ) - table.insert(result, inserted) + for _, tbl in pairs(data[opts.required]) do + local _values = {} + for _, v in ipairs(inserted_type) do + if tbl[v] then + table.insert(_values, tbl[v][1]) + else + local jump_text = neogen.configuration.jump_text + table.insert(_values, jump_text) end end + if not vim.tbl_isempty(_values) then + local inserted = conditional_prefix_inserter( + prefix, + formatted_string:format(unpack(_values)) + ) + table.insert(result, inserted) + end end end end