L3MON4D3 / LuaSnip

Snippet Engine for Neovim written in Lua.
Apache License 2.0
3.31k stars 234 forks source link

How to know if there's an active snippet session? #934

Closed MariaSolOs closed 1 year ago

MariaSolOs commented 1 year ago

This isn't a bug per se, I would just appreciate your help here 🙏🏻

So I'm trying out to figure out the way to disable copilot when I'm inside a snippet (since I use <tab> to both navigate inside a snippet and accept a copilot suggestion). I've tried the code below, but this doesn't fully work since I want to disable copilot when I'm navigating anywhere inside the snippet (mainly, when expand_or_locally_jumpable() is true), and reenable it afterwards.

            -- Disable suggestions when entering a snippet.
            vim.api.nvim_create_autocmd('User', {
                pattern = 'LuasnipInsertNodeEnter',
                callback = function()
                    copilot.toggle_auto_trigger()
                end,
            })
            vim.api.nvim_create_autocmd('User', {
                pattern = 'LuasnipInsertNodeLeave',
                callback = function()
                    copilot.toggle_auto_trigger()
                end,
            })

I would really appreciate it if you could guide me in finding a better event to achieve this :)

L3MON4D3 commented 1 year ago

Don't worry about using issues for asking questions, as far as I'm concerned that's what they're for :D

Is there some way to explicitly turn auto_trigger on/off? If so, you could just check expand_or_locally_jumpable on CursorMoved (or some other common event)

MariaSolOs commented 1 year ago

Thanks a lot <3 I ended up using this:

local set_trigger = function(trigger)
  vim.b.copilot_suggestion_auto_trigger = trigger
  vim.b.copilot_suggestion_hidden = not trigger
end

vim.api.nvim_create_autocmd('User', {
  pattern = { 'LuasnipInsertNodeEnter', 'LuasnipInsertNodeLeave' },
  callback = function()
    set_trigger(not luasnip.expand_or_locally_jumpable())
  end,
})

I didn't want to use another event like CursorMovedI since that's triggered so often and I was afraid I might end up with a weird race condition. So far this seems to work as expected :)

MariaSolOs commented 1 year ago

I'm closing this for now since I don't think there's anything here to fix, but thank you @L3MON4D3!

L3MON4D3 commented 1 year ago

You're welcome :D