Implement source group.

Fixes #391
This commit is contained in:
hrsh7th
2021-10-22 18:23:49 +09:00
parent b546f50f09
commit e4a2cec153
5 changed files with 90 additions and 57 deletions

View File

@@ -46,18 +46,22 @@ Plug 'hrsh7th/cmp-nvim-lsp'
Plug 'hrsh7th/cmp-buffer' Plug 'hrsh7th/cmp-buffer'
Plug 'hrsh7th/nvim-cmp' Plug 'hrsh7th/nvim-cmp'
" For vsnip user. " For vsnip users.
Plug 'hrsh7th/cmp-vsnip' Plug 'hrsh7th/cmp-vsnip'
Plug 'hrsh7th/vim-vsnip' Plug 'hrsh7th/vim-vsnip'
" For luasnip user. " For luasnip users.
" Plug 'L3MON4D3/LuaSnip' " Plug 'L3MON4D3/LuaSnip'
" Plug 'saadparwaiz1/cmp_luasnip' " Plug 'saadparwaiz1/cmp_luasnip'
" For ultisnips user. " For ultisnips users.
" Plug 'SirVer/ultisnips' " Plug 'SirVer/ultisnips'
" Plug 'quangnguyen30192/cmp-nvim-ultisnips' " Plug 'quangnguyen30192/cmp-nvim-ultisnips'
" For snippy users.
" Plug 'dcampos/nvim-snippy'
" Plug 'dcampos/cmp-snippy'
call plug#end() call plug#end()
set completeopt=menu,menuone,noselect set completeopt=menu,menuone,noselect
@@ -69,14 +73,10 @@ lua <<EOF
cmp.setup({ cmp.setup({
snippet = { snippet = {
expand = function(args) expand = function(args)
-- For `vsnip` user. vim.fn["vsnip#anonymous"](args.body) -- For `vsnip` users.
vim.fn["vsnip#anonymous"](args.body) -- require('luasnip').lsp_expand(args.body) -- For `luasnip` users.
-- vim.fn["UltiSnips#Anon"](args.body) -- For `ultisnips` users.
-- For `luasnip` user. -- require'snippy'.expand_snippet(args.body) -- For `snippy` users.
-- require('luasnip').lsp_expand(args.body)
-- For `ultisnips` user.
-- vim.fn["UltiSnips#Anon"](args.body)
end, end,
}, },
mapping = { mapping = {
@@ -86,25 +86,22 @@ lua <<EOF
['<C-e>'] = cmp.mapping.close(), ['<C-e>'] = cmp.mapping.close(),
['<CR>'] = cmp.mapping.confirm({ select = true }), ['<CR>'] = cmp.mapping.confirm({ select = true }),
}, },
sources = { sources = cmp.config.sources({
{ name = 'nvim_lsp' }, { name = 'nvim_lsp' },
{ name = 'vsnip' }, -- For vsnip users.
-- For vsnip user. -- { name = 'luasnip' }, -- For luasnip users.
{ name = 'vsnip' }, -- { name = 'ultisnips' }, -- For ultisnips users.
-- { name = 'snippy' }, -- For snippy users.
-- For luasnip user. }, {
-- { name = 'luasnip' },
-- For ultisnips user.
-- { name = 'ultisnips' },
{ name = 'buffer' }, { name = 'buffer' },
})
} }
}) })
-- Setup lspconfig. -- Setup lspconfig.
local capabilities = require('cmp_nvim_lsp').update_capabilities(vim.lsp.protocol.make_client_capabilities())
require('lspconfig')[%YOUR_LSP_SERVER%].setup { require('lspconfig')[%YOUR_LSP_SERVER%].setup {
capabilities = require('cmp_nvim_lsp').update_capabilities(vim.lsp.protocol.make_client_capabilities()) capabilities = capabilities
} }
EOF EOF
``` ```
@@ -241,6 +238,14 @@ The source specific keyword_length for override.
The source specific maximum item count. The source specific maximum item count.
#### sources[number].group (type: number)
The source group index.
This option must be sequential order.
You can call built-in utility like `cmp.config.sources({ { name = 'a' } }, { { name = 'b' } })`.
#### preselect (type: cmp.PreselectMode) #### preselect (type: cmp.PreselectMode)
Specify preselect mode. The following modes are available. Specify preselect mode. The following modes are available.

View File

@@ -0,0 +1,10 @@
return function(...)
local sources = {}
for i, group in ipairs({ unpack(...) }) do
for _, source in ipairs(group) do
source.group = i
table.insert(sources, source);
end
end
return sources
end

View File

@@ -18,6 +18,7 @@ cmp.vim = require('cmp.types.vim')
---Export default config presets. ---Export default config presets.
cmp.config = {} cmp.config = {}
cmp.config.compare = require('cmp.config.compare') cmp.config.compare = require('cmp.config.compare')
cmp.config.sources = require('cmp.config.sources')
---Export mapping ---Export mapping
cmp.mapping = require('cmp.config.mapping') cmp.mapping = require('cmp.config.mapping')

View File

@@ -120,5 +120,6 @@ cmp.ItemField.Menu = 'menu'
---@field public keyword_pattern string ---@field public keyword_pattern string
---@field public keyword_length number ---@field public keyword_length number
---@field public max_item_count number ---@field public max_item_count number
---@field public group number
return cmp return cmp

View File

@@ -45,51 +45,67 @@ end
---@param ctx cmp.Context ---@param ctx cmp.Context
---@param sources cmp.Source[] ---@param sources cmp.Source[]
view.open = function(self, ctx, sources) view.open = function(self, ctx, sources)
local group_index = -1
local entries = {} local entries = {}
while true do
group_index = group_index + 1
-- check the source triggered by character local group = vim.tbl_filter(function(s)
local has_triggered_by_symbol_source = false return (s:get_config().group or 0) == group_index
for _, s in ipairs(sources) do end, sources)
if #s:get_entries(ctx) > 0 then
if s.is_triggered_by_symbol then if #group == 0 then
has_triggered_by_symbol_source = true break
break
end
end end
end
-- create filtered entries. -- check the source triggered by character
local offset = ctx.cursor.col local has_triggered_by_symbol_source = false
for i, s in ipairs(sources) do for _, s in ipairs(group) do
if s.offset <= offset then if #s:get_entries(ctx) > 0 then
if not has_triggered_by_symbol_source or s.is_triggered_by_symbol then if s.is_triggered_by_symbol then
-- source order priority bonus. has_triggered_by_symbol_source = true
local priority = s:get_config().priority or ((#sources - (i - 1)) * config.get().sorting.priority_weight) break
for _, e in ipairs(s:get_entries(ctx)) do
e.score = e.score + priority
table.insert(entries, e)
offset = math.min(offset, e:get_offset())
end end
end end
end end
end
-- sort. -- create filtered entries.
local comparetors = config.get().sorting.comparators local offset = ctx.cursor.col
table.sort(entries, function(e1, e2) for i, s in ipairs(group) do
for _, fn in ipairs(comparetors) do if s.offset <= offset then
local diff = fn(e1, e2) if not has_triggered_by_symbol_source or s.is_triggered_by_symbol then
if diff ~= nil then -- source order priority bonus.
return diff local priority = s:get_config().priority or ((#group - (i - 1)) * config.get().sorting.priority_weight)
for _, e in ipairs(s:get_entries(ctx)) do
e.score = e.score + priority
table.insert(entries, e)
offset = math.min(offset, e:get_offset())
end
end
end end
end end
end)
-- open -- sort.
if #entries > 0 then local comparetors = config.get().sorting.comparators
self:_get_entries_view():open(offset, entries) table.sort(entries, function(e1, e2)
else for _, fn in ipairs(comparetors) do
local diff = fn(e1, e2)
if diff ~= nil then
return diff
end
end
end)
-- open
if #entries > 0 then
self:_get_entries_view():open(offset, entries)
break
end
end
-- close.
if #entries == 0 then
self:close() self:close()
end end
end end