fix: Use only filetype tree for generation (#26)
This commit is contained in:
132
lua/neogen.lua
132
lua/neogen.lua
@@ -1,6 +1,7 @@
|
|||||||
local ok, ts_utils = pcall(require, "nvim-treesitter.ts_utils")
|
local ok, ts_utils = pcall(require, "nvim-treesitter.ts_utils")
|
||||||
assert(ok, "neogen requires nvim-treesitter to operate :(")
|
assert(ok, "neogen requires nvim-treesitter to operate :(")
|
||||||
|
|
||||||
|
---@diagnostic disable-next-line: lowercase-global
|
||||||
neogen = {}
|
neogen = {}
|
||||||
|
|
||||||
-- Require utilities
|
-- Require utilities
|
||||||
@@ -18,91 +19,94 @@ neogen.generate = function(opts)
|
|||||||
opts = opts or {
|
opts = opts or {
|
||||||
type = "func",
|
type = "func",
|
||||||
}
|
}
|
||||||
vim.treesitter.get_parser(0):for_each_tree(function(tree, language_tree)
|
|
||||||
local language = neogen.configuration.languages[language_tree:lang()]
|
|
||||||
|
|
||||||
if language then
|
local parser = vim.treesitter.get_parser(0, vim.bo.filetype)
|
||||||
language.locator = language.locator or neogen.default_locator
|
local tstree = parser:parse()[1]
|
||||||
language.granulator = language.granulator or neogen.default_granulator
|
local tree = tstree:root()
|
||||||
language.generator = language.generator or neogen.default_generator
|
|
||||||
|
|
||||||
if not language.parent[opts.type] or not language.data[opts.type] then
|
local language = neogen.configuration.languages[vim.bo.filetype]
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Use the language locator to locate one of the required parent nodes above the cursor
|
if language then
|
||||||
local located_parent_node = language.locator({
|
language.locator = language.locator or neogen.default_locator
|
||||||
root = tree:root(),
|
language.granulator = language.granulator or neogen.default_granulator
|
||||||
current = ts_utils.get_node_at_cursor(0),
|
language.generator = language.generator or neogen.default_generator
|
||||||
}, language.parent[opts.type])
|
|
||||||
|
|
||||||
if not located_parent_node then
|
if not language.parent[opts.type] or not language.data[opts.type] then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Use the language granulator to get the required content inside the node found with the locator
|
-- Use the language locator to locate one of the required parent nodes above the cursor
|
||||||
local data = language.granulator(located_parent_node, language.data[opts.type])
|
local located_parent_node = language.locator({
|
||||||
|
root = tree,
|
||||||
|
current = ts_utils.get_node_at_cursor(0),
|
||||||
|
}, language.parent[opts.type])
|
||||||
|
|
||||||
if data then
|
if not located_parent_node then
|
||||||
-- Will try to generate the documentation from a template and the data found from the granulator
|
return
|
||||||
local to_place, start_column, content = language.generator(
|
end
|
||||||
located_parent_node,
|
|
||||||
data,
|
|
||||||
language.template,
|
|
||||||
opts.type
|
|
||||||
)
|
|
||||||
|
|
||||||
if #content ~= 0 then
|
-- Use the language granulator to get the required content inside the node found with the locator
|
||||||
neogen.utilities.cursor.del_extmarks() -- Delete previous extmarks before setting any new ones
|
local data = language.granulator(located_parent_node, language.data[opts.type])
|
||||||
|
|
||||||
local jump_text = language.jump_text or neogen.configuration.jump_text
|
if data then
|
||||||
|
-- Will try to generate the documentation from a template and the data found from the granulator
|
||||||
|
local to_place, start_column, content = language.generator(
|
||||||
|
located_parent_node,
|
||||||
|
data,
|
||||||
|
language.template,
|
||||||
|
opts.type
|
||||||
|
)
|
||||||
|
|
||||||
--- Removes jump_text marks and keep the second part of jump_text|other_text if there is one (which is other_text)
|
if #content ~= 0 then
|
||||||
local delete_marks = function(v)
|
neogen.utilities.cursor.del_extmarks() -- Delete previous extmarks before setting any new ones
|
||||||
local pattern = jump_text .. "[|%w]+"
|
|
||||||
local matched = string.match(v, pattern)
|
|
||||||
|
|
||||||
if matched then
|
local jump_text = language.jump_text or neogen.configuration.jump_text
|
||||||
local split = vim.split(matched, "|", true)
|
|
||||||
if #split == 2 and neogen.configuration.input_after_comment == false then
|
--- Removes jump_text marks and keep the second part of jump_text|other_text if there is one (which is other_text)
|
||||||
return string.gsub(v, jump_text .. "|", "")
|
local delete_marks = function(v)
|
||||||
end
|
local pattern = jump_text .. "[|%w]+"
|
||||||
else
|
local matched = string.match(v, pattern)
|
||||||
return string.gsub(v, jump_text, "")
|
|
||||||
|
if matched then
|
||||||
|
local split = vim.split(matched, "|", true)
|
||||||
|
if #split == 2 and neogen.configuration.input_after_comment == false then
|
||||||
|
return string.gsub(v, jump_text .. "|", "")
|
||||||
end
|
end
|
||||||
|
else
|
||||||
return string.gsub(v, pattern, "")
|
return string.gsub(v, jump_text, "")
|
||||||
end
|
end
|
||||||
|
|
||||||
local content_with_marks = vim.deepcopy(content)
|
return string.gsub(v, pattern, "")
|
||||||
|
end
|
||||||
|
|
||||||
-- delete all jump_text marks
|
local content_with_marks = vim.deepcopy(content)
|
||||||
content = vim.tbl_map(delete_marks, content)
|
|
||||||
|
|
||||||
-- Append the annotation in required place
|
-- delete all jump_text marks
|
||||||
vim.fn.append(to_place, content)
|
content = vim.tbl_map(delete_marks, content)
|
||||||
|
|
||||||
-- Place cursor after annotations and start editing
|
-- Append the annotation in required place
|
||||||
if neogen.configuration.input_after_comment == true then
|
vim.fn.append(to_place, content)
|
||||||
-- Creates extmark for the beggining of the content
|
|
||||||
neogen.utilities.cursor.create(to_place + 1, start_column)
|
-- Place cursor after annotations and start editing
|
||||||
-- Creates extmarks for the content
|
if neogen.configuration.input_after_comment == true then
|
||||||
for i, value in pairs(content_with_marks) do
|
-- Creates extmark for the beggining of the content
|
||||||
local input_start, _ = string.find(value, jump_text)
|
neogen.utilities.cursor.create(to_place + 1, start_column)
|
||||||
if input_start then
|
-- Creates extmarks for the content
|
||||||
neogen.utilities.cursor.create(to_place + i, input_start)
|
for i, value in pairs(content_with_marks) do
|
||||||
end
|
local input_start, _ = string.find(value, jump_text)
|
||||||
|
if input_start then
|
||||||
|
neogen.utilities.cursor.create(to_place + i, input_start)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Creates extmark for the end of the content
|
|
||||||
neogen.utilities.cursor.create(to_place + #content + 1, 0)
|
|
||||||
|
|
||||||
neogen.utilities.cursor.jump({ first_time = true })
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Creates extmark for the end of the content
|
||||||
|
neogen.utilities.cursor.create(to_place + #content + 1, 0)
|
||||||
|
|
||||||
|
neogen.utilities.cursor.jump({ first_time = true })
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end)
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function neogen.jump_next()
|
function neogen.jump_next()
|
||||||
|
|||||||
Reference in New Issue
Block a user