Skip to content

Commit e930179

Browse files
authored
Merge pull request #2484 from fesily/generic-pattern1
doc param support generic pattern
2 parents 8da156b + ad310d1 commit e930179

File tree

4 files changed

+100
-1
lines changed

4 files changed

+100
-1
lines changed

script/parser/luadoc.lua

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,53 @@ local function parseCode(parent)
705705
return code
706706
end
707707

708+
local function parseCodePattern(parent)
709+
local tp, pattern = peekToken()
710+
if not tp or tp ~= 'name' then
711+
return nil
712+
end
713+
local codeOffset
714+
local finishOffset
715+
local content
716+
for i = 2, 8 do
717+
local next, nextContent = peekToken(i)
718+
if not next or TokenFinishs[Ci+i-1] + 1 ~= TokenStarts[Ci+i] then
719+
if codeOffset then
720+
finishOffset = i
721+
break
722+
end
723+
---不连续的name,无效的
724+
return nil
725+
end
726+
if next == 'code' then
727+
if codeOffset and content ~= nextContent then
728+
-- 暂时不支持多generic
729+
return nil
730+
end
731+
codeOffset = i
732+
pattern = pattern .. "%s"
733+
content = nextContent
734+
elseif next ~= 'name' then
735+
return nil
736+
else
737+
pattern = pattern .. nextContent
738+
end
739+
end
740+
local start = getStart()
741+
for i = 2 , finishOffset do
742+
nextToken()
743+
end
744+
local code = {
745+
type = 'doc.type.code',
746+
start = start,
747+
finish = getFinish(),
748+
parent = parent,
749+
pattern = pattern,
750+
[1] = content,
751+
}
752+
return code
753+
end
754+
708755
local function parseInteger(parent)
709756
local tp, content = peekToken()
710757
if not tp or tp ~= 'integer' then
@@ -760,6 +807,7 @@ function parseTypeUnit(parent)
760807
or parseInteger(parent)
761808
or parseBoolean(parent)
762809
or parseParen(parent)
810+
or parseCodePattern(parent)
763811
if not result then
764812
result = parseName('doc.type.name', parent)
765813
or parseDots('doc.type.name', parent)

script/vm/sign.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ function mt:resolve(uri, args)
5353
for n in node:eachObject() do
5454
if n.type == 'string' then
5555
---@cast n parser.object
56-
local type = vm.declareGlobal('type', n[1], guide.getUri(n))
56+
local type = vm.declareGlobal('type', object.pattern and object.pattern:format(n[1]) or n[1], guide.getUri(n))
5757
resolved[key] = vm.createNode(type, resolved[key])
5858
end
5959
end

test/completion/common.lua

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4413,3 +4413,24 @@ new 'A' {
44134413
kind = define.CompletionItemKind.Property,
44144414
}
44154415
}
4416+
4417+
TEST [[
4418+
---@class namespace.A
4419+
---@overload fun(x: {id: string})
4420+
4421+
---@generic T
4422+
---@param t namespace.`T`
4423+
---@return T
4424+
local function new(t) end
4425+
4426+
new 'A' {
4427+
<??>
4428+
}
4429+
]]
4430+
{
4431+
{
4432+
label = 'id',
4433+
kind = define.CompletionItemKind.Property,
4434+
}
4435+
}
4436+

test/definition/luadoc.lua

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,21 @@ local v1 = Generic(Foo)
304304
print(v1.<?bar1?>)
305305
]]
306306

307+
308+
TEST [[
309+
---@class n.Foo
310+
local Foo = {}
311+
function Foo:bar1() end
312+
313+
---@generic T
314+
---@param arg1 n.`T`
315+
---@return T
316+
function Generic(arg1) print(arg1) end
317+
318+
local v1 = Generic(Foo)
319+
print(v1.<?bar1?>)
320+
]]
321+
307322
TEST [[
308323
---@class Foo
309324
local Foo = {}
@@ -318,6 +333,21 @@ local v1 = Generic("Foo")
318333
print(v1.<?bar1?>)
319334
]]
320335

336+
337+
TEST [[
338+
---@class n.Foo
339+
local Foo = {}
340+
function Foo:<!bar1!>() end
341+
342+
---@generic T
343+
---@param arg1 n.`T`
344+
---@return T
345+
function Generic(arg1) print(arg1) end
346+
347+
local v1 = Generic("Foo")
348+
print(v1.<?bar1?>)
349+
]]
350+
321351
TEST [[
322352
---@class A
323353
local t

0 commit comments

Comments
 (0)