cmp-cmdline icon indicating copy to clipboard operation
cmp-cmdline copied to clipboard

Remove constraint of working only in command mode

Open sassanh opened this issue 2 years ago • 2 comments

The hard constraint of being available only in command mode is limiting the use of this source where end user believes it's needed. For example it used to be not available in command line window (:q).

sassanh avatar Sep 01 '22 19:09 sassanh

FWIW, you can also override the exported function yourself locally in your own configuration. E.g. to setup support for the command window:

vim.api.nvim_create_autocmd({ "CmdwinEnter" }, {
  group = vim.api.nvim_create_augroup("__user_cmdwin_enter", { clear = true }),
  callback = function()
    vim.b.is_cmdwin = true
  end
})

require("cmp_cmdline").is_available = function()
  return vim.api.nvim_get_mode().mode == "c" or vim.b.is_cmdwin == true
end

or realistically whatever else you want there. I wouldn't go quite as far as making it always available, as in most contexts (e.g. a Python file) completion for commands is likely not useful, right?

levouh avatar Sep 11 '22 03:09 levouh

The problem is currently we have 2 approaches for attaching a source to a buffer, and it may be overkilling the customization and causing confusion. One approach is using vim's own tools like autocmd to call cmp.setup.buffer for each buffer based on its options/filetype/etc and the other approach is setting up all sources for every file type in cmp.setup and then write is_available for each source. These two in parallel cause confusion in my opinion. For example when I write:

    vim.api.nvim_create_augroup('CMP', { clear = true })
    vim.api.nvim_create_autocmd('CmdwinEnter', {
      group = 'CMP',
      pattern = '*',
      callback = function()
        cmp.setup.buffer({ sources = cmp.config.sources({ { name = "cmdline" } } ) })
      end
    })

I'm specifically activating cmdline source for a specific buffer type, there is really no need for is_available.

So I think cmp.setup.buffer and is_available are conflicting and causing two ways to do a specific customization. I can't imagine a scenario when one can't do what the other is able to do.

sassanh avatar Sep 12 '22 08:09 sassanh

+1 for deleting this constraint.

users can use separate configs using require('cmp').setup.filetype or using autocmds

snippet from my config:

...
        cmp.setup.cmdline(':', {
          mapping = cmp.mapping.preset.cmdline(mapping),
          sources = cmp.config.sources(cmdline_sources),
        })

        -- FIXME: https://github.com/hrsh7th/cmp-cmdline/pull/61
        require('cmp_cmdline').is_available = function()
          return true
        end

        -- this applies to vim files and command-window as well
        cmp.setup.filetype('vim', {
          mapping = cmp.mapping.preset.cmdline(mapping),
          sources = cmp.config.sources(cmdline_sources),
        })

liljaylj avatar Sep 16 '22 04:09 liljaylj

LGTM. I think most user doesn't specify cmdline source as normal mode but this limitation isn't necessary.

hrsh7th avatar Sep 16 '22 12:09 hrsh7th