Add python support

At the moment, only supports params for function definitions.
I added more options to configure the default generator (please see the
templates field in configuration/python.lua)
This commit is contained in:
Daniel Mathiot
2021-08-23 12:53:27 +02:00
parent df48fe0a38
commit 40616b25d7
4 changed files with 70 additions and 3 deletions

View File

@@ -63,6 +63,7 @@ neogen.setup = function(opts)
-- DEFAULT CONFIGURATION
languages = {
lua = require("neogen.configurations.lua"),
python = require("neogen.configurations.python"),
},
})

View File

@@ -0,0 +1,39 @@
local ts_utils = require("nvim-treesitter.ts_utils")
return {
-- Search for these nodes
parent = { "function_definition" },
-- Traverse down these nodes and extract the information as necessary
data = {
["function_definition"] = {
["2"] = {
match = "parameters",
extract = function(node)
local regular_params = neogen.utility:extract_children("identifier")(node)
return {
parameters = regular_params,
}
end,
},
},
},
-- Use default granulator and generator
locator = nil,
granulator = nil,
generator = nil,
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)
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: " } },
{ nil, '"""' },
},
},
}

View File

@@ -7,9 +7,20 @@ local ts_utils = require("nvim-treesitter.ts_utils")
--- @param template table a template from the configuration
--- @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, _, _ = ts_utils.get_node_range(parent)
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)
end
if not template or not template.annotation_convention then
-- Default template
generated_template = {
@@ -27,11 +38,26 @@ neogen.default_generator = function(parent, data, template)
local function parse_generated_template()
local result = {}
local prefix = (" "):rep(start_column) .. commentstring
local prefix = (" "):rep(col_to_place)
-- Do not append the comment string if not wanted
if template.use_default_comment ~= false then
prefix = prefix .. commentstring
end
for _, values in ipairs(generated_template) do
local type = values[1]
-- Checks for custom options
-- Supported options:
-- - before_first_item = value
local opts = values[3] or {}
-- Will append the item before all their nodes
if opts.before_first_item then
table.insert(result, prefix .. opts.before_first_item)
end
if not type then
table.insert(result, prefix .. values[2]:format(""))
else
@@ -50,5 +76,5 @@ neogen.default_generator = function(parent, data, template)
return result
end
return start_row, start_column, parse_generated_template()
return row_to_place, col_to_place, parse_generated_template()
end