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

Commit d62fff8

Browse files
authored
Merge pull request #374 from MrcJkb/sorting-performance
Improvement to sorting performance.
2 parents c8db953 + 97c83a7 commit d62fff8

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

lua/completion/util.lua

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,29 @@ end
1616
-- completion items --
1717
------------------------
1818

19-
function M.sort_completion_items(items)
20-
table.sort(items, function(a, b)
21-
if a.priority ~= b.priority and a.priority ~= nil and b.priority ~= nil then
22-
return a.priority > b.priority
23-
elseif a.score ~= b.score and a.score ~= nil and b.score ~= nil then
24-
return a.score < b.score
25-
elseif opt.get_option("sorting") == 'alphabet' then
19+
local function compare_strings(a, b)
20+
if opt.get_option("sorting") == 'alphabet' then
2621
return a.word < b.word
2722
elseif opt.get_option("sorting") == 'length_desc' then
2823
return string.len(a.word) > string.len(b.word)
2924
else
3025
return string.len(a.word) < string.len(b.word)
3126
end
27+
end
28+
29+
local function compare_scores_then_strings(a, b)
30+
if a.score == b.score or a.score == nil or b.score == nil then
31+
return compare_strings(a, b);
32+
end
33+
return a.score < b.score
34+
end
35+
36+
function M.sort_completion_items(items)
37+
table.sort(items, function(a, b)
38+
if a.priority == b.priority or a.priority == nil or b.priority == nil then
39+
return compare_scores_then_strings(a, b)
40+
end
41+
return a.priority > b.priority
3242
end)
3343
end
3444

0 commit comments

Comments
 (0)