Skip to content

teramako/cmp-cmdline-prompt.nvim

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cmp-cmdline-prompt.nvim

nvim-cmp source for Neovim's command line input() prompt.

Sample settings

local cmp = require('cmp')

-- for cmdline `input()` prompt
-- see: `:help getcmdtype()`
cmp.cmdline.setup('@', {
    mapping = cmp.mapping.preset.cmdline(),
    sources = cmp.config.sources({
        { name = 'cmdline-prompt' },
    },
    sorting = {
        comparators = { cmp.config.compare.order }
    },
    window = {
        completion = {
            -- "prompt: " Adjust to the number of the prompt charaters 
            -- Can't get prompt length as far as I know :(
            col_offset = 8,
        },
    },
})

Exclude specific completions

cmp.cmdline.setup('@', {
    sources = cmp.config.sources({
        {
            name = 'cmdline-prompt',
            ---@type prompt.Option
            option = {
                ---@type string[]
                excludes = { 'file', 'dir' }, -- complete with 'hrsh7th/cmp-path' instead of 'cmdline-prompt'
            }
        },
        { namae = 'path' },
    },
})

For more detailed control, function also can be defined:

cmp.cmdline.setup('@', {
    sources = cmp.config.sources({
        {
            name = 'cmdline-prompt',
            ---@type prompt.Option
            option = {
                ---@type fun(context: cmd.Context, completion_type: string, custom_function: string)
                excludes = function(context, completion_type, custom_function)
                    if completion_type == 'file' or completion_type == 'dir' then
                        return true
                    end
                    return false
                end
            }
        },
        { namae = 'path' }
    },
})

Arguments:

  1. context (cmd.Context): See: context.lua in nvim-cmp.
    you can get bufnr, filetype ...etc.
  2. completion_type (string): See: :help getcompletion()
  3. custom_function (string): function name will be supplied when the completion type is custom or customlist.

Appearance

By default, all completion items are set as Text kind. You can change by defining option.kinds, and also define a different highlight group than the kind.

Additionaly, can be shown using lspkind.

local lspkind = require('lspkind')

cmp.setup.cmdline('@', {
    mapping = cmp.mapping.preset.cmdline(),
    sources = cmp.config.sources({
        {
            name = 'cmdline-prompt',
            ---@type prompt.Option
            option = {
                kinds = {
                    file = cmp.lsp.CompletionItemKind.File,
                    dir  = {
                        kind = cmp.lsp.CompletionItemKind.Folder,
                        hl_group = 'CmpItemKindEnum'
                    },
                }
            }
        },
    }),
    formatting = {
        fields = { 'kind', 'abbr', 'menu' },
        format = function(entry, vim_item)
            local item = entry:get_completion_item()
            if entry.source.name == 'cmdline-prompt' then
                vim_item.kind = cmp.lsp.CompletionItemKind[item.kind]
                local kind = lspkind.cmp_format({ mode = 'symbol_text' })(entry, vim_item)
                local strings = vim.split(kind.kind, '%s', { trimempty = true })
                kind.kind = ' ' .. (strings[1] or '')
                kind.menu = ' (' .. (item.data.completion_type or '') .. ')'
                kind.menu_hl_group = kind.kind_hl_group
                return kind
            else
                return vim_item
            end
        end
    },
})

About

nvim-cmp source for vim's command line `input()` prompt

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Languages