L3MON4D3 / LuaSnip

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

expand_or_jumpable return false for lsp snippets and custom snippets #1218

Open voidkrc opened 3 months ago

voidkrc commented 3 months ago

I am currently at commit 7552e65 and when running this code, the snippet does not expand. This happens for both LSP snippets and custom snippets.

Configuration file:

local cmp = require("cmp")
local ls = require("luasnip")

local M = {}

local function load_cmp_config()
  local cmp_select = { behavior = cmp.SelectBehavior.Select }

  cmp.setup({
    sources = {
      { name = "nvim_lsp" },
      { name = "nvim_lua" },
      { name = "luasnip" },
      { name = "path" },
      { name = "buffer" },
    },

    mapping = {
      ["<C-p>"] = cmp.mapping.select_prev_item(cmp_select),
      ["<C-n>"] = cmp.mapping.select_next_item(cmp_select),
      ["<C-y>"] = cmp.mapping.confirm({ select = true }),
    },

    snippet = {
      expand = function(args)
        ls.lsp_expand(args.body)
      end,
    },
  })
end

local function load_luasnip_config()
  local s = ls.snippet
  local t = ls.text_node

  ls.add_snippets("c", { s("testc", { t("Hello World") }) })

  -- Luasnip configuration
  ls.config.set_config({
    history = true,
    updateevents = "TextChanged,TextChangedI",
    --override_builtin = true,
  })

  vim.keymap.set({ "i", "s" }, "<C-k>", function()
    if ls.expand_or_jumpable() then
      print("Expanding or jumping")
      ls.expand_or_jump()
    else
      print("Not expand or jumpable")
    end
  end, { silent = true, noremap = true })

  vim.keymap.set({ "i", "s" }, "<C-j>", function()
    if ls.jumpable(-1) then
      ls.jump(-1)
    end
  end, { silent = true, noremap = true })
end

M.config = function()
  load_cmp_config()
  load_luasnip_config()
end

return M

Plugin file:

return {
  "hrsh7th/nvim-cmp",
  dependencies = {
    "hrsh7th/cmp-buffer",
    "hrsh7th/cmp-nvim-lua",
    "hrsh7th/cmp-path",
    "hrsh7th/cmp-nvim-lsp",
    "saadparwaiz1/cmp_luasnip",
    { "L3MON4D3/LuaSnip", build = "make install_jsregexp" },
  },
  config = function()
    require("voidkrc.config.completion").config()
  end,
}

Example of what I see:

Screenshot 2024-08-01 at 21 52 41 Screenshot 2024-08-01 at 21 52 57
voidkrc commented 3 months ago

To add on this, I can see my custom snippets in the cmp window.

Screenshot 2024-08-01 at 21 28 36
L3MON4D3 commented 3 months ago

Hi :) It seems a bit like you're conflating what luasnip does and what cmp/cmp_luasnip does, so to make sure that's not the case I'll outline what each of them is responsible for: ls.expand_or_jumpable() returns whether the current snippet is jumpable or whether the text immediately before the cursor matches a snippet added via add_snippets. This does not include lsp-snippets! cmp_luasnip takes all snippets added via add_snippets and lists them as a cmp-source. This means all snippets can be expanded from the cmp-list by accepting the item (<C-y> in your case) cmp (or, the nvim_lsp-source) takes snippets generated by the languageserver, and, upon accepting the item, it expands them via the ls.lsp_expand()-function.

Does this help clear the issue, or were we on the same page before? :D

voidkrc commented 3 months ago

Hi :) It seems a bit like you're conflating what luasnip does and what cmp/cmp_luasnip does, so to make sure that's not the case I'll outline what each of them is responsible for: ls.expand_or_jumpable() returns whether the current snippet is jumpable or whether the text immediately before the cursor matches a snippet added via add_snippets. This does not include lsp-snippets! cmp_luasnip takes all snippets added via add_snippets and lists them as a cmp-source. This means all snippets can be expanded from the cmp-list by accepting the item (<C-y> in your case) cmp (or, the nvim_lsp-source) takes snippets generated by the languageserver, and, upon accepting the item, it expands them via the ls.lsp_expand()-function.

Does this help clear the issue, or were we on the same page before? :D

Thanks @L3MON4D3 , I got confused because I thought luasnip would match the snippet even if i wasn't writing the full word. Is there a way to implement that?

L3MON4D3 commented 3 months ago

luasnip only expands snippets if the whole trigger matches, cmp_luasnip should also list snippet-items if there is a partial match with the typed text. So #incl -> list snippet-items with cmp -> select one with <C-y> -> snippet expands. At least that's how that should work. Is this not working for you?