feat: Allow users to jump previous (#13)
This commit is contained in:
34
README.md
34
README.md
@@ -114,20 +114,26 @@ cmp.setup {
|
|||||||
|
|
||||||
-- You must set mapping if you want.
|
-- You must set mapping if you want.
|
||||||
mapping = {
|
mapping = {
|
||||||
["<tab>"] = cmp.mapping(function(fallback)
|
["<tab>"] = cmp.mapping(function(fallback)
|
||||||
if vim.fn.pumvisible() == 1 then
|
if neogen.jumpable() then
|
||||||
vim.fn.feedkeys(t("<C-n>"), "n")
|
vim.fn.feedkeys(t("<cmd>lua require('neogen').jump_next()<CR>"), "")
|
||||||
elseif neogen.jumpable() then
|
else
|
||||||
vim.fn.feedkeys(t("<cmd>lua require('neogen').jump_next()<CR>"), "")
|
fallback()
|
||||||
elseif check_back_space() then
|
end
|
||||||
vim.fn.feedkeys(t("<tab>"), "n")
|
end, {
|
||||||
else
|
"i",
|
||||||
fallback()
|
"s",
|
||||||
end
|
}),
|
||||||
end, {
|
["<S-tab>"] = cmp.mapping(function(fallback)
|
||||||
"i",
|
if neogen.jumpable(-1) then
|
||||||
"s",
|
vim.fn.feedkeys(t("<cmd>lua require('neogen').jump_prev()<CR>"), "")
|
||||||
}),
|
else
|
||||||
|
fallback()
|
||||||
|
end
|
||||||
|
end, {
|
||||||
|
"i",
|
||||||
|
"s",
|
||||||
|
}),
|
||||||
},
|
},
|
||||||
...
|
...
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -88,6 +88,7 @@ neogen.generate = function(opts)
|
|||||||
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
|
||||||
|
-- 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)
|
||||||
@@ -113,10 +114,16 @@ function neogen.jump_next()
|
|||||||
neogen.utilities.cursor.jump()
|
neogen.utilities.cursor.jump()
|
||||||
end
|
end
|
||||||
|
|
||||||
function neogen.jumpable()
|
function neogen.jump_prev()
|
||||||
return neogen.utilities.cursor.jumpable()
|
neogen.utilities.cursor.jump_prev()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function neogen.jumpable(reverse)
|
||||||
|
return neogen.utilities.cursor.jumpable(reverse)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function neogen.generate_command()
|
function neogen.generate_command()
|
||||||
vim.api.nvim_command('command! -range -bar Neogen lua require("neogen").generate()')
|
vim.api.nvim_command('command! -range -bar Neogen lua require("neogen").generate()')
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,12 +1,14 @@
|
|||||||
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 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)
|
||||||
|
current_position = 1
|
||||||
return vim.api.nvim_buf_set_extmark(0, neogen_ns, line - 1, col - 1, {})
|
return vim.api.nvim_buf_set_extmark(0, neogen_ns, line - 1, col - 1, {})
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -15,13 +17,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 position = current_position + 1
|
||||||
|
|
||||||
if #extm_list ~= 2 then
|
if #extm_list ~= 2 then
|
||||||
local pos = { extm_list[2][2] + 1, extm_list[2][3] }
|
local pos = { extm_list[position][2] + 1, extm_list[position][3] }
|
||||||
|
|
||||||
vim.api.nvim_win_set_cursor(0, pos)
|
vim.api.nvim_win_set_cursor(0, pos)
|
||||||
if #extm_list ~= 0 then
|
current_position = current_position + 1
|
||||||
vim.api.nvim_buf_del_extmark(0, neogen_ns, extm_list[1][1])
|
|
||||||
end
|
|
||||||
return true
|
return true
|
||||||
else
|
else
|
||||||
return false
|
return false
|
||||||
@@ -45,6 +47,20 @@ neogen.utilities.cursor.jump = function(opts)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
neogen.utilities.cursor.jump_prev = function()
|
||||||
|
local marks = vim.api.nvim_buf_get_extmarks(0, neogen_ns, 0, -1, {})
|
||||||
|
|
||||||
|
if #marks == 2 then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
local position = current_position - 1
|
||||||
|
local pos = { marks[position][2] + 1, marks[position][3] }
|
||||||
|
vim.api.nvim_win_set_cursor(0, pos)
|
||||||
|
current_position = current_position - 1
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
--- Delete all active extmarks
|
--- Delete all active extmarks
|
||||||
neogen.utilities.cursor.del_extmarks = function()
|
neogen.utilities.cursor.del_extmarks = function()
|
||||||
local extmarks = vim.api.nvim_buf_get_extmarks(0, neogen_ns, 0, -1, {})
|
local extmarks = vim.api.nvim_buf_get_extmarks(0, neogen_ns, 0, -1, {})
|
||||||
@@ -55,7 +71,7 @@ end
|
|||||||
|
|
||||||
--- Checks if there are still possible jump positions to perform
|
--- Checks if there are still possible jump positions to perform
|
||||||
--- Verifies if the cursor is in the last annotated part
|
--- Verifies if the cursor is in the last annotated part
|
||||||
neogen.utilities.cursor.jumpable = function()
|
neogen.utilities.cursor.jumpable = function(reverse)
|
||||||
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, {})
|
||||||
if #extm_list == 0 then
|
if #extm_list == 0 then
|
||||||
return false
|
return false
|
||||||
@@ -64,6 +80,19 @@ neogen.utilities.cursor.jumpable = function()
|
|||||||
if cursor[1] > extm_list[#extm_list][2] or cursor[1] < extm_list[1][2] then
|
if cursor[1] > extm_list[#extm_list][2] or cursor[1] < extm_list[1][2] then
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- We arrive at the end, we can't jump anymore
|
||||||
|
if current_position == #extm_list then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
if reverse == -1 then
|
||||||
|
-- Check first boundaries
|
||||||
|
if current_position == 1 then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
if #extm_list > 2 then
|
if #extm_list > 2 then
|
||||||
return true
|
return true
|
||||||
else
|
else
|
||||||
|
|||||||
Reference in New Issue
Block a user