Recognize indentation level
The plugin now recognizes indentation level and will write the comments
at the same indentation level as the function.
for example:
function()
-- this is junk
-- this is junk
-- The annotation will be created here
function()
end
-- this is junk
-- this is junk
end
This commit is contained in:
@@ -1,5 +1,4 @@
|
|||||||
local ts_utils = require("nvim-treesitter.ts_utils")
|
local ts_utils = require("nvim-treesitter.ts_utils")
|
||||||
local ts_query = require("nvim-treesitter.query")
|
|
||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
@@ -21,6 +20,12 @@ M.generate = function ()
|
|||||||
end
|
end
|
||||||
local line = ts_utils.get_node_range(function_node)
|
local line = ts_utils.get_node_range(function_node)
|
||||||
|
|
||||||
|
-- find the starting position in the line function
|
||||||
|
local line_content = vim.api.nvim_buf_get_lines(0, line, line+1, false)[1]
|
||||||
|
local offset = line_content:match("^%s+") or ""
|
||||||
|
|
||||||
|
local return_comment = offset .. "---@return "
|
||||||
|
local param_comment = offset .. "---@param "
|
||||||
|
|
||||||
-- Parse and iterate over each found query
|
-- Parse and iterate over each found query
|
||||||
local returned = vim.treesitter.parse_query("lua", query)
|
local returned = vim.treesitter.parse_query("lua", query)
|
||||||
@@ -31,18 +36,18 @@ M.generate = function ()
|
|||||||
local params = ts_utils.get_node_text(node)[1]:sub(2,-2)
|
local params = ts_utils.get_node_text(node)[1]:sub(2,-2)
|
||||||
for p in string.gmatch(params, '[^,]+') do
|
for p in string.gmatch(params, '[^,]+') do
|
||||||
p = p:gsub("%s+", "") -- remove trailing spaces
|
p = p:gsub("%s+", "") -- remove trailing spaces
|
||||||
table.insert(comment, "---@param " .. p .. " ")
|
table.insert(comment, param_comment .. p .. " ")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Try to add return statement
|
-- Try to add return statement
|
||||||
if returned.captures[id] == "return" then
|
if returned.captures[id] == "return" then
|
||||||
table.insert(comment, "---@return ")
|
table.insert(comment, return_comment)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- At the end, add description annotation
|
-- At the end, add description annotation
|
||||||
table.insert(comment, 1, "---")
|
table.insert(comment, 1, offset .. "---")
|
||||||
|
|
||||||
if #comment == 0 then return end
|
if #comment == 0 then return end
|
||||||
|
|
||||||
@@ -50,7 +55,6 @@ M.generate = function ()
|
|||||||
vim.fn.append(line, comment)
|
vim.fn.append(line, comment)
|
||||||
vim.fn.cursor(line+1, #comment[1])
|
vim.fn.cursor(line+1, #comment[1])
|
||||||
vim.api.nvim_command('startinsert!')
|
vim.api.nvim_command('startinsert!')
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.generate_command()
|
function M.generate_command()
|
||||||
|
|||||||
Reference in New Issue
Block a user