From 499a4301fad9417254100ded982bf9bd4267e081 Mon Sep 17 00:00:00 2001 From: Daniel Mathiot Date: Sat, 28 Aug 2021 01:31:51 +0200 Subject: [PATCH] Fix first time insert mode I did a silly workaround because when at normal mode and going to insert mode, it is not the same as being in insert mode and going to insert mode. Somehow the cursor jumping is broken. So what i did was to call a insert_mode a first time before any actual jumping. --- lua/neogen.lua | 12 ++++++------ lua/neogen/utilities/cursor.lua | 25 +++++++++++++++++++------ 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/lua/neogen.lua b/lua/neogen.lua index c64289d..cf8db68 100644 --- a/lua/neogen.lua +++ b/lua/neogen.lua @@ -65,14 +65,12 @@ neogen.generate = function(opts) if matched then local split = vim.split(matched, "|", true) if #split == 2 and neogen.configuration.input_after_comment == false then - return string.gsub(v, jump_text .. "|", "") .. " " - elseif #split == 1 then - string.gsub(v, jump_text, "") + return string.gsub(v, jump_text .. "|", "") end else return string.gsub(v, jump_text, "") end - + return string.gsub(v, pattern, "") end @@ -93,7 +91,9 @@ neogen.generate = function(opts) neogen.utilities.cursor.create(to_place + i, input_start) end end - neogen.utilities.cursor.jump() + if neogen.utilities.cursor.jumpable() then + neogen.utilities.cursor.jump({ first_time = true }) + end end end end @@ -129,7 +129,7 @@ neogen.setup = function(opts) if neogen.configuration.enabled == true then neogen.generate_command() - vim.api.nvim_set_keymap("i", neogen.configuration.jump_map, "lua require('neogen').jump_next()", { }) + vim.api.nvim_set_keymap("i", neogen.configuration.jump_map, "lua require('neogen').jump_next()", {}) end end diff --git a/lua/neogen/utilities/cursor.lua b/lua/neogen/utilities/cursor.lua index e6616ce..848d8e9 100644 --- a/lua/neogen/utilities/cursor.lua +++ b/lua/neogen/utilities/cursor.lua @@ -12,10 +12,13 @@ end --- Find next created extmark and goes to it. --- It removes the extmark afterwards. -neogen.utilities.cursor.go_next_extmark = function() +neogen.utilities.cursor.go_next_extmark = function(first_time) local extm_list = vim.api.nvim_buf_get_extmarks(0, neogen_ns, 0, -1, {}) + P(extm_list) if #extm_list ~= 0 then - vim.api.nvim_win_set_cursor(0, { extm_list[1][2] + 1, extm_list[1][3] }) + local pos = { extm_list[1][2] + 1, extm_list[1][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 @@ -25,9 +28,19 @@ neogen.utilities.cursor.go_next_extmark = function() end end ---- Goes to next extmark and start insert mode -neogen.utilities.cursor.jump = function() - if neogen.utilities.cursor.go_next_extmark() then +--- Goes to next extmark and start insert mode. +--- If `opts.first_time` is supplied, will try to go to normal mode before going to extmark +--- @param opts table +neogen.utilities.cursor.jump = function(opts) + opts = opts or {} + + -- This is weird, the first time nvim goes to insert is not the same as when i'm already on insert mode + -- that's why i put a first_time flag + if opts.first_time then + vim.api.nvim_command("startinsert") + end + + if neogen.utilities.cursor.go_next_extmark(opts.first_time) then vim.api.nvim_command("startinsert") end end @@ -41,7 +54,7 @@ neogen.utilities.cursor.del_extmarks = function() end --- Checks if there are still possible jump positions to perform -neogen.utilities.cursor.jumpable = function () +neogen.utilities.cursor.jumpable = function() local extm_list = vim.api.nvim_buf_get_extmarks(0, neogen_ns, 0, -1, {}) if #extm_list ~= 0 then return true