From 2c9936781fa5a9a6a6899b6ad1b4908a6247814f Mon Sep 17 00:00:00 2001 From: Daniel Mathiot Date: Fri, 27 Aug 2021 20:23:48 +0200 Subject: [PATCH] Add multiple cursor positioning to template --- lua/neogen.lua | 25 ++++++++++++++++--- lua/neogen/configurations/lua.lua | 14 +++++------ lua/neogen/generators/default.lua | 1 - lua/neogen/utilities/cursor.lua | 40 +++++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 11 deletions(-) create mode 100644 lua/neogen/utilities/cursor.lua diff --git a/lua/neogen.lua b/lua/neogen.lua index b2f4d36..928b291 100644 --- a/lua/neogen.lua +++ b/lua/neogen.lua @@ -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"), diff --git a/lua/neogen/configurations/lua.lua b/lua/neogen/configurations/lua.lua index bbd4a1b..128425e 100644 --- a/lua/neogen/configurations/lua.lua +++ b/lua/neogen/configurations/lua.lua @@ -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" }, }, }, } diff --git a/lua/neogen/generators/default.lua b/lua/neogen/generators/default.lua index cc5f920..3403f7e 100644 --- a/lua/neogen/generators/default.lua +++ b/lua/neogen/generators/default.lua @@ -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) diff --git a/lua/neogen/utilities/cursor.lua b/lua/neogen/utilities/cursor.lua new file mode 100644 index 0000000..bd7217b --- /dev/null +++ b/lua/neogen/utilities/cursor.lua @@ -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