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

Commit cbae91a

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

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

lua/completion/complete.lua

+13-4
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

+3-1
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

plugin/completion.vim

+4
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)