supermaven-inc / supermaven-nvim

The official Neovim plugin for Supermaven
https://supermaven.com/
MIT License
619 stars 30 forks source link

Programmatically accept suggestion #15

Closed sQVe closed 4 months ago

sQVe commented 4 months ago

Hey,

I would love to be able to interact with this plugin via Lua code. This is super useful when configuring AI suggestions together with LSP completion.

This is how I set up accepting both Luasnip snippets and GitHub Copilot under the save key via nvim-cmp:

M.expand = function(fallback)
  local luasnip = require('luasnip')
  local suggestion = require('copilot.suggestion')

  if luasnip.expandable() then
    luasnip.expand()
  elseif suggestion.is_visible() then
    suggestion.accept()
  else
    fallback()
  end
end

Cheers!

hboon commented 4 months ago

Second this. Just mapping to a function like this, but falling back to <Tab> worked for me yesterday, but it no longer works.

kaiiserni commented 4 months ago

As of the last PR, something like this should work btw:

require("supermaven-nvim").setup({
  disable_keymaps = true
})

...

M.expand = function(fallback)
  local luasnip = require('luasnip')
  local suggestion = require('supermaven-nvim.completion_preview')

  if luasnip.expandable() then
    luasnip.expand()
  elseif suggestion.has_suggestion() then
    suggestion.on_accept_suggestion()
  else
    fallback()
  end
end 
hboon commented 4 months ago

Just in case this saves someone's time in the future, there is also expand_or_jumpable() and expand_or_jump().

sQVe commented 4 months ago

Closing issue since https://github.com/supermaven-inc/supermaven-nvim/issues/15#issuecomment-2119390970 provides a solution.