Skip to content
This repository has been archived by the owner on Oct 13, 2021. It is now read-only.

Allow sorting items by source #345

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions lua/completion/complete.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
5 changes: 4 additions & 1 deletion lua/completion/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions plugin/completion.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down