hrsh7th / cmp-cmdline

nvim-cmp source for vim's cmdline
MIT License
550 stars 42 forks source link

Remove constraint of working only in command mode #61

Closed sassanh closed 2 years ago

sassanh commented 2 years ago

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).

levouh commented 2 years ago

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?

sassanh commented 2 years ago

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.

liljaylj commented 2 years ago

+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),
        })
hrsh7th commented 2 years ago

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