hrsh7th / nvim-cmp

A completion plugin for neovim coded in Lua.
MIT License
8.16k stars 407 forks source link

snippet getting confirm and not preselect when doing cmp.select_next_item #1940

Open GZLiew opened 6 months ago

GZLiew commented 6 months ago

FAQ

Announcement

Minimal reproducible full config

cmp.setup({
  snippet = {
    expand = function(args)
      vim.fn["vsnip#anonymous"](args.body)
    end,
  },
  mapping = {
        ['<C-n>'] = function(fallback)
        if cmp.visible() then
          cmp.select_next_item()
        else
          fallback()
        end
      end,
        ['<C-p>'] = function(fallback)
        if cmp.visible() then
          cmp.select_prev_item()
        else
          fallback()
        end
      end,
    ['<C-y>'] = cmp.config.disable,
    ['<C-Space>'] = cmp.mapping(cmp.mapping.complete(), { 'i', 'c', 's', 'v' }),
    ['<C-f>'] = cmp.mapping.confirm({ select = true }),
    ['<Tab>'] = cmp.mapping.confirm({ select = true }),
    ['<CR>'] = cmp.mapping.confirm({ select = true }),
  },
  sources = cmp.config.sources({
    { name = 'vsnip' },
    { name = 'nvim_lsp' },
    { name = 'buffer' },
  })
})

Description

https://github.com/hrsh7th/nvim-cmp/assets/43195293/b8d59787-1bc6-45c9-a8f2-c216ab56b1ee

Steps to reproduce

https://github.com/hrsh7th/nvim-cmp/assets/43195293/b8d59787-1bc6-45c9-a8f2-c216ab56b1ee

Expected behavior

should preselect the trycatch snippet and not select and hide cmp dialog

Actual behavior

selected the trycatch snippet and hides cmp dialog

Additional context

No response

KeqiZeng commented 6 months ago

Facing the same issue when select some rust macros. Keystroke sequence: print<tab>

https://github.com/hrsh7th/nvim-cmp/assets/75246050/9b2a87fd-8acd-4701-b202-ed409d4a845b

KeqiZeng commented 6 months ago

Facing the same issue when select some rust macros. Keystroke sequence: print<tab>

Screen.Recording.2024-06-02.at.17.11.34.mov

mapping = {
    ["<Tab>"] = cmp.mapping(function(fallback)
        if cmp.visible() then
            cmp.select_next_item()
        elseif vim.snippet.active({ direction = 1 }) then
            vim.schedule(function()
                vim.snippet.jump(1)
            end)
        elseif has_words_before() then
            cmp.complete()
        else
            fallback()
        end
    end, { "i", "s" }),
}
local has_words_before = function()
    ---@diagnostic disable-next-line: deprecated
    local line, col = unpack(vim.api.nvim_win_get_cursor(0))
    return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match("%s") == nil
end
sglkc commented 5 months ago

I have the same issue particularly with intelephense using lspconfig. It seems that some completion get confirmed early like the function mysqli_init or max, don't know what might be the problem since my config works fine before I upgraded to 0.10.0 from 0.9.5

aymanalqadhi commented 5 months ago

I had this exact issue.

It turned out andymass/vim-matchup was trying to match selection text emitted for LSP snippets (snippets from other sources such as LuaSnip do not have this issue), which makes the completion menu close then open again with the previously selected text now as the current prefix.

I just removed the plugin, and everything worked correctly.

nhattVim commented 5 months ago

I had this exact issue.

It turned out was trying to match selection text emitted for LSP snippets (snippets from other sources such as LuaSnip do not have this issue), which makes the completion menu close then open again with the previously selected text now as the current prefix.andymass/vim-matchup

I just removed the plugin, and everything worked correctly.

I just removed this plugin and everything is ok, but do you know any plugin that can replace andymass/vim-matchup ?

loichyan commented 4 months ago

It turned out andymass/vim-matchup was trying to ...

Temporarily disabling vim-matchup works well for me:

-- plugins/vim-matchup.lua
return {
  "andymass/vim-matchup",
  config = function(_, opts)
    local ok, cmp = pcall(require, "cmp")
    if ok then
      cmp.event:on("menu_opened", function() vim.b.matchup_matchparen_enabled = false end)
      cmp.event:on("menu_closed", function() vim.b.matchup_matchparen_enabled = true end)
    end
    require("match-up").setup(opts)
  end,
}