From 46febe447c4b6f5523d60ecbc5b66cde64a9e626 Mon Sep 17 00:00:00 2001 From: Daniel Mathiot Date: Mon, 23 Aug 2021 15:23:27 +0200 Subject: [PATCH] Use child name to append text after parent Instead of providing the number of childs to append after the parent, it will append the comment just after the first child encountered with child_name --- lua/neogen/configurations/python.lua | 2 +- lua/neogen/generators/default.lua | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lua/neogen/configurations/python.lua b/lua/neogen/configurations/python.lua index ab86c78..40f5ad9 100644 --- a/lua/neogen/configurations/python.lua +++ b/lua/neogen/configurations/python.lua @@ -28,7 +28,7 @@ return { template = { annotation_convention = "google_docstrings", -- required: Which annotation convention to use (default_generator) - append = { position = "after", child_number = 3 }, -- optional: where to append the text (default_generator) + append = { position = "after", child_name = "block" }, -- optional: where to append the text (default_generator) use_default_comment = false, -- If you want to prefix the template with the default comment for the language (default_generator) google_docstrings = { { nil, '"""' }, diff --git a/lua/neogen/generators/default.lua b/lua/neogen/generators/default.lua index 991426f..da3d80d 100644 --- a/lua/neogen/generators/default.lua +++ b/lua/neogen/generators/default.lua @@ -5,9 +5,11 @@ local ts_utils = require("nvim-treesitter.ts_utils") --- @param parent userdata the node used to generate the annotations --- @param data table the data from the granulator, which is a set of [type] = results --- @param template table a template from the configuration +--- There are some special fields it can take: --- @return table { line, content }, with line being the line to append the content neogen.default_generator = function(parent, data, template) local start_row, start_column, end_row, end_column = ts_utils.get_node_range(parent) + P(ts_utils.get_node_range(parent)) local commentstring, generated_template = vim.trim(vim.api.nvim_buf_get_option(0, "commentstring"):format("")) local row_to_place = start_row @@ -16,8 +18,13 @@ neogen.default_generator = function(parent, data, template) local append = template.append or {} if append.position == "after" then - local append_at_child = parent:child(append.child_number + 1) - row_to_place, col_to_place, _ , _ = append_at_child:range() + for child in parent:iter_children() do + if child:type() == append.child_name then + row_to_place, col_to_place, _ , _ = child:range() + break + end + end + end if not template or not template.annotation_convention then