hrsh7th / nvim-cmp

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

The "Safely select entries with <CR>" example from wiki does not work correctly in command mode #1753

Open dam9000 opened 8 months ago

dam9000 commented 8 months ago

FAQ

Announcement

Minimal reproducible full config

if has('vim_starting')
  set encoding=utf-8
endif
scriptencoding utf-8

if &compatible
  set nocompatible
endif

let s:plug_dir = expand('/tmp/plugged/vim-plug')
if !filereadable(s:plug_dir .. '/plug.vim')
  execute printf('!curl -fLo %s/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim', s:plug_dir)
end

execute 'set runtimepath+=' . s:plug_dir
call plug#begin(s:plug_dir)
Plug 'hrsh7th/nvim-cmp'
Plug 'hrsh7th/cmp-buffer'
Plug 'hrsh7th/cmp-nvim-lsp'
Plug 'hrsh7th/vim-vsnip'
Plug 'neovim/nvim-lspconfig'
call plug#end()
PlugInstall | quit

" Setup global configuration. More on configuration below.
lua << EOF
local cmp = require "cmp"
cmp.setup {
  snippet = {
    expand = function(args)
      vim.fn["vsnip#anonymous"](args.body)
    end,
  },

  -- mapping = {
  --   ['<CR>'] = cmp.mapping.confirm({ select = true })
  -- },
  mapping = {
     ["<CR>"] = cmp.mapping({
       i = function(fallback)
         if cmp.visible() and cmp.get_active_entry() then
           cmp.confirm({ behavior = cmp.ConfirmBehavior.Replace, select = false })
         else
           fallback()
         end
       end,
       s = cmp.mapping.confirm({ select = true }),
       c = cmp.mapping.confirm({ behavior = cmp.ConfirmBehavior.Replace, select = true }),
     }),
  },

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

lua << EOF
local capabilities = require('cmp_nvim_lsp').default_capabilities()

require'lspconfig'.cssls.setup {
  capabilities = capabilities,
}
EOF

Description

With the added config from wiki: Safely select entries with <CR> When in command mode and pressing tab for completion, selecting some other menu item than the first and then pressing <Enter><Enter> will execute the first item instead of the currently selected one.

Steps to reproduce

Type :co<TAB> you get a menu of:

 colder         
 colorscheme    
 comclear       
 command        
 compiler       
 confirm        
 const          
 continue       
 copen          
 copy       

with the first entry colder selected by default press <TAB> one more time so that colorscheme is selected press <Enter> :colorscheme is printed out in the command line, however when pressing <Enter> again this happens:

E380: At bottom of quickfix stack                                                                                                   
error list 1 of 0; 0 errors
Press ENTER or type command to continue

Which is the output from the first entry colder Instead of the expected output from colorscheme which would be default. Pressing :<Up> or :history to reveal the command history will show that :colder was actually entered.

Expected behavior

Selected menu entry would be executed instead of the first

Actual behavior

The first menu entry is always executed

Additional context

If I change this config line:

      c = cmp.mapping.confirm({ behavior = cmp.ConfirmBehavior.Replace, select = true }),

to:

      c = cmp.mapping.confirm({ behavior = cmp.ConfirmBehavior.Replace, select = false }),

Then it works as expected. Not sure if it's a bug in the code, or a wrong example on wiki.

andrenbrandao commented 2 months ago

I am having the same issue and can confirm your change works as expected.