feat: Return to previous position after las cycling (closes #35)

This commit is contained in:
danymat
2022-01-10 17:23:53 +01:00
parent 6be3e414b7
commit 40b53dea83
2 changed files with 15 additions and 16 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, true) neogen.utilities.cursor.create(to_place + 1, start_column)
-- 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
@@ -106,8 +106,12 @@ neogen.generate = function(opts)
end end
end end
-- Create extmark to jump back to current location
local pos = vim.api.nvim_win_get_cursor(0)
neogen.utilities.cursor.create(pos[1], pos[2])
-- 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, true) neogen.utilities.cursor.create(to_place + #content + 1, 0)
neogen.utilities.cursor.jump({ first_time = true }) neogen.utilities.cursor.jump({ first_time = true })
end end

View File

@@ -5,15 +5,12 @@ 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 number
--- @param col string --- @param col number
--- @return number --- @return number
neogen.utilities.cursor.create = function(line, col, delimiters) neogen.utilities.cursor.create = function(line, col)
local opts = delimiters == true and {}
or { virt_text = { { "TODO", "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
@@ -22,18 +19,13 @@ 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
table.sort(extm_list, function(a, b)
return a[1] < b[1]
end)
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 and #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
return true return true
@@ -61,6 +53,9 @@ end
neogen.utilities.cursor.jump_prev = function() neogen.utilities.cursor.jump_prev = function()
local marks = vim.api.nvim_buf_get_extmarks(0, neogen_ns, 0, -1, {}) local marks = vim.api.nvim_buf_get_extmarks(0, neogen_ns, 0, -1, {})
table.sort(marks, function(a, b)
return a[1] < b[1]
end)
if #marks == 2 then if #marks == 2 then
return false return false