Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Postfix snippets from Luasnip are not expanded correctly #533

Open
2 tasks done
bartoszb-rgb opened this issue Dec 13, 2024 · 9 comments
Open
2 tasks done

Postfix snippets from Luasnip are not expanded correctly #533

bartoszb-rgb opened this issue Dec 13, 2024 · 9 comments
Labels
bug Something isn't working

Comments

@bartoszb-rgb
Copy link

Make sure you have done the following

  • I have updated to the latest version of blink.cmp
  • I have read the README

Bug Description

When selecting postfix snippet it appears after TS "context".
bug-show

Relevant configuration

snippets = {
      expand = function(snippet)
        require('luasnip').lsp_expand(snippet)
      end,
      active = function(filter)
        if filter and filter.direction then
          return require('luasnip').jumpable(filter.direction)
        end
        return require('luasnip').in_snippet()
      end,
      jump = function(direction) require('luasnip').jump(direction) end,
    },

neovim version

NVIM v0.10.2 Build type: Release LuaJIT 2.1.1727870382 Run "nvim -V1 -v" for more info

blink.cmp version: branch, tag, or commit

v0.7.6

@bartoszb-rgb bartoszb-rgb added the bug Something isn't working label Dec 13, 2024
@westernwontons
Copy link

westernwontons commented Dec 18, 2024

I've found the solution for postfix snippets defined with Luasnip:

keymap = {
            ["<C-f>"] = {
                function(cmp)
                    vim.schedule(function()
                        local ls = require "luasnip"
                        if ls.expandable() then
                            ls.expand()
                        else
                            cmp.select_and_accept()
                        end
                    end)
                end
        }

<C-f> is used to expand a snippet. Wrap the entire thing in vim.schedule and postfix snippets are expanded correctly.

@bartoszb-rgb
Copy link
Author

I would like to select elements without any additional keymaps.
I did similar workaround already, but it is annoying that I need to remember it.

@westernwontons
Copy link

I'm not sure what you mean. Do you mind giving an example?

@bartoszb-rgb
Copy link
Author

Sorry I though you suggested new keymap just for it. I tried your solution and it seems working partially.
Still after selection of entry and pressing keymap behaviour is the same. Snipped is not being correctly expanded.

@westernwontons
Copy link

Can you please dump your entire blink.cmp config?

@bartoszb-rgb
Copy link
Author

return {
  'saghen/blink.cmp',
  lazy = false, -- lazy loading handled internally

  version = 'v0.*',
  opts = {
    keymap = {
      preset = 'super-tab',
      ['<CR>'] = { 'accept', 'fallback' },
      -- fix attempt
      ['<C-g>'] = { function(cmp)
        vim.schedule(function()
          local ls = require "luasnip"
          if ls.expandable() then
            ls.expand()
          else
            cmp.select_and_accept()
          end
        end)
      end },


      ['<S-Tab>'] = { 'select_prev', 'fallback' },
      ['<Tab>'] = { 'select_next', 'fallback' },
      ['<C-n>'] = {
        function(cmp)
          if cmp.snippet_active() then
            return cmp.accept()
          else
            return cmp.select_and_accept()
          end
        end,
        'snippet_forward',
        'fallback'
      },
      ['<C-p>'] = { 'snippet_backward', 'fallback' },
    },

    completion = {
      trigger = {
        show_in_snippet = false
      },
      list = {
        selection = "auto_insert"
      }
    },

    documentation = {
      auto_show = false,
    },

    appearance = {
      use_nvim_cmp_as_default = true,
      nerd_font_variant = 'mono'
    },

    sources = {
      completion = {
        enabled_providers = function(ctx)
           -- providers func body
        end
      },
      providers = {
      -- non related providers
      },

    snippets = {
      expand = function(snippet)
          require('luasnip').lsp_expand(snippet)
      end,
      active = function(filter)
        if filter and filter.direction then
          return require('luasnip').jumpable(filter.direction)
        end
        return require('luasnip').in_snippet()
      end,
      jump = function(direction) require('luasnip').jump(direction) end,
    },
  },

  opts_extend = { "sources.completion.enabled_providers" }
}

@westernwontons
Copy link

westernwontons commented Dec 19, 2024

I would recommend updating to the latest version first. Here's the updated config:

--- @type LazySpec
return {
    "saghen/blink.cmp",
    dependencies = "L3MON4D3/LuaSnip",
    lazy = false, -- lazy loading handled internally

    -- version = 'v0.*',
    ---@module 'blink.cmp'
    ---@type blink.cmp.Config
    opts = {
        keymap = {
            preset = "super-tab",
            ["<CR>"] = { "accept", "fallback" },
            -- fix attempt
            ["<C-g>"] = {
                function(cmp)
                    vim.schedule(function()
                        local ls = require "luasnip"
                        if ls.expandable() then
                            ls.expand()
                        else
                            cmp.select_and_accept()
                        end
                    end)
                end,
            },

            ["<S-Tab>"] = { "select_prev", "fallback" },
            ["<Tab>"] = { "select_next", "fallback" },
            ["<C-n>"] = {
                function(cmp)
                    if cmp.snippet_active() then
                        return cmp.accept()
                    else
                        return cmp.select_and_accept()
                    end
                end,
                "snippet_forward",
                "fallback",
            },
            ["<C-p>"] = { "snippet_backward", "fallback" },
        },

        completion = {
            trigger = {
                show_in_snippet = false,
            },
            list = {
                selection = "auto_insert",
            },
        },

        documentation = {
            auto_show = false,
        },

        appearance = {
            use_nvim_cmp_as_default = true,
            nerd_font_variant = "mono",
        },

        sources = {
            completion = {
                default = { "lsp", "buffer", "path", "luasnip" },
            },
            providers = {
                -- non related providers
            },

            snippets = {
                expand = function(snippet)
                    require("luasnip").lsp_expand(snippet)
                end,
                active = function(filter)
                    if filter and filter.direction then
                        return require("luasnip").jumpable(filter.direction)
                    end
                    return require("luasnip").in_snippet()
                end,
                jump = function(direction)
                    require("luasnip").jump(direction)
                end,
            },
        },

        opts_extend = { "sources.default" },
    },
}

I made blink.cmp depend on luasnip and commented out the explicitly specified version plus added the new sources.default key in place of enabled_providers.

After updating the config, I would recommend that you delete blink through Lazy by running :Lazy, selecting blink and pressing x. Then blink will appear at the top under 'Not installed'. Hover your cursor over blink.cmp then press i to install and then it should work. If it doesn't, I would recommend installing from source by adding build = "cargo build --release" to instruct Lazy to build blink.cmp from source. Let me know how it goes!

@bartoszb-rgb
Copy link
Author

Hey, I followed your steps, adjusted the configuration and dived deep into testing.
I found out couple of things:

  • Your work around works partially. It can not be assign to <CR> and fallback logic is not working since it is wrapped in vim.schedule
  • When completion.list.selection is set to auto_select then additional dot is being added before snippet while picking option. It might be the reason why it is not working correctly in general.
  • When on manual or preselect mode the normal accept works partially, leaving the dot at the end of capture.

@westernwontons
Copy link

For <CR> I have the following defined, which expands snippets as expected on my setup:

            ["<cr>"] = {
                function(cmp)
                    if cmp.is_visible() then return cmp.accept() end
                end,
                "fallback",
            },

When completion.list.selection is set to auto_select then additional dot is being added before snippet while picking option. It might be the reason why it is not working correctly in general.

When on manual or preselect mode the normal accept works partially, leaving the dot at the end of capture.

I'm not experiencing those issues at all, so unfortunately I'm unable to provide hints there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants