diff --git a/lua/neogen.lua b/lua/neogen.lua index 6c00117..896b9d8 100644 --- a/lua/neogen.lua +++ b/lua/neogen.lua @@ -91,7 +91,7 @@ neogen.generate = function(opts) -- First and last extmarks are needed to know the range of inserted content if neogen.configuration.input_after_comment == true then -- Creates extmark for the beggining of the content - neogen.utilities.cursor.create(to_place + 1, start_column) + neogen.utilities.cursor.create(to_place + 1, start_column, true) -- Creates extmarks for the content for i, value in pairs(content_with_marks) do local start = 0 @@ -107,7 +107,7 @@ neogen.generate = function(opts) end -- Creates extmark for the end of the content - neogen.utilities.cursor.create(to_place + #content + 1, 0) + neogen.utilities.cursor.create(to_place + #content + 1, 0, true) neogen.utilities.cursor.jump({ first_time = true }) end diff --git a/lua/neogen/utilities/cursor.lua b/lua/neogen/utilities/cursor.lua index 61bb9aa..2181be0 100644 --- a/lua/neogen/utilities/cursor.lua +++ b/lua/neogen/utilities/cursor.lua @@ -1,15 +1,19 @@ neogen.utilities.cursor = {} local neogen_ns = vim.api.nvim_create_namespace("neogen") +local neogen_virt_text_ns = vim.api.nvim_create_namespace("neogen_virt_text") local current_position = 1 --- Wrapper around set_extmark with 1-based numbering for `line` and `col`, and returns the id of the created extmark --- @param line string --- @param col string --- @return number -neogen.utilities.cursor.create = function(line, col) +neogen.utilities.cursor.create = function(line, col, delimiters) + local opts = delimiters == true and {} + or { virt_text = { { ">", "TSTitle" } }, virt_text_pos = "overlay", virt_text_hide = true } current_position = 1 - local new_col = col == 0 and 0 or col -1 + local new_col = col == 0 and 0 or col - 1 + vim.api.nvim_buf_set_extmark(0, neogen_virt_text_ns, line - 1, new_col, opts) return vim.api.nvim_buf_set_extmark(0, neogen_ns, line - 1, new_col, {}) end @@ -18,10 +22,17 @@ end --- First jumpable extmark is the one after the extmarks responsible of start/end of annotation neogen.utilities.cursor.go_next_extmark = function() local extm_list = vim.api.nvim_buf_get_extmarks(0, neogen_ns, 0, -1, {}) + local virt_text_extm = vim.api.nvim_buf_get_extmarks(0, neogen_virt_text_ns, 0, -1, {}) local position = current_position + 1 if #extm_list ~= 2 then local pos = { extm_list[position][2] + 1, extm_list[position][3] } + local virt_text = vim.tbl_filter(function(e) + return e[2] == extm_list[position][2] and e[3] == extm_list[position][3] + end, virt_text_extm)[1] + if #virt_text > 0 then + vim.api.nvim_buf_del_extmark(0, neogen_virt_text_ns, virt_text[1]) + end vim.api.nvim_win_set_cursor(0, pos) current_position = current_position + 1