Add multiple cursor positioning to template

This commit is contained in:
Daniel Mathiot
2021-08-27 20:23:48 +02:00
parent 4e4c7da587
commit 2c9936781f
4 changed files with 69 additions and 11 deletions

View File

@@ -7,6 +7,7 @@ neogen = {}
neogen.utilities = {}
require("neogen.utilities.extractors")
require("neogen.utilities.nodes")
require("neogen.utilities.cursor")
-- Require defaults
require("neogen.locators.default")
@@ -52,13 +53,30 @@ neogen.generate = function(opts)
)
if #content ~= 0 then
local jump_text = language.jump_text or neogen.configuration.jump_text
local delete_marks = function(v)
return string.gsub(v, jump_text, "")
end
local content_with_marks = vim.deepcopy(content)
-- delete all jump_text marks
neogen.utilities.cursor.replace_jump_text(content, language.template)
content = vim.tbl_map(delete_marks, content)
-- Append the annotation in required place
vim.fn.append(to_place, content)
-- Place cursor after annotations and start editing
if neogen.configuration.input_after_comment == true then
vim.fn.cursor(to_place + 1, start_column)
vim.api.nvim_command("startinsert!")
-- Creates extmarks for the content
for i, value in pairs(content_with_marks) do
local input_start, _ = string.find(value, jump_text)
if input_start then
neogen.utilities.cursor.create(to_place + i, input_start)
end
end
neogen.utilities.cursor.jump()
end
end
end
@@ -72,7 +90,8 @@ end
neogen.setup = function(opts)
neogen.configuration = vim.tbl_deep_extend("keep", opts or {}, {
input_after_comment = true,
input_after_comment = true, -- bool, If you want to jump with the cursor after annotation
jump_text = "$1", -- symbol to find for jumping cursor in template
-- DEFAULT CONFIGURATION
languages = {
lua = require("neogen.configurations.lua"),

View File

@@ -116,13 +116,13 @@ return {
-- Which annotation convention to use
annotation_convention = "emmylua",
emmylua = {
{ nil, "- ", { type = { "class", "func" } } }, -- add this string only on requested types
{ nil, "- ", { no_results = true } }, -- Shows only when there's no results from the granulator
{ "parameters", "- @param %s any" },
{ "vararg", "- @vararg any" },
{ "return_statement", "- @return any" },
{ "class_name", "- @class any" },
{ "type", "- @type %s" },
{ nil, "- $1", { type = { "class", "func" } } }, -- add this string only on requested types
{ nil, "- $1", { no_results = true } }, -- Shows only when there's no results from the granulator
{ "parameters", "- @param %s any $1" },
{ "vararg", "- @vararg any $1" },
{ "return_statement", "- @return any $1" },
{ "class_name", "- @class any $1" },
{ "type", "- @type %s $1" },
},
},
}

View File

@@ -92,7 +92,6 @@ neogen.default_generator = function(parent, data, template, required_type)
end
if opts.type and vim.tbl_contains(opts.type, required_type) then
-- Will append the item before all their nodes
if opts.before_first_item and data[type] then
result = add_values_to_result(result, opts.before_first_item, prefix)

View File

@@ -0,0 +1,40 @@
neogen.utilities.cursor = {}
local neogen_ns = vim.api.nvim_create_namespace("neogen")
neogen.utilities.cursor.create = function(line, col)
return vim.api.nvim_buf_set_extmark(0, neogen_ns, line - 1, col - 1, {})
end
neogen.utilities.cursor.go_next_extmark = function()
local extm_list = vim.api.nvim_buf_get_extmarks(0, neogen_ns, 0, -1, {})
if #extm_list ~= 0 then
vim.api.nvim_buf_del_extmark(0, neogen_ns, extm_list[1][1])
end
if #extm_list ~= 0 then
vim.api.nvim_win_set_cursor(0, { extm_list[1][2] + 1, extm_list[1][3] + 1 })
return
end
end
neogen.utilities.cursor.go_next_extmark = function()
local extm_list = vim.api.nvim_buf_get_extmarks(0, neogen_ns, 0, -1, {})
if #extm_list ~= 0 then
vim.api.nvim_buf_del_extmark(0, neogen_ns, extm_list[1][1])
P("remaning marks", #extm_list - 1)
end
if #extm_list ~= 0 then
vim.api.nvim_win_set_cursor(0, { extm_list[1][2] + 1, extm_list[1][3] })
return true
else
return false
end
end
neogen.utilities.cursor.jump = function()
if neogen.utilities.cursor.go_next_extmark() then
vim.api.nvim_command("startinsert!")
end
end
neogen.utilities.cursor.replace_jump_text = function(content, template) end