garymjr / nvim-snippets

Snippet support using native neovim snippets
MIT License
215 stars 13 forks source link

Check if word before cursor is expandable snippet #38

Closed sQVe closed 3 months ago

sQVe commented 3 months ago

Hey,

I previously used find_snippet_prefix, which was removed in commit https://github.com/garymjr/nvim-snippets/commit/8f85fc2a27d54434a22e9d9bc71655ce632fbef0, to conditionally expand snippets outside of the built-in nvim-cmp expand functionality.

I explicitly use the following method in my nvim-cmp keymap to allow expanding both snippets and ai suggestions with Ctrl + l:

M.expand_snippet_or_suggestion = function(fallback)
  local suggestion = require('supermaven-nvim.completion_preview')
  local find_snippet_prefix = require('snippets.utils').find_snippet_prefix

  local word_before_cursor, row, col = cursor.get_word_before()
  local snippet = find_snippet_prefix(word_before_cursor)

  if snippet ~= nil then
    cursor.clear_before_by_length(#word_before_cursor)
    vim.snippet.expand(snippet.body)
  elseif suggestion.has_suggestion() then
    suggestion.on_accept_suggestion()
  else
    fallback()
  end
end

See permalink for further context.


Could this plugin provide a suitable API for cases like this? If not, could you consider keeping utilities like find_snippet_prefix that make implementations like this easier?

Cheers 🍻

garymjr commented 3 months ago

I added require('snippets.utils').find_snippet_prefix back. I like the idea of adding a helper function to allow expanding the word at the cursor though, so I'll look into getting that added. Thanks for the suggestion!