hrsh7th / nvim-cmp

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

Snippets automatically insert themselves and close the completion menu #1269

Open b3nj5m1n opened 1 year ago

b3nj5m1n commented 1 year ago

FAQ

Announcement

Minimal reproducible full config

vim.cmd [[packadd packer.nvim]]

require('packer').startup({function(use)
    use 'wbthomason/packer.nvim'
    use 'hrsh7th/nvim-cmp'
    use 'neovim/nvim-lspconfig'
    use "hrsh7th/cmp-nvim-lsp"
    use "hrsh7th/cmp-buffer"
    use "gpanders/nvim-parinfer"

    end, config = {
        compile_path = (vim.fn.stdpath("config") .. "/lua/packer_compiled_test.lua")  
    }
})

require("packer_compiled_test")

local cmp = require "cmp"
cmp.setup {
  mapping = {
        ["<C-n>"] = cmp.mapping.select_next_item({"i", "c"}),
        ["<C-p>"] = cmp.mapping.select_prev_item({"i", "c"}),
        ['<CR>'] = cmp.mapping.confirm({ select = true })
  },

  sources = cmp.config.sources({
    { name = "nvim_lsp" },
    { name = "buffer" },
  }),
}

local capabilities = require('cmp_nvim_lsp').default_capabilities()

require'lspconfig'.clojure_lsp.setup {
  capabilities = capabilities,
}

Description

When cycling through the auto-complete menu using ctrl+n with nvim-parinfer enabled, at some point a snippet will insert itself automatically and close the completion menu.

Steps to reproduce

Using the minimal config, open a clojure file (or any other file that makes use of parinfer, like fennel or common lisp), wait for the lsp to load, then type something like def and start cycling through the suggsted completions using ctrl+n.

Expected behavior

You can go forwards & backwards in the completion menu as much as you want, at some point you select an item from the selections.

Actual behavior

After hitting ctrl+n twice, a snippet automatically inserts itself and closes the completion menu.

Peek 2022-10-29 01-44

Additional context

nvim-parinfer automatically tries to balance parens, so when it encounters an unclosed opening parenthesis, it will insert a closing parenthesis where it thinks it belongs. I believe because the snippet includes parenthesis, this somehow causes one of the plugins to break.

I'm pretty sure this was working before I updated nvim-cmp, I was probably a few months out of date, so I think this has probably been introduced somewhat recently in this plugin, although I'm not 100% sure. nvim-parinfer has not had any updates in this timeframe.

cathaysia commented 5 months ago

I have the same problem, eventhrough I don't mapping any complete for tab :

            ['<Tab>'] = cmp.mapping(function(fallback)
                if cmp.visible() then
                    cmp.select_next_item()
                elseif luasnip.locally_jumpable(1) then
                    luasnip.jump(1)
                else
                    fallback()
                end
            end, { 'i', 's' }),
cathaysia commented 5 months ago

This problem does not occur unless the files are switched using Telescope find_files

mawkler commented 4 months ago

I'm also having the same issue specifically with rust_analyzer's snippet for match, but I couldn't reproduce the issue in a mininal config.

It seems to be an issue with the snippet's first line being expanded when moving the cmp selection over it. As a workaround I disabled inserting the hovered text by setting this in cmp's .setup() (see :help cmp.select_next_item), which fixes the issue:

{
  mapping = {
    ['<C-j>'] = cmp_map(cmp.mapping.select_next_item({ behavior = cmp.SelectBehavior.Select })),
    ['<C-k>'] = cmp_map(cmp.mapping.select_prev_item({ behavior = cmp.SelectBehavior.Select })),
  }
}
yujinyuz commented 4 months ago

Been having the same issue as well.

Don't like to use cmp.SelectBehavior.Select because I have a habit of pressing escape after selecting the next item via <C-n> and I expect that the selected text was inserted.

It seems there is a related issue here https://github.com/hrsh7th/nvim-cmp/issues/1780

But @hrsh7th mentioned it has been fixed so I'm not really sure why this still happens

https://github.com/hrsh7th/nvim-cmp/assets/10972027/c2601816-033e-4616-abea-443a83b5296a

cathaysia commented 1 month ago

hi, any process about this?

andrevmatos commented 2 weeks ago

I was experiencing this even without any autopair plugin, if the completion item had some opening character (usually (, [, {). Setting behavior = cmp.SelectBehavior.Select did work around the issue, even after re-enabling the ultimate-autopairs plugin.