Skip to content

Commit d9687d8

Browse files
committed
improve completion label of table fields
1 parent 24b8ab8 commit d9687d8

File tree

6 files changed

+126
-12
lines changed

6 files changed

+126
-12
lines changed

script/core/completion/completion.lua

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1572,14 +1572,13 @@ local function checkTableLiteralField(state, position, tbl, fields, results)
15721572
end
15731573
end
15741574
if left then
1575-
local hasResult = false
1575+
local fieldResults = {}
15761576
for _, field in ipairs(fields) do
15771577
local name = guide.getKeyName(field)
15781578
if name
15791579
and not mark[name]
15801580
and matchKey(left, tostring(name)) then
1581-
hasResult = true
1582-
results[#results+1] = {
1581+
local res = {
15831582
label = guide.getKeyName(field),
15841583
kind = define.CompletionItemKind.Property,
15851584
id = stack(field, function (newField) ---@async
@@ -1589,9 +1588,19 @@ local function checkTableLiteralField(state, position, tbl, fields, results)
15891588
}
15901589
end),
15911590
}
1591+
if field.optional then
1592+
res.insertText = res.label
1593+
res.label = res.label.. '?'
1594+
end
1595+
fieldResults[#fieldResults+1] = res
15921596
end
15931597
end
1594-
return hasResult
1598+
util.sortByScore(fieldResults, {
1599+
function (r) return r.insertText and 0 or 1 end,
1600+
util.sortCallbackOfIndex(fieldResults),
1601+
})
1602+
util.arrayMerge(results, fieldResults)
1603+
return #fieldResults > 0
15951604
end
15961605
end
15971606

script/utility.lua

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -528,18 +528,25 @@ function m.utf8Len(str, start, finish)
528528
return len
529529
end
530530

531-
function m.revertTable(t)
532-
local len = #t
531+
-- 把数组中的元素顺序*原地*反转
532+
---@param arr any[]
533+
---@return any[]
534+
function m.revertArray(arr)
535+
local len = #arr
533536
if len <= 1 then
534-
return t
537+
return arr
535538
end
536539
for x = 1, len // 2 do
537540
local y = len - x + 1
538-
t[x], t[y] = t[y], t[x]
541+
arr[x], arr[y] = arr[y], arr[x]
539542
end
540-
return t
543+
return arr
541544
end
542545

546+
-- 创建一个value-key表
547+
---@generic K, V
548+
---@param t table<K, V>
549+
---@return table<V, K>
543550
function m.revertMap(t)
544551
local nt = {}
545552
for k, v in pairs(t) do
@@ -634,6 +641,11 @@ function m.eachLine(text, keepNL)
634641
end
635642
end
636643

644+
---@alias SortByScoreCallback fun(o: any): integer
645+
646+
-- 按照分数排序,分数越高越靠前
647+
---@param tbl any[]
648+
---@param callbacks SortByScoreCallback | SortByScoreCallback[]
637649
function m.sortByScore(tbl, callbacks)
638650
if type(callbacks) ~= 'table' then
639651
callbacks = { callbacks }
@@ -661,6 +673,16 @@ function m.sortByScore(tbl, callbacks)
661673
end)
662674
end
663675

676+
---@param arr any[]
677+
---@return SortByScoreCallback
678+
function m.sortCallbackOfIndex(arr)
679+
---@type table<any, integer>
680+
local indexMap = m.revertMap(arr)
681+
return function (v)
682+
return - indexMap[v]
683+
end
684+
end
685+
664686
---裁剪字符串
665687
---@param str string
666688
---@param mode? '"left"'|'"right"'
@@ -855,6 +877,15 @@ function m.arrayHas(array, value)
855877
return false
856878
end
857879

880+
function m.arrayIndexOf(array, value)
881+
for i = 1, #array do
882+
if array[i] == value then
883+
return i
884+
end
885+
end
886+
return nil
887+
end
888+
858889
function m.arrayInsert(array, value)
859890
if not m.arrayHas(array, value) then
860891
array[#array+1] = value
@@ -887,4 +918,24 @@ function m.cacheReturn(func)
887918
end
888919
end
889920

921+
---@param a table
922+
---@param b table
923+
---@return table
924+
function m.tableMerge(a, b)
925+
for k, v in pairs(b) do
926+
a[k] = v
927+
end
928+
return a
929+
end
930+
931+
---@param a any[]
932+
---@param b any[]
933+
---@return any[]
934+
function m.arrayMerge(a, b)
935+
for i = 1, #b do
936+
a[#a+1] = b[i]
937+
end
938+
return a
939+
end
940+
890941
return m

script/vm/type.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,7 @@ function vm.viewTypeErrorMessage(uri, errs)
752752
lines[#lines+1] = '- ' .. line
753753
end
754754
end
755-
util.revertTable(lines)
755+
util.revertArray(lines)
756756
if #lines > 15 then
757757
lines[13] = ('...(+%d)'):format(#lines - 15)
758758
table.move(lines, #lines - 2, #lines, 14)

test/completion/common.lua

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4307,3 +4307,57 @@ a:<??>
43074307
kind = define.CompletionItemKind.Function,
43084308
},
43094309
}
4310+
4311+
TEST [[
4312+
---@class A
4313+
---@field x number
4314+
---@field y? number
4315+
---@field z number
4316+
4317+
---@type A
4318+
local t = {
4319+
<??>
4320+
}
4321+
]]
4322+
{
4323+
{
4324+
label = 'x',
4325+
kind = define.CompletionItemKind.Property,
4326+
},
4327+
{
4328+
label = 'z',
4329+
kind = define.CompletionItemKind.Property,
4330+
},
4331+
{
4332+
label = 'y?',
4333+
kind = define.CompletionItemKind.Property,
4334+
},
4335+
}
4336+
4337+
TEST [[
4338+
---@class A
4339+
---@field x number
4340+
---@field y? number
4341+
---@field z number
4342+
4343+
---@param t A
4344+
local function f(t) end
4345+
4346+
f {
4347+
<??>
4348+
}
4349+
]]
4350+
{
4351+
{
4352+
label = 'x',
4353+
kind = define.CompletionItemKind.Property,
4354+
},
4355+
{
4356+
label = 'z',
4357+
kind = define.CompletionItemKind.Property,
4358+
},
4359+
{
4360+
label = 'y?',
4361+
kind = define.CompletionItemKind.Property,
4362+
},
4363+
}

test/full/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ end) do
3737
end
3838
end
3939

40-
util.revertTable(times)
40+
util.revertArray(times)
4141
for _, time in ipairs(times) do
4242
print(time)
4343
end

test/full/self.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ end) do
8686
end
8787
end
8888

89-
util.revertTable(printTexts)
89+
util.revertArray(printTexts)
9090

9191
for _, text in ipairs(printTexts) do
9292
print(text)

0 commit comments

Comments
 (0)