summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2023-07-20 16:45:44 +0800
committer最萌小汐 <sumneko@hotmail.com>2023-07-20 16:45:49 +0800
commitd9687d8f1b6a55bdd97a65ba81d9ca2b6cb2357f (patch)
tree3be6554d16adef74e8c1d42d08c487d214d1928a
parent24b8ab8a1da3b8ae6f6c561ec58e9bd5fcff2c2b (diff)
downloadlua-language-server-d9687d8f1b6a55bdd97a65ba81d9ca2b6cb2357f.zip
improve completion label of table fields
-rw-r--r--script/core/completion/completion.lua17
-rw-r--r--script/utility.lua61
-rw-r--r--script/vm/type.lua2
-rw-r--r--test/completion/common.lua54
-rw-r--r--test/full/init.lua2
-rw-r--r--test/full/self.lua2
6 files changed, 126 insertions, 12 deletions
diff --git a/script/core/completion/completion.lua b/script/core/completion/completion.lua
index 30c1899e..518c716e 100644
--- a/script/core/completion/completion.lua
+++ b/script/core/completion/completion.lua
@@ -1572,14 +1572,13 @@ local function checkTableLiteralField(state, position, tbl, fields, results)
end
end
if left then
- local hasResult = false
+ local fieldResults = {}
for _, field in ipairs(fields) do
local name = guide.getKeyName(field)
if name
and not mark[name]
and matchKey(left, tostring(name)) then
- hasResult = true
- results[#results+1] = {
+ local res = {
label = guide.getKeyName(field),
kind = define.CompletionItemKind.Property,
id = stack(field, function (newField) ---@async
@@ -1589,9 +1588,19 @@ local function checkTableLiteralField(state, position, tbl, fields, results)
}
end),
}
+ if field.optional then
+ res.insertText = res.label
+ res.label = res.label.. '?'
+ end
+ fieldResults[#fieldResults+1] = res
end
end
- return hasResult
+ util.sortByScore(fieldResults, {
+ function (r) return r.insertText and 0 or 1 end,
+ util.sortCallbackOfIndex(fieldResults),
+ })
+ util.arrayMerge(results, fieldResults)
+ return #fieldResults > 0
end
end
diff --git a/script/utility.lua b/script/utility.lua
index 0a7ada88..b383a171 100644
--- a/script/utility.lua
+++ b/script/utility.lua
@@ -528,18 +528,25 @@ function m.utf8Len(str, start, finish)
return len
end
-function m.revertTable(t)
- local len = #t
+-- 把数组中的元素顺序*原地*反转
+---@param arr any[]
+---@return any[]
+function m.revertArray(arr)
+ local len = #arr
if len <= 1 then
- return t
+ return arr
end
for x = 1, len // 2 do
local y = len - x + 1
- t[x], t[y] = t[y], t[x]
+ arr[x], arr[y] = arr[y], arr[x]
end
- return t
+ return arr
end
+-- 创建一个value-key表
+---@generic K, V
+---@param t table<K, V>
+---@return table<V, K>
function m.revertMap(t)
local nt = {}
for k, v in pairs(t) do
@@ -634,6 +641,11 @@ function m.eachLine(text, keepNL)
end
end
+---@alias SortByScoreCallback fun(o: any): integer
+
+-- 按照分数排序,分数越高越靠前
+---@param tbl any[]
+---@param callbacks SortByScoreCallback | SortByScoreCallback[]
function m.sortByScore(tbl, callbacks)
if type(callbacks) ~= 'table' then
callbacks = { callbacks }
@@ -661,6 +673,16 @@ function m.sortByScore(tbl, callbacks)
end)
end
+---@param arr any[]
+---@return SortByScoreCallback
+function m.sortCallbackOfIndex(arr)
+ ---@type table<any, integer>
+ local indexMap = m.revertMap(arr)
+ return function (v)
+ return - indexMap[v]
+ end
+end
+
---裁剪字符串
---@param str string
---@param mode? '"left"'|'"right"'
@@ -855,6 +877,15 @@ function m.arrayHas(array, value)
return false
end
+function m.arrayIndexOf(array, value)
+ for i = 1, #array do
+ if array[i] == value then
+ return i
+ end
+ end
+ return nil
+end
+
function m.arrayInsert(array, value)
if not m.arrayHas(array, value) then
array[#array+1] = value
@@ -887,4 +918,24 @@ function m.cacheReturn(func)
end
end
+---@param a table
+---@param b table
+---@return table
+function m.tableMerge(a, b)
+ for k, v in pairs(b) do
+ a[k] = v
+ end
+ return a
+end
+
+---@param a any[]
+---@param b any[]
+---@return any[]
+function m.arrayMerge(a, b)
+ for i = 1, #b do
+ a[#a+1] = b[i]
+ end
+ return a
+end
+
return m
diff --git a/script/vm/type.lua b/script/vm/type.lua
index 8382eb86..dd7b359f 100644
--- a/script/vm/type.lua
+++ b/script/vm/type.lua
@@ -752,7 +752,7 @@ function vm.viewTypeErrorMessage(uri, errs)
lines[#lines+1] = '- ' .. line
end
end
- util.revertTable(lines)
+ util.revertArray(lines)
if #lines > 15 then
lines[13] = ('...(+%d)'):format(#lines - 15)
table.move(lines, #lines - 2, #lines, 14)
diff --git a/test/completion/common.lua b/test/completion/common.lua
index a6df8da7..e33b2a1b 100644
--- a/test/completion/common.lua
+++ b/test/completion/common.lua
@@ -4307,3 +4307,57 @@ a:<??>
kind = define.CompletionItemKind.Function,
},
}
+
+TEST [[
+---@class A
+---@field x number
+---@field y? number
+---@field z number
+
+---@type A
+local t = {
+ <??>
+}
+]]
+{
+ {
+ label = 'x',
+ kind = define.CompletionItemKind.Property,
+ },
+ {
+ label = 'z',
+ kind = define.CompletionItemKind.Property,
+ },
+ {
+ label = 'y?',
+ kind = define.CompletionItemKind.Property,
+ },
+}
+
+TEST [[
+---@class A
+---@field x number
+---@field y? number
+---@field z number
+
+---@param t A
+local function f(t) end
+
+f {
+ <??>
+}
+]]
+{
+ {
+ label = 'x',
+ kind = define.CompletionItemKind.Property,
+ },
+ {
+ label = 'z',
+ kind = define.CompletionItemKind.Property,
+ },
+ {
+ label = 'y?',
+ kind = define.CompletionItemKind.Property,
+ },
+}
diff --git a/test/full/init.lua b/test/full/init.lua
index ac78f134..a6496a99 100644
--- a/test/full/init.lua
+++ b/test/full/init.lua
@@ -37,7 +37,7 @@ end) do
end
end
-util.revertTable(times)
+util.revertArray(times)
for _, time in ipairs(times) do
print(time)
end
diff --git a/test/full/self.lua b/test/full/self.lua
index 09d2d154..34b19b2b 100644
--- a/test/full/self.lua
+++ b/test/full/self.lua
@@ -86,7 +86,7 @@ end) do
end
end
-util.revertTable(printTexts)
+util.revertArray(printTexts)
for _, text in ipairs(printTexts) do
print(text)