norcalli / snippets.nvim

GNU General Public License v3.0
277 stars 13 forks source link

Integration with nvim-compe #29

Open lovesegfault opened 3 years ago

lovesegfault commented 3 years ago

Currently nvim-compe has an example of how it can be used with vsnip:

local t = function(str)
  return vim.api.nvim_replace_termcodes(str, true, true, true)
end

local check_back_space = function()
    local col = vim.fn.col('.') - 1
    return col == 0 or vim.fn.getline('.'):sub(col, col):match('%s') ~= nil
end

-- Use (s-)tab to:
--- move to prev/next item in completion menuone
--- jump to prev/next snippet's placeholder
_G.tab_complete = function()
  if vim.fn.pumvisible() == 1 then
    return t "<C-n>"
  elseif vim.fn['vsnip#available'](1) == 1 then
    return t "<Plug>(vsnip-expand-or-jump)"
  elseif check_back_space() then
    return t "<Tab>"
  else
    return vim.fn['compe#complete']()
  end
end
_G.s_tab_complete = function()
  if vim.fn.pumvisible() == 1 then
    return t "<C-p>"
  elseif vim.fn['vsnip#jumpable'](-1) == 1 then
    return t "<Plug>(vsnip-jump-prev)"
  else
    -- If <S-Tab> is not working in your terminal, change it to <C-h>
    return t "<S-Tab>"
  end
end

Is there an easy easy way to use snippets.nvim here? I'm especially unsure of what the equivalent to vsnip#available is.

Thanks!

gpanders commented 3 years ago

You can use this:

local function check_snippet()
    if require("snippets").has_active_snippet() then
        return true
    end

    local _, snippet = require("snippets").lookup_snippet_at_cursor()
    return snippet ~= nil
end

Unfortunately snippets.nvim doesn't expose a public function to check whether or not a snippet expansion would occur, but the function above does effectively that.

For the <S-Tab> portion, you can just use require("snippets").has_active_snippet().