L3MON4D3 / LuaSnip

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

I cannot trigger any snippet :( #899

Closed nyngwang closed 1 year ago

nyngwang commented 1 year ago

I don't know what I did wrong, but none of my keymaps cannot trigger a single snip. I also did all the default setup provided by the README of nvim-cmp, doesn't help.

return function (use)
  -------------------------------------------------------------------------------------------------
  -- Fun.
  -------------------------------------------------------------------------------------------------
  use {
    'L3MON4D3/LuaSnip',
    -- follow latest release.
    -- version = 'v1.*', -- Replace <CurrentMajor> by the latest released major (first number of latest release)
    -- install jsregexp (optional!).
    -- build = 'make install_jsregexp',
    config = function ()
      -- require('luasnip').setup {
      -- }
      local luasnip = require('luasnip')
      local types = require('luasnip.util.types')

      vim.keymap.set({ 'i', 's' }, '<C-j>', function ()
        if luasnip.expand_or_jumpable() then
          luasnip.expand_or_jump()
        end
      end)
      vim.keymap.set({ 'i', 's' }, '<M-k>', function () pcall(luasnip.jump, -1) end)
      vim.keymap.set({ 'i', 's' }, '<M-l>', function () pcall(luasnip.change_choice, 1) end)
      luasnip.config.set_config {
        history = true,
        updateevents = 'TextChanged,TextChangedI',

        ext_opts = {
          [types.choiceNode] = {
            active = {
              virt_text = { { '󱦱', 'Error' } }
            }
          }
        }
      }
      local events = require "luasnip.util.events"
      local t = luasnip.text_node
      local c = luasnip.choice_node
      local toexpand_count = 0
      luasnip.add_snippets(nil, {
        -- basic, don't need to know anything else
        --    arg 1: string
        --    arg 2: a node
        luasnip.s("simple", t "wow, you were right!"),

        -- callbacks table
        luasnip.s("toexpand", c(1, { t "hello", t "world", t "last" }), {
          callbacks = {
            [1] = {
              [events.enter] = function(--[[ node ]])
                toexpand_count = toexpand_count + 1
                print("Number of times entered:", toexpand_count)
              end,
            },
          },
        }),
      })
    end
  }
end
L3MON4D3 commented 1 year ago

Mhm, the add_snippets call is a bit wrong, if it gets nil for the filetype, the snippet-table should have the format {ft1 = {<snippets>}, ft2 = {<snippets>}}. Try passing "all" instead of nil

nyngwang commented 1 year ago

@L3MON4D3 Lol, thank you! That's the config I copy-pasted from TJ. Now it works:

off-topic

I'm not sure whether you know TJ made a nice video to promote your plugin, but you(ok maybe me) definitely need to notify him and the viewers(e.g. me) maybe from the comment section to let them know that some of the syntaxes are deprecated. E.g. in the video he used the old syntax to add snippets:

luasnip.snippets = {
  -- ...
}

That is: I followed the video, which is the reason why I created this issue. Apparently, the tutorial didn't work for me.

L3MON4D3 commented 1 year ago

Nice :+1: Yeah, we do mention that in the README, but I guess that's not suuuper discoverable :sweat_smile:

nyngwang commented 1 year ago

OK, I'm blind: https://github.com/L3MON4D3/LuaSnip#resources-for-new-users. 🤣 Closing this!