From 64c2a8cb3b6aaea65f53e0833dcf7546a2940f9f Mon Sep 17 00:00:00 2001 From: Pawel Bogut Date: Sun, 14 Feb 2021 17:19:29 +0100 Subject: [PATCH] Allow sorting items by source --- lua/completion/complete.lua | 17 +++++++++++++---- lua/completion/util.lua | 5 ++++- plugin/completion.vim | 4 ++++ 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/lua/completion/complete.lua b/lua/completion/complete.lua index 30fb53e..9675521 100644 --- a/lua/completion/complete.lua +++ b/lua/completion/complete.lua @@ -21,10 +21,19 @@ local function checkCallback(callback_array) return true end +local function assignSourcePriority(items, source) + local source_priority = opt.get_option('source_priority')[source] or 1 + for _, item in ipairs(items) do + item.source_priority = source_priority + end +end + local function getCompletionItems(items_array, prefix) local complete_items = {} - for _,func in ipairs(items_array) do - vim.list_extend(complete_items, func(prefix)) + for source, func in pairs(items_array) do + local items = func(prefix) + assignSourcePriority(items, source) + vim.list_extend(complete_items, items) end return complete_items end @@ -54,7 +63,7 @@ M.performComplete = function(complete_source, complete_items_map, params) cache_complete_items = {} table.insert(callback_array, complete_items.callback) complete_items.trigger(manager, params) - table.insert(items_array, complete_items.item) + items_array[item] = complete_items.item end else if complete_items ~= nil then @@ -66,7 +75,7 @@ M.performComplete = function(complete_source, complete_items_map, params) -- will remove it when refactoring aysnc sources complete_items.trigger(manager, params) end - table.insert(items_array, complete_items.item) + items_array[item] = complete_items.item end end end diff --git a/lua/completion/util.lua b/lua/completion/util.lua index 8f6a4f6..197b39b 100644 --- a/lua/completion/util.lua +++ b/lua/completion/util.lua @@ -18,7 +18,9 @@ end function M.sort_completion_items(items) table.sort(items, function(a, b) - if a.priority ~= b.priority and a.priority ~= nil and b.priority ~= nil then + if a.source_priority ~= b.source_priority and a.source_priority ~= nil and b.source_priority ~= nil then + return a.source_priority > b.source_priority + elseif a.priority ~= b.priority and a.priority ~= nil and b.priority ~= nil then return a.priority > b.priority elseif a.score ~= b.score and a.score ~= nil and b.score ~= nil then return a.score < b.score @@ -54,6 +56,7 @@ function M.addCompletionItems(item_table, item) menu = item.menu or '', info = item.info or '', priority = item.priority or 1, + source_priority = item.source_priority or 1, icase = 1, dup = item.dup or 1, empty = 1, diff --git a/plugin/completion.vim b/plugin/completion.vim index f30fad5..49244b8 100644 --- a/plugin/completion.vim +++ b/plugin/completion.vim @@ -117,6 +117,10 @@ if ! exists('g:completion_items_priority') let g:completion_items_priority = {} endif +if ! exists('g:completion_source_priority') + let g:completion_source_priority = {} +endif + if ! exists('g:completion_abbr_length') let g:completion_abbr_length = 0 endif