feat: Add vim.notify messages (#36)

This commit is contained in:
danymat
2022-01-11 12:18:24 +01:00
parent 1a76b572f0
commit f76fd284a4

View File

@@ -19,101 +19,115 @@ neogen.generate = function(opts)
opts = opts or {} opts = opts or {}
opts.type = (opts.type == nil or opts.type == "") and "func" or opts.type -- Default type opts.type = (opts.type == nil or opts.type == "") and "func" or opts.type -- Default type
if not neogen.configuration.enabled then
vim.notify("Neogen not enabled. Please enable it.", vim.log.levels.WARN)
return
end
if vim.bo.filetype == "" then
vim.notify("No filetype detected", vim.log.levels.WARN)
return
end
local parser = vim.treesitter.get_parser(0, vim.bo.filetype) local parser = vim.treesitter.get_parser(0, vim.bo.filetype)
local tstree = parser:parse()[1] local tstree = parser:parse()[1]
local tree = tstree:root() local tree = tstree:root()
local language = neogen.configuration.languages[vim.bo.filetype] local language = neogen.configuration.languages[vim.bo.filetype]
if language then if not language then
language.locator = language.locator or neogen.default_locator vim.notify("Language " .. vim.bo.filetype .. " not supported.", vim.log.levels.WARN)
language.granulator = language.granulator or neogen.default_granulator return
language.generator = language.generator or neogen.default_generator end
if not language.parent[opts.type] or not language.data[opts.type] then language.locator = language.locator or neogen.default_locator
return language.granulator = language.granulator or neogen.default_granulator
end language.generator = language.generator or neogen.default_generator
-- Use the language locator to locate one of the required parent nodes above the cursor if not language.parent[opts.type] or not language.data[opts.type] then
local located_parent_node = language.locator({ vim.notify("Type `" .. opts.type .. "` not supported", vim.log.levels.WARN)
root = tree, return
current = ts_utils.get_node_at_cursor(0), end
}, language.parent[opts.type])
if not located_parent_node then -- Use the language locator to locate one of the required parent nodes above the cursor
return local located_parent_node = language.locator({
end root = tree,
current = ts_utils.get_node_at_cursor(0),
}, language.parent[opts.type])
-- Use the language granulator to get the required content inside the node found with the locator if not located_parent_node then
local data = language.granulator(located_parent_node, language.data[opts.type]) return
end
if data then -- Use the language granulator to get the required content inside the node found with the locator
-- Will try to generate the documentation from a template and the data found from the granulator local data = language.granulator(located_parent_node, language.data[opts.type])
local to_place, start_column, content = language.generator(
located_parent_node,
data,
language.template,
opts.type
)
if #content ~= 0 then if data then
neogen.utilities.cursor.del_extmarks() -- Delete previous extmarks before setting any new ones -- 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
)
local jump_text = language.jump_text or neogen.configuration.jump_text if #content ~= 0 then
neogen.utilities.cursor.del_extmarks() -- Delete previous extmarks before setting any new ones
--- Removes jump_text marks and keep the second part of jump_text|other_text if there is one (which is other_text) local jump_text = language.jump_text or neogen.configuration.jump_text
local delete_marks = function(v)
local pattern = jump_text .. "[|%w]+"
local matched = string.match(v, pattern)
if matched then --- Removes jump_text marks and keep the second part of jump_text|other_text if there is one (which is other_text)
local split = vim.split(matched, "|", true) local delete_marks = function(v)
if #split == 2 and neogen.configuration.input_after_comment == false then local pattern = jump_text .. "[|%w]+"
return string.gsub(v, jump_text .. "|", "") local matched = string.match(v, pattern)
end
else if matched then
return string.gsub(v, jump_text, "") 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
-- First and last extmarks are needed to know the range of inserted content vim.fn.append(to_place, content)
if neogen.configuration.input_after_comment == true then
-- Creates extmark for the beggining of the content -- Place cursor after annotations and start editing
neogen.utilities.cursor.create(to_place + 1, start_column) -- First and last extmarks are needed to know the range of inserted content
-- 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 start = 0 neogen.utilities.cursor.create(to_place + 1, start_column)
local count = 0 -- Creates extmarks for the content
while true do for i, value in pairs(content_with_marks) do
start = string.find(value, jump_text, start + 1) local start = 0
if not start then local count = 0
break while true do
end start = string.find(value, jump_text, start + 1)
neogen.utilities.cursor.create(to_place + i, start - count * #jump_text) if not start then
count = count + 1 break
end end
neogen.utilities.cursor.create(to_place + i, start - count * #jump_text)
count = count + 1
end end
-- Create extmark to jump back to current location
local pos = vim.api.nvim_win_get_cursor(0)
neogen.utilities.cursor.create(pos[1], pos[2] + 2)
-- 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
-- Create extmark to jump back to current location
local pos = vim.api.nvim_win_get_cursor(0)
neogen.utilities.cursor.create(pos[1], pos[2] + 2)
-- 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