From 24a04984770201c11c6c365235f928b35f8bb266 Mon Sep 17 00:00:00 2001 From: danymat Date: Sun, 9 Jan 2022 14:14:23 +0100 Subject: [PATCH] fix: Places `file` annotations always on top --- lua/neogen/configurations/c.lua | 5 +++++ lua/neogen/generators/default.lua | 27 +++++++++++++++++++-------- lua/neogen/locators/default.lua | 8 ++++++++ 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/lua/neogen/configurations/c.lua b/lua/neogen/configurations/c.lua index 82413b3..c13b8d9 100644 --- a/lua/neogen/configurations/c.lua +++ b/lua/neogen/configurations/c.lua @@ -140,6 +140,11 @@ return { if not result then return nil end + + if node_info.current == nil then + return result + end + -- if the function happens to be a function template we want to place -- the annotation before the template statement and extract the -- template parameters names as well diff --git a/lua/neogen/generators/default.lua b/lua/neogen/generators/default.lua index 3403f7e..0c50479 100644 --- a/lua/neogen/generators/default.lua +++ b/lua/neogen/generators/default.lua @@ -50,11 +50,26 @@ end --- @param required_type string --- @return table { line, content, opts }, with line being the line to append the content neogen.default_generator = function(parent, data, template, required_type) - local start_row, start_column, end_row, end_column = ts_utils.get_node_range(parent) + local row_to_place, col_to_place + -- You can use a custom position placement + if template.position and type(template.position) == "function" then + row_to_place, col_to_place = template.position(parent, required_type) + end + + -- If the custom placement does not return the correct row and cols, default to the node range + -- Same if there is no custom placement + if not row_to_place and not col_to_place then + -- Because the file type is always at top + if required_type == "file" then + row_to_place = 0 + col_to_place = 0 + else + row_to_place, col_to_place, _, _ = ts_utils.get_node_range(parent) + end + end + local commentstring, generated_template = vim.trim(vim.api.nvim_buf_get_option(0, "commentstring"):format("")) - local row_to_place = start_row - local col_to_place = start_column local append = template.append or {} if append.position == "after" then @@ -87,9 +102,7 @@ neogen.default_generator = function(parent, data, template, required_type) local type = values[1] local formatted_string = values[2] local opts = vim.deepcopy(values[3]) or {} - if not opts.type then - opts.type = { required_type } - end + opts.type = opts.type or { required_type } if opts.type and vim.tbl_contains(opts.type, required_type) then -- Will append the item before all their nodes @@ -98,8 +111,6 @@ neogen.default_generator = function(parent, data, template, required_type) end -- If there is no data returned, will append the string with opts.no_results - --P(data) - --P(opts) if opts.no_results == true and vim.tbl_isempty(data) then local inserted = conditional_prefix_inserter(prefix, formatted_string) table.insert(result, inserted) diff --git a/lua/neogen/locators/default.lua b/lua/neogen/locators/default.lua index 9ad553f..cc1e67f 100644 --- a/lua/neogen/locators/default.lua +++ b/lua/neogen/locators/default.lua @@ -8,6 +8,14 @@ --- @param nodes_to_match table a list of parent nodes to match --- @return userdata node one of the nodes to match directly above the given node neogen.default_locator = function(node_info, nodes_to_match) + if not node_info.current then + if vim.tbl_contains(nodes_to_match, node_info.root:type()) then + return node_info.root + end + + return + end + -- If we find one of the wanted nodes in current one, return the current node if vim.tbl_contains(nodes_to_match, node_info.current:type()) then return node_info.current