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.
This commit is contained in:
Daniel Mathiot
2021-08-28 01:31:51 +02:00
parent 56b20fcd11
commit 499a4301fa
2 changed files with 25 additions and 12 deletions

View File

@@ -65,9 +65,7 @@ neogen.generate = function(opts)
if matched then if matched then
local split = vim.split(matched, "|", true) local split = vim.split(matched, "|", true)
if #split == 2 and neogen.configuration.input_after_comment == false then if #split == 2 and neogen.configuration.input_after_comment == false then
return string.gsub(v, jump_text .. "|", "") .. " " return string.gsub(v, jump_text .. "|", "")
elseif #split == 1 then
string.gsub(v, jump_text, "")
end end
else else
return string.gsub(v, jump_text, "") return string.gsub(v, jump_text, "")
@@ -93,7 +91,9 @@ neogen.generate = function(opts)
neogen.utilities.cursor.create(to_place + i, input_start) neogen.utilities.cursor.create(to_place + i, input_start)
end end
end end
neogen.utilities.cursor.jump() if neogen.utilities.cursor.jumpable() then
neogen.utilities.cursor.jump({ first_time = true })
end
end end
end end
end end
@@ -129,7 +129,7 @@ neogen.setup = function(opts)
if neogen.configuration.enabled == true then if neogen.configuration.enabled == true then
neogen.generate_command() neogen.generate_command()
vim.api.nvim_set_keymap("i", neogen.configuration.jump_map, "<cmd>lua require('neogen').jump_next()<CR>", { }) vim.api.nvim_set_keymap("i", neogen.configuration.jump_map, "<cmd>lua require('neogen').jump_next()<CR>", {})
end end
end end

View File

@@ -12,10 +12,13 @@ end
--- Find next created extmark and goes to it. --- Find next created extmark and goes to it.
--- It removes the extmark afterwards. --- 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, {}) local extm_list = vim.api.nvim_buf_get_extmarks(0, neogen_ns, 0, -1, {})
P(extm_list)
if #extm_list ~= 0 then 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 if #extm_list ~= 0 then
vim.api.nvim_buf_del_extmark(0, neogen_ns, extm_list[1][1]) vim.api.nvim_buf_del_extmark(0, neogen_ns, extm_list[1][1])
end end
@@ -25,9 +28,19 @@ neogen.utilities.cursor.go_next_extmark = function()
end end
end end
--- Goes to next extmark and start insert mode --- Goes to next extmark and start insert mode.
neogen.utilities.cursor.jump = function() --- If `opts.first_time` is supplied, will try to go to normal mode before going to extmark
if neogen.utilities.cursor.go_next_extmark() then --- @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") vim.api.nvim_command("startinsert")
end end
end end
@@ -41,7 +54,7 @@ neogen.utilities.cursor.del_extmarks = function()
end end
--- Checks if there are still possible jump positions to perform --- 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, {}) 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 true return true