dcampos / nvim-snippy

Snippet plugin for Neovim written in Lua
MIT License
310 stars 18 forks source link

How to clean all jump marks after switched to normal mode? #97

Closed AllenDang closed 1 year ago

AllenDang commented 1 year ago

Problem Statement

Say if current snippet is defined like this

snippet fr
  for $1, $2 := range($3) {
    $4
  }$0

Press ESC to switch to normal mode after jumped to the $4 mark, move the cursor to some place, press "i", press "tab", cursor will still jump to previous snippet's $0 mark.

Ideas or possible solutions

When i press "ESC" to switch to normal mode, all marks of previous snippet should not remain valid.

Alternatives you have considered

Any solution for it? I used to use autocmd to manually disable all jump marks of luasnip when I switched to normal mode.

dcampos commented 1 year ago

You could use an autocmd to run require('snippy.buf').clear_state(), which will clear all jump marks.

AllenDang commented 1 year ago

@dcampos It's not as easy as I think. I create an autocmd for ModeChange and specify pattern for any mode to normal mode, the snippy jump will be totally disabled for nvim-cmp, reason is, when I hit enter to complete a auto-complete from lsp, the mode will change three times, i->n, n->v, v->s.

How to enable to snippy jump after nvim-cmp's complete and still clean the jump marks when I actually hit esc?

Here is what I did right now.

function leave_snippet()
  if (vim.v.event.old_mode == "s" and vim.v.event.new_mode == "n") or vim.v.event.old_mode == "i" then
    require("snippy.buf").clear_state()
  end
end

-- stop snippets when you leave to normal mode
vim.api.nvim_command([[
     autocmd ModeChanged * lua leave_snippet()
 ]])
dcampos commented 1 year ago

I see. Maybe you could set up a mapping for that? This one worked for me:

vim.keymap.set({'i', 's'}, '<Esc>', function()
    if require('snippy').is_active() then
        require('snippy.buf').clear_state()
    end
    return '<Esc>'
end, { expr = true })