From d16557c3e67c78b14881c1da872eebaf861deb50 Mon Sep 17 00:00:00 2001 From: danymat Date: Fri, 7 Jan 2022 19:44:41 +0100 Subject: [PATCH] feat: Allow users to jump previous (#13) --- README.md | 34 ++++++++++++++++------------ lua/neogen.lua | 11 ++++++++-- lua/neogen/utilities/cursor.lua | 39 ++++++++++++++++++++++++++++----- 3 files changed, 63 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 5748b50..40833d7 100644 --- a/README.md +++ b/README.md @@ -114,20 +114,26 @@ cmp.setup { -- You must set mapping if you want. mapping = { - [""] = cmp.mapping(function(fallback) - if vim.fn.pumvisible() == 1 then - vim.fn.feedkeys(t(""), "n") - elseif neogen.jumpable() then - vim.fn.feedkeys(t("lua require('neogen').jump_next()"), "") - elseif check_back_space() then - vim.fn.feedkeys(t(""), "n") - else - fallback() - end - end, { - "i", - "s", - }), + [""] = cmp.mapping(function(fallback) + if neogen.jumpable() then + vim.fn.feedkeys(t("lua require('neogen').jump_next()"), "") + else + fallback() + end + end, { + "i", + "s", + }), + [""] = cmp.mapping(function(fallback) + if neogen.jumpable(-1) then + vim.fn.feedkeys(t("lua require('neogen').jump_prev()"), "") + else + fallback() + end + end, { + "i", + "s", + }), }, ... } diff --git a/lua/neogen.lua b/lua/neogen.lua index 949a92f..6a8eca5 100644 --- a/lua/neogen.lua +++ b/lua/neogen.lua @@ -88,6 +88,7 @@ neogen.generate = function(opts) vim.fn.append(to_place, content) -- 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 -- Creates extmark for the beggining of the content neogen.utilities.cursor.create(to_place + 1, start_column) @@ -113,10 +114,16 @@ function neogen.jump_next() neogen.utilities.cursor.jump() end -function neogen.jumpable() - return neogen.utilities.cursor.jumpable() +function neogen.jump_prev() + neogen.utilities.cursor.jump_prev() end +function neogen.jumpable(reverse) + return neogen.utilities.cursor.jumpable(reverse) +end + + + function neogen.generate_command() vim.api.nvim_command('command! -range -bar Neogen lua require("neogen").generate()') end diff --git a/lua/neogen/utilities/cursor.lua b/lua/neogen/utilities/cursor.lua index 5d6bc63..d72673f 100644 --- a/lua/neogen/utilities/cursor.lua +++ b/lua/neogen/utilities/cursor.lua @@ -1,12 +1,14 @@ neogen.utilities.cursor = {} 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 --- @param line string --- @param col string --- @return number neogen.utilities.cursor.create = function(line, col) + current_position = 1 return vim.api.nvim_buf_set_extmark(0, neogen_ns, line - 1, col - 1, {}) end @@ -15,13 +17,13 @@ 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 position = current_position + 1 + 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) - if #extm_list ~= 0 then - vim.api.nvim_buf_del_extmark(0, neogen_ns, extm_list[1][1]) - end + current_position = current_position + 1 return true else return false @@ -45,6 +47,20 @@ neogen.utilities.cursor.jump = function(opts) 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 neogen.utilities.cursor.del_extmarks = function() 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 --- 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, {}) if #extm_list == 0 then 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 return false 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 return true else