summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2023-07-19 18:03:20 +0800
committer最萌小汐 <sumneko@hotmail.com>2023-07-19 18:03:20 +0800
commit8b681399d4a5098fd1ec35e053f775df7cd67a5a (patch)
treebcea9fed8c281823c0250b324d114e670a214e19
parent682614e0004472d485b834e3608bfa9c62a6fee6 (diff)
downloadlua-language-server-8b681399d4a5098fd1ec35e053f775df7cd67a5a.zip
sort results of completion
-rw-r--r--script/core/completion/completion.lua37
-rw-r--r--test/completion/common.lua80
2 files changed, 115 insertions, 2 deletions
diff --git a/script/core/completion/completion.lua b/script/core/completion/completion.lua
index 0ec503de..30c1899e 100644
--- a/script/core/completion/completion.lua
+++ b/script/core/completion/completion.lua
@@ -504,7 +504,8 @@ local function checkFieldThen(state, name, src, word, startPos, position, parent
local kind = define.CompletionItemKind.Field
if (value.type == 'function' and not vm.isVarargFunctionWithOverloads(value))
or value.type == 'doc.type.function' then
- if oop then
+ local isMethod = value.parent.type == 'setmethod'
+ if isMethod then
kind = define.CompletionItemKind.Method
else
kind = define.CompletionItemKind.Function
@@ -512,6 +513,7 @@ local function checkFieldThen(state, name, src, word, startPos, position, parent
buildFunction(results, src, value, oop, {
label = name,
kind = kind,
+ isMethod = isMethod,
match = name:match '^[^(]+',
insertText = name:match '^[^(]+',
deprecated = vm.getDeprecated(src) and true or nil,
@@ -626,12 +628,43 @@ local function checkFieldOfRefs(refs, state, word, startPos, position, parent, o
end
::CONTINUE::
end
+
+ local fieldResults = {}
for name, src in util.sortPairs(fields) do
if src then
- checkFieldThen(state, name, src, word, startPos, position, parent, oop, results)
+ checkFieldThen(state, name, src, word, startPos, position, parent, oop, fieldResults)
await.delay()
end
end
+
+ local scoreMap = {}
+ for i, res in ipairs(fieldResults) do
+ scoreMap[res] = i
+ end
+ table.sort(fieldResults, function (a, b)
+ local score1 = scoreMap[a]
+ local score2 = scoreMap[b]
+ if oop then
+ if not a.isMethod then
+ score1 = score1 + 10000
+ end
+ if not b.isMethod then
+ score2 = score2 + 10000
+ end
+ else
+ if a.isMethod then
+ score1 = score1 + 10000
+ end
+ if b.isMethod then
+ score2 = score2 + 10000
+ end
+ end
+ return score1 < score2
+ end)
+
+ for _, res in ipairs(fieldResults) do
+ results[#results+1] = res
+ end
end
---@async
diff --git a/test/completion/common.lua b/test/completion/common.lua
index d38f2912..a6df8da7 100644
--- a/test/completion/common.lua
+++ b/test/completion/common.lua
@@ -4227,3 +4227,83 @@ bar.<??>
},
}
Cared['description'] = false
+
+TEST [[
+---@class A
+local M = {}
+
+function M:method1()
+end
+
+function M.static1(tt)
+end
+
+function M:method2()
+end
+
+function M.static2(tt)
+end
+
+---@type A
+local a
+
+a.<??>
+]]
+{
+ {
+ label ='static1(tt)',
+ kind = define.CompletionItemKind.Function,
+ },
+ {
+ label ='static2(tt)',
+ kind = define.CompletionItemKind.Function,
+ },
+ {
+ label ='method1(self)',
+ kind = define.CompletionItemKind.Method,
+ },
+ {
+ label ='method2(self)',
+ kind = define.CompletionItemKind.Method,
+ },
+}
+
+TEST [[
+---@class A
+local M = {}
+
+function M:method1()
+end
+
+function M.static1(tt)
+end
+
+function M:method2()
+end
+
+function M.static2(tt)
+end
+
+---@type A
+local a
+
+a:<??>
+]]
+{
+ {
+ label ='method1()',
+ kind = define.CompletionItemKind.Method,
+ },
+ {
+ label ='method2()',
+ kind = define.CompletionItemKind.Method,
+ },
+ {
+ label ='static1()',
+ kind = define.CompletionItemKind.Function,
+ },
+ {
+ label ='static2()',
+ kind = define.CompletionItemKind.Function,
+ },
+}