hrsh7th / cmp-cmdline

nvim-cmp source for vim's cmdline
MIT License
559 stars 43 forks source link

Only enable `/` completion #52

Open ewoij opened 2 years ago

ewoij commented 2 years ago

Hello,

I didn't manage to use the completion only for the search. Is it possible?

With the following config, the "default nvim completion" gets disabled after the first search.

Thanks,


asciicast

Minimal config (nvim 0.7.0):

call plug#begin(stdpath('data') . '/plugged')
Plug 'neovim/nvim-lspconfig'
Plug 'hrsh7th/cmp-nvim-lsp'
Plug 'hrsh7th/cmp-buffer'
Plug 'hrsh7th/cmp-path'
Plug 'hrsh7th/cmp-cmdline'
Plug 'hrsh7th/nvim-cmp'

" For vsnip users.
Plug 'hrsh7th/cmp-vsnip'
Plug 'hrsh7th/vim-vsnip'

" For luasnip users.
" Plug 'L3MON4D3/LuaSnip'
" Plug 'saadparwaiz1/cmp_luasnip'

" For ultisnips users.
" Plug 'SirVer/ultisnips'
" Plug 'quangnguyen30192/cmp-nvim-ultisnips'

" For snippy users.
" Plug 'dcampos/nvim-snippy'
" Plug 'dcampos/cmp-snippy'

call plug#end()

set completeopt=menu,menuone,noselect

lua <<EOF
  -- Setup nvim-cmp.
  local cmp = require'cmp'

  cmp.setup({
    snippet = {
      -- REQUIRED - you must specify a snippet engine
      expand = function(args)
        vim.fn["vsnip#anonymous"](args.body) -- For `vsnip` users.
        -- require('luasnip').lsp_expand(args.body) -- For `luasnip` users.
        -- require('snippy').expand_snippet(args.body) -- For `snippy` users.
        -- vim.fn["UltiSnips#Anon"](args.body) -- For `ultisnips` users.
      end,
    },
    window = {
      -- completion = cmp.config.window.bordered(),
      -- documentation = cmp.config.window.bordered(),
    },
    mapping = cmp.mapping.preset.insert({
      ['<C-b>'] = cmp.mapping.scroll_docs(-4),
      ['<C-f>'] = cmp.mapping.scroll_docs(4),
      ['<C-Space>'] = cmp.mapping.complete(),
      ['<C-e>'] = cmp.mapping.abort(),
      ['<CR>'] = cmp.mapping.confirm({ select = true }), -- Accept currently selected item. Set `select` to `false` to only confirm explicitly selected items.
    }),
    sources = cmp.config.sources({
      { name = 'nvim_lsp' },
      { name = 'vsnip' }, -- For vsnip users.
      -- { name = 'luasnip' }, -- For luasnip users.
      -- { name = 'ultisnips' }, -- For ultisnips users.
      -- { name = 'snippy' }, -- For snippy users.
    }, {
      { name = 'buffer' },
    })
  })

  -- Set configuration for specific filetype.
  cmp.setup.filetype('gitcommit', {
    sources = cmp.config.sources({
      { name = 'cmp_git' }, -- You can specify the `cmp_git` source if you were installed it.
    }, {
      { name = 'buffer' },
    })
  })

  -- Use buffer source for `/` (if you enabled `native_menu`, this won't work anymore).
  cmp.setup.cmdline('/', {
    mapping = cmp.mapping.preset.cmdline(),
    sources = {
      { name = 'buffer' }
    }
  })
EOF
cliffxuan commented 2 years ago

I had exactly the same problem. Took me a while to figure out why my wildmenu for : is broken.

The reasons I don't want to cmp-cmdline for : are:

I'm guessing others are facing the same issue.

Would be really good if the author or someone else shares a solution.

hrsh7th commented 1 year ago

The vim's key-mapping is global. So I have no solution for this problem...

jiaming-shi commented 1 year ago

The vim's key-mapping is global. So I have no solution for this problem...

I have the same issue, cmp-cmdline will break vim-grepper.

vim-grepper creates custom cmdline mapping when it is activated, and restore original maping after exit. So maybe cmp-cmdline could do with the same way? https://github.com/mhinz/vim-grepper/blob/2b93535752ffcb312f9fab73d90e80dc9f2e60fc/plugin/grepper.vim#L764..L827

osthomas commented 1 year ago

I don't have a solution to this problem, but a workaround: run the setup for cmdline, but configure it with no sources and to send wildchar on <Tab> (or some other key). This effectively restores native cmdline completion and prevents ^I being inserted when Tab-completing after searching once.

-- `:` cmdline setup.
-- Use cmp.cmdline only for '/' search completion because it does not play nice
-- with:
-- - $ENV_VARS
-- - wildcard expansion (%:h:p, ...)
-- - */** notation
-- Skipping the setup entirely prevents cmdline completion from working after
-- searching once (insertion of '^I' when pressing Tab).
local function send_wildchar()
    local char = vim.fn.nr2char(vim.opt.wildchar:get())
    local key = vim.api.nvim_replace_termcodes(char, true, false, true)
    vim.api.nvim_feedkeys(key, "nt", true)
end
cmp.setup.cmdline(":", {
    mapping = {
        ["<Tab>"] = {c = send_wildchar}
    },
    sources = cmp.config.sources({})
})