Make comment in after position more resilient

This commit is contained in:
Daniel Mathiot
2021-08-23 15:08:41 +02:00
parent 40616b25d7
commit 03781f23d6
3 changed files with 10 additions and 10 deletions

View File

@@ -2,7 +2,7 @@ local ts_utils = require("nvim-treesitter.ts_utils")
return {
-- Search for these nodes
parent = { "function_definition" },
parent = { "function_definition", "class_definition" },
-- Traverse down these nodes and extract the information as necessary
data = {
@@ -28,11 +28,11 @@ return {
template = {
annotation_convention = "google_docstrings", -- required: Which annotation convention to use (default_generator)
append = { position = "after", offset = 4 }, -- optional: where to append the text (default_generator)
append = { position = "after", child_number = 3 }, -- 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, '"""' },
{ "parameters", "\t%s: ", { before_first_item = "Args: " } },
{ "parameters", "\t%s: ", { before_first_item = "Args: " } }, -- FIXME when no parameter is set
{ nil, '"""' },
},
},

View File

@@ -8,17 +8,16 @@ local ts_utils = require("nvim-treesitter.ts_utils")
--- @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
local col_to_place = start_column
local append = template.append or {}
if append.position == "after" then
row_to_place = end_row - 1
-- Add the offset if there's one
col_to_place = start_column + (append.offset or 0)
local append_at_child = parent:child(append.child_number + 1)
row_to_place, col_to_place, _ , _ = append_at_child:range()
end
if not template or not template.annotation_convention then

View File

@@ -10,6 +10,7 @@ neogen.utility = {
local result = {}
local split = vim.split(name, "|", true)
for child in node:iter_children() do
if vim.tbl_contains(split, child:type()) then
table.insert(result, ts_utils.get_node_text(child)[1])
@@ -34,13 +35,13 @@ neogen.utility = {
return function(node)
local result = {}
for i, subtree in ipairs(tree) do
local child_node = node:named_child(i - 1)
for i, subtree in pairs(tree) do
local child_node = node:named_child(tonumber(i) - 1)
if subtree == "extract" then
return self:extract_children(name)(child_node)
else
return self:extract_children_from(subtree, name)(node)
return self:extract_children_from(subtree, name)(child_node)
end
end