Skip to content

Commit afc5f4e

Browse files
committed
add completion and semantic for @cast
1 parent 125d943 commit afc5f4e

File tree

7 files changed

+86
-1
lines changed

7 files changed

+86
-1
lines changed

script/core/completion/completion.lua

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1514,6 +1514,7 @@ local function tryluaDocCate(word, results)
15141514
'module',
15151515
'async',
15161516
'nodiscard',
1517+
'cast',
15171518
} do
15181519
if matchKey(word, docType) then
15191520
results[#results+1] = {
@@ -1662,8 +1663,27 @@ local function tryluaDocBySource(state, position, source, results)
16621663
}
16631664
end
16641665
end
1666+
return true
16651667
elseif source.type == 'doc.module' then
16661668
collectRequireNames('require', state.uri, source.module or '', source, source.smark, position, results)
1669+
return true
1670+
elseif source.type == 'doc.cast.name' then
1671+
local locals = guide.getVisibleLocals(state.ast, position)
1672+
for name, loc in util.sortPairs(locals) do
1673+
if matchKey(source[1], name) then
1674+
results[#results+1] = {
1675+
label = name,
1676+
kind = define.CompletionItemKind.Variable,
1677+
id = stack(function () ---@async
1678+
return {
1679+
detail = buildDetail(loc),
1680+
description = buildDesc(loc),
1681+
}
1682+
end),
1683+
}
1684+
end
1685+
end
1686+
return true
16671687
end
16681688
return false
16691689
end
@@ -1758,6 +1778,22 @@ local function tryluaDocByErr(state, position, err, docState, results)
17581778
end
17591779
elseif err.type == 'LUADOC_MISS_MODULE_NAME' then
17601780
collectRequireNames('require', state.uri, '', docState, nil, position, results)
1781+
elseif err.type == 'LUADOC_MISS_LOCAL_NAME' then
1782+
local locals = guide.getVisibleLocals(state.ast, position)
1783+
for name, loc in util.sortPairs(locals) do
1784+
if name ~= '_ENV' then
1785+
results[#results+1] = {
1786+
label = name,
1787+
kind = define.CompletionItemKind.Variable,
1788+
id = stack(function () ---@async
1789+
return {
1790+
detail = buildDetail(loc),
1791+
description = buildDesc(loc),
1792+
}
1793+
end),
1794+
}
1795+
end
1796+
end
17611797
end
17621798
end
17631799

script/core/definition.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ local accept = {
5353
['doc.alias.name'] = true,
5454
['doc.see.name'] = true,
5555
['doc.see.field'] = true,
56+
['doc.cast.name'] = true,
5657
}
5758

5859
local function checkRequire(source, offset)

script/core/semantic-tokens.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,14 @@ local Care = util.switch()
652652
type = define.TokenTypes.keyword,
653653
}
654654
end)
655+
: case 'doc.cast.name'
656+
: call(function (source, options, results)
657+
results[#results+1] = {
658+
start = source.start,
659+
finish = source.finish,
660+
type = define.TokenTypes.variable,
661+
}
662+
end)
655663

656664
local function buildTokens(uri, results)
657665
local tokens = {}

script/parser/luadoc.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1235,7 +1235,6 @@ local docSwitch = util.switch()
12351235
start = getFinish(),
12361236
finish = getFinish(),
12371237
}
1238-
result.casts[#result.casts+1] = block
12391238
if checkToken('symbol', '+', 1) then
12401239
block.mode = '+'
12411240
nextToken()
@@ -1261,6 +1260,10 @@ local docSwitch = util.switch()
12611260
end
12621261
end
12631262

1263+
if block.optional or block.extends then
1264+
result.casts[#result.casts+1] = block
1265+
end
1266+
12641267
if checkToken('symbol', ',', 1) then
12651268
nextToken()
12661269
else

script/vm/def.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ simpleSwitch = util.switch()
7979
pushResult(source.node)
8080
end
8181
end)
82+
: case 'doc.cast.name'
83+
: call(function (source, pushResult)
84+
local loc = guide.getLocal(source, source[1], source.start)
85+
if loc then
86+
pushResult(loc)
87+
end
88+
end)
8289

8390
local searchFieldSwitch = util.switch()
8491
: case 'table'

test/completion/common.lua

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3324,3 +3324,27 @@ x.y.<??>
33243324
kind = define.CompletionItemKind.Field,
33253325
}
33263326
}
3327+
3328+
TEST [[
3329+
local xyz
3330+
3331+
---@cast <??>
3332+
]]
3333+
{
3334+
{
3335+
label = 'xyz',
3336+
kind = define.CompletionItemKind.Variable,
3337+
},
3338+
}
3339+
3340+
TEST [[
3341+
local xyz
3342+
3343+
---@cast x<??>
3344+
]]
3345+
{
3346+
{
3347+
label = 'xyz',
3348+
kind = define.CompletionItemKind.Variable,
3349+
}
3350+
}

test/definition/luadoc.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -893,3 +893,9 @@ TEST [[
893893
894894
---@type XXX<<?YYY?>>
895895
]]
896+
897+
TEST [[
898+
local <!x!>
899+
900+
---@cast <?x?> integer
901+
]]

0 commit comments

Comments
 (0)