Skip to content

Commit 8b68139

Browse files
committed
sort results of completion
1 parent 682614e commit 8b68139

File tree

2 files changed

+115
-2
lines changed

2 files changed

+115
-2
lines changed

script/core/completion/completion.lua

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -504,14 +504,16 @@ local function checkFieldThen(state, name, src, word, startPos, position, parent
504504
local kind = define.CompletionItemKind.Field
505505
if (value.type == 'function' and not vm.isVarargFunctionWithOverloads(value))
506506
or value.type == 'doc.type.function' then
507-
if oop then
507+
local isMethod = value.parent.type == 'setmethod'
508+
if isMethod then
508509
kind = define.CompletionItemKind.Method
509510
else
510511
kind = define.CompletionItemKind.Function
511512
end
512513
buildFunction(results, src, value, oop, {
513514
label = name,
514515
kind = kind,
516+
isMethod = isMethod,
515517
match = name:match '^[^(]+',
516518
insertText = name:match '^[^(]+',
517519
deprecated = vm.getDeprecated(src) and true or nil,
@@ -626,12 +628,43 @@ local function checkFieldOfRefs(refs, state, word, startPos, position, parent, o
626628
end
627629
::CONTINUE::
628630
end
631+
632+
local fieldResults = {}
629633
for name, src in util.sortPairs(fields) do
630634
if src then
631-
checkFieldThen(state, name, src, word, startPos, position, parent, oop, results)
635+
checkFieldThen(state, name, src, word, startPos, position, parent, oop, fieldResults)
632636
await.delay()
633637
end
634638
end
639+
640+
local scoreMap = {}
641+
for i, res in ipairs(fieldResults) do
642+
scoreMap[res] = i
643+
end
644+
table.sort(fieldResults, function (a, b)
645+
local score1 = scoreMap[a]
646+
local score2 = scoreMap[b]
647+
if oop then
648+
if not a.isMethod then
649+
score1 = score1 + 10000
650+
end
651+
if not b.isMethod then
652+
score2 = score2 + 10000
653+
end
654+
else
655+
if a.isMethod then
656+
score1 = score1 + 10000
657+
end
658+
if b.isMethod then
659+
score2 = score2 + 10000
660+
end
661+
end
662+
return score1 < score2
663+
end)
664+
665+
for _, res in ipairs(fieldResults) do
666+
results[#results+1] = res
667+
end
635668
end
636669

637670
---@async

test/completion/common.lua

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4227,3 +4227,83 @@ bar.<??>
42274227
},
42284228
}
42294229
Cared['description'] = false
4230+
4231+
TEST [[
4232+
---@class A
4233+
local M = {}
4234+
4235+
function M:method1()
4236+
end
4237+
4238+
function M.static1(tt)
4239+
end
4240+
4241+
function M:method2()
4242+
end
4243+
4244+
function M.static2(tt)
4245+
end
4246+
4247+
---@type A
4248+
local a
4249+
4250+
a.<??>
4251+
]]
4252+
{
4253+
{
4254+
label ='static1(tt)',
4255+
kind = define.CompletionItemKind.Function,
4256+
},
4257+
{
4258+
label ='static2(tt)',
4259+
kind = define.CompletionItemKind.Function,
4260+
},
4261+
{
4262+
label ='method1(self)',
4263+
kind = define.CompletionItemKind.Method,
4264+
},
4265+
{
4266+
label ='method2(self)',
4267+
kind = define.CompletionItemKind.Method,
4268+
},
4269+
}
4270+
4271+
TEST [[
4272+
---@class A
4273+
local M = {}
4274+
4275+
function M:method1()
4276+
end
4277+
4278+
function M.static1(tt)
4279+
end
4280+
4281+
function M:method2()
4282+
end
4283+
4284+
function M.static2(tt)
4285+
end
4286+
4287+
---@type A
4288+
local a
4289+
4290+
a:<??>
4291+
]]
4292+
{
4293+
{
4294+
label ='method1()',
4295+
kind = define.CompletionItemKind.Method,
4296+
},
4297+
{
4298+
label ='method2()',
4299+
kind = define.CompletionItemKind.Method,
4300+
},
4301+
{
4302+
label ='static1()',
4303+
kind = define.CompletionItemKind.Function,
4304+
},
4305+
{
4306+
label ='static2()',
4307+
kind = define.CompletionItemKind.Function,
4308+
},
4309+
}

0 commit comments

Comments
 (0)