feat: add extmarks on jumps

This commit is contained in:
danymat
2022-01-09 21:36:34 +01:00
parent e3445e8f88
commit cb3569dda1
2 changed files with 15 additions and 4 deletions

View File

@@ -91,7 +91,7 @@ neogen.generate = function(opts)
-- First and last extmarks are needed to know the range of inserted content -- First and last extmarks are needed to know the range of inserted content
if neogen.configuration.input_after_comment == true then if neogen.configuration.input_after_comment == true then
-- Creates extmark for the beggining of the content -- 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 -- Creates extmarks for the content
for i, value in pairs(content_with_marks) do for i, value in pairs(content_with_marks) do
local start = 0 local start = 0
@@ -107,7 +107,7 @@ neogen.generate = function(opts)
end end
-- Creates extmark for the end of the content -- 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 }) neogen.utilities.cursor.jump({ first_time = true })
end end

View File

@@ -1,15 +1,19 @@
neogen.utilities.cursor = {} neogen.utilities.cursor = {}
local neogen_ns = vim.api.nvim_create_namespace("neogen") 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 local current_position = 1
--- Wrapper around set_extmark with 1-based numbering for `line` and `col`, and returns the id of the created extmark --- Wrapper around set_extmark with 1-based numbering for `line` and `col`, and returns the id of the created extmark
--- @param line string --- @param line string
--- @param col string --- @param col string
--- @return number --- @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 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, {}) return vim.api.nvim_buf_set_extmark(0, neogen_ns, line - 1, new_col, {})
end end
@@ -18,10 +22,17 @@ end
--- First jumpable extmark is the one after the extmarks responsible of start/end of annotation --- First jumpable extmark is the one after the extmarks responsible of start/end of annotation
neogen.utilities.cursor.go_next_extmark = function() neogen.utilities.cursor.go_next_extmark = function()
local extm_list = vim.api.nvim_buf_get_extmarks(0, neogen_ns, 0, -1, {}) 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 local position = current_position + 1
if #extm_list ~= 2 then if #extm_list ~= 2 then
local pos = { extm_list[position][2] + 1, extm_list[position][3] } 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) vim.api.nvim_win_set_cursor(0, pos)
current_position = current_position + 1 current_position = current_position + 1