Add multiple cursor positioning to template
This commit is contained in:
@@ -7,6 +7,7 @@ neogen = {}
|
|||||||
neogen.utilities = {}
|
neogen.utilities = {}
|
||||||
require("neogen.utilities.extractors")
|
require("neogen.utilities.extractors")
|
||||||
require("neogen.utilities.nodes")
|
require("neogen.utilities.nodes")
|
||||||
|
require("neogen.utilities.cursor")
|
||||||
|
|
||||||
-- Require defaults
|
-- Require defaults
|
||||||
require("neogen.locators.default")
|
require("neogen.locators.default")
|
||||||
@@ -52,13 +53,30 @@ neogen.generate = function(opts)
|
|||||||
)
|
)
|
||||||
|
|
||||||
if #content ~= 0 then
|
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
|
-- Append the annotation in required place
|
||||||
vim.fn.append(to_place, content)
|
vim.fn.append(to_place, content)
|
||||||
|
|
||||||
-- Place cursor after annotations and start editing
|
-- Place cursor after annotations and start editing
|
||||||
if neogen.configuration.input_after_comment == true then
|
if neogen.configuration.input_after_comment == true then
|
||||||
vim.fn.cursor(to_place + 1, start_column)
|
-- Creates extmarks for the content
|
||||||
vim.api.nvim_command("startinsert!")
|
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
|
end
|
||||||
end
|
end
|
||||||
@@ -72,7 +90,8 @@ end
|
|||||||
|
|
||||||
neogen.setup = function(opts)
|
neogen.setup = function(opts)
|
||||||
neogen.configuration = vim.tbl_deep_extend("keep", opts or {}, {
|
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
|
-- DEFAULT CONFIGURATION
|
||||||
languages = {
|
languages = {
|
||||||
lua = require("neogen.configurations.lua"),
|
lua = require("neogen.configurations.lua"),
|
||||||
|
|||||||
@@ -116,13 +116,13 @@ return {
|
|||||||
-- Which annotation convention to use
|
-- Which annotation convention to use
|
||||||
annotation_convention = "emmylua",
|
annotation_convention = "emmylua",
|
||||||
emmylua = {
|
emmylua = {
|
||||||
{ nil, "- ", { type = { "class", "func" } } }, -- add this string only on requested types
|
{ nil, "- $1", { type = { "class", "func" } } }, -- add this string only on requested types
|
||||||
{ nil, "- ", { no_results = true } }, -- Shows only when there's no results from the granulator
|
{ nil, "- $1", { no_results = true } }, -- Shows only when there's no results from the granulator
|
||||||
{ "parameters", "- @param %s any" },
|
{ "parameters", "- @param %s any $1" },
|
||||||
{ "vararg", "- @vararg any" },
|
{ "vararg", "- @vararg any $1" },
|
||||||
{ "return_statement", "- @return any" },
|
{ "return_statement", "- @return any $1" },
|
||||||
{ "class_name", "- @class any" },
|
{ "class_name", "- @class any $1" },
|
||||||
{ "type", "- @type %s" },
|
{ "type", "- @type %s $1" },
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -92,7 +92,6 @@ neogen.default_generator = function(parent, data, template, required_type)
|
|||||||
end
|
end
|
||||||
|
|
||||||
if opts.type and vim.tbl_contains(opts.type, required_type) then
|
if opts.type and vim.tbl_contains(opts.type, required_type) then
|
||||||
|
|
||||||
-- Will append the item before all their nodes
|
-- Will append the item before all their nodes
|
||||||
if opts.before_first_item and data[type] then
|
if opts.before_first_item and data[type] then
|
||||||
result = add_values_to_result(result, opts.before_first_item, prefix)
|
result = add_values_to_result(result, opts.before_first_item, prefix)
|
||||||
|
|||||||
40
lua/neogen/utilities/cursor.lua
Normal file
40
lua/neogen/utilities/cursor.lua
Normal 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
|
||||||
Reference in New Issue
Block a user