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

Commit 64c2a8c

Browse files
committed
Allow sorting items by source
1 parent fc9b2fd commit 64c2a8c

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

lua/completion/complete.lua

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,19 @@ local function checkCallback(callback_array)
2121
return true
2222
end
2323

24+
local function assignSourcePriority(items, source)
25+
local source_priority = opt.get_option('source_priority')[source] or 1
26+
for _, item in ipairs(items) do
27+
item.source_priority = source_priority
28+
end
29+
end
30+
2431
local function getCompletionItems(items_array, prefix)
2532
local complete_items = {}
26-
for _,func in ipairs(items_array) do
27-
vim.list_extend(complete_items, func(prefix))
33+
for source, func in pairs(items_array) do
34+
local items = func(prefix)
35+
assignSourcePriority(items, source)
36+
vim.list_extend(complete_items, items)
2837
end
2938
return complete_items
3039
end
@@ -54,7 +63,7 @@ M.performComplete = function(complete_source, complete_items_map, params)
5463
cache_complete_items = {}
5564
table.insert(callback_array, complete_items.callback)
5665
complete_items.trigger(manager, params)
57-
table.insert(items_array, complete_items.item)
66+
items_array[item] = complete_items.item
5867
end
5968
else
6069
if complete_items ~= nil then
@@ -66,7 +75,7 @@ M.performComplete = function(complete_source, complete_items_map, params)
6675
-- will remove it when refactoring aysnc sources
6776
complete_items.trigger(manager, params)
6877
end
69-
table.insert(items_array, complete_items.item)
78+
items_array[item] = complete_items.item
7079
end
7180
end
7281
end

lua/completion/util.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ end
1818

1919
function M.sort_completion_items(items)
2020
table.sort(items, function(a, b)
21-
if a.priority ~= b.priority and a.priority ~= nil and b.priority ~= nil then
21+
if a.source_priority ~= b.source_priority and a.source_priority ~= nil and b.source_priority ~= nil then
22+
return a.source_priority > b.source_priority
23+
elseif a.priority ~= b.priority and a.priority ~= nil and b.priority ~= nil then
2224
return a.priority > b.priority
2325
elseif a.score ~= b.score and a.score ~= nil and b.score ~= nil then
2426
return a.score < b.score
@@ -54,6 +56,7 @@ function M.addCompletionItems(item_table, item)
5456
menu = item.menu or '',
5557
info = item.info or '',
5658
priority = item.priority or 1,
59+
source_priority = item.source_priority or 1,
5760
icase = 1,
5861
dup = item.dup or 1,
5962
empty = 1,

plugin/completion.vim

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ if ! exists('g:completion_items_priority')
117117
let g:completion_items_priority = {}
118118
endif
119119

120+
if ! exists('g:completion_source_priority')
121+
let g:completion_source_priority = {}
122+
endif
123+
120124
if ! exists('g:completion_abbr_length')
121125
let g:completion_abbr_length = 0
122126
endif

0 commit comments

Comments
 (0)