Refs and support for local functions in lua
This commit is contained in:
7
after/queries/lua/neogen.scm
Normal file
7
after/queries/lua/neogen.scm
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
(function (parameters) @params)
|
||||||
|
(function_definition (parameters) @params)
|
||||||
|
(local_function (parameters) @params)
|
||||||
|
|
||||||
|
(function (return_statement) @return)
|
||||||
|
(function_definition (if_statement (return_statement) @return))
|
||||||
|
(local_function (return_statement) @return)
|
||||||
@@ -1,15 +1,11 @@
|
|||||||
local ts_utils = require("nvim-treesitter.ts_utils")
|
local ts_utils = require("nvim-treesitter.ts_utils")
|
||||||
|
|
||||||
local M = {}
|
neogen = {}
|
||||||
|
|
||||||
M.generate = function ()
|
local configuration = require('neogen.config')
|
||||||
|
|
||||||
|
neogen.generate = function ()
|
||||||
local comment = {}
|
local comment = {}
|
||||||
local query = [[
|
|
||||||
(function (parameters) @params)
|
|
||||||
(function (return_statement) @return)
|
|
||||||
(function_definition (parameters) @params)
|
|
||||||
(function_definition (if_statement (return_statement) @return))
|
|
||||||
]]
|
|
||||||
|
|
||||||
-- Try to find the upper function
|
-- Try to find the upper function
|
||||||
local cursor = ts_utils.get_node_at_cursor(0)
|
local cursor = ts_utils.get_node_at_cursor(0)
|
||||||
@@ -17,6 +13,7 @@ M.generate = function ()
|
|||||||
while function_node ~= nil do
|
while function_node ~= nil do
|
||||||
if function_node:type() == "function_definition" then break end
|
if function_node:type() == "function_definition" then break end
|
||||||
if function_node:type() == "function" then break end
|
if function_node:type() == "function" then break end
|
||||||
|
if function_node:type() == "local_function" then break end
|
||||||
function_node = function_node:parent()
|
function_node = function_node:parent()
|
||||||
end
|
end
|
||||||
local line = ts_utils.get_node_range(function_node)
|
local line = ts_utils.get_node_range(function_node)
|
||||||
@@ -29,7 +26,7 @@ M.generate = function ()
|
|||||||
local param_comment = offset .. "---@param "
|
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.get_query("lua", "neogen")
|
||||||
for id, node in returned:iter_captures(function_node) do
|
for id, node in returned:iter_captures(function_node) do
|
||||||
|
|
||||||
-- Try to add params
|
-- Try to add params
|
||||||
@@ -58,15 +55,13 @@ M.generate = function ()
|
|||||||
vim.api.nvim_command('startinsert!')
|
vim.api.nvim_command('startinsert!')
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.generate_command()
|
function neogen.generate_command()
|
||||||
vim.api.nvim_command('command! -range -bar Neogen lua require("neogen").generate()')
|
vim.api.nvim_command('command! -range -bar Neogen lua require("neogen").generate()')
|
||||||
end
|
end
|
||||||
|
|
||||||
M.setup = function(opts)
|
neogen.setup = function(opts)
|
||||||
local config = opts or {}
|
local config = opts or configuration
|
||||||
if config.enabled == true then
|
if config.enabled == true then neogen.generate_command() end
|
||||||
M.generate_command()
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return M
|
return neogen
|
||||||
|
|||||||
5
lua/neogen/config.lua
Normal file
5
lua/neogen/config.lua
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
neogen.configuration = {
|
||||||
|
enabled = false,
|
||||||
|
}
|
||||||
|
|
||||||
|
return neogen
|
||||||
Reference in New Issue
Block a user