Skip to content

Commit

Permalink
Merge pull request #2484 from fesily/generic-pattern1
Browse files Browse the repository at this point in the history
doc param support generic pattern
  • Loading branch information
sumneko authored Mar 18, 2024
2 parents 8da156b + ad310d1 commit e930179
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 1 deletion.
48 changes: 48 additions & 0 deletions script/parser/luadoc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,53 @@ local function parseCode(parent)
return code
end

local function parseCodePattern(parent)
local tp, pattern = peekToken()
if not tp or tp ~= 'name' then
return nil
end
local codeOffset
local finishOffset
local content
for i = 2, 8 do
local next, nextContent = peekToken(i)
if not next or TokenFinishs[Ci+i-1] + 1 ~= TokenStarts[Ci+i] then
if codeOffset then
finishOffset = i
break
end
---不连续的name,无效的
return nil
end
if next == 'code' then
if codeOffset and content ~= nextContent then
-- 暂时不支持多generic
return nil
end
codeOffset = i
pattern = pattern .. "%s"
content = nextContent
elseif next ~= 'name' then
return nil
else
pattern = pattern .. nextContent
end
end
local start = getStart()
for i = 2 , finishOffset do
nextToken()
end
local code = {
type = 'doc.type.code',
start = start,
finish = getFinish(),
parent = parent,
pattern = pattern,
[1] = content,
}
return code
end

local function parseInteger(parent)
local tp, content = peekToken()
if not tp or tp ~= 'integer' then
Expand Down Expand Up @@ -760,6 +807,7 @@ function parseTypeUnit(parent)
or parseInteger(parent)
or parseBoolean(parent)
or parseParen(parent)
or parseCodePattern(parent)
if not result then
result = parseName('doc.type.name', parent)
or parseDots('doc.type.name', parent)
Expand Down
2 changes: 1 addition & 1 deletion script/vm/sign.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function mt:resolve(uri, args)
for n in node:eachObject() do
if n.type == 'string' then
---@cast n parser.object
local type = vm.declareGlobal('type', n[1], guide.getUri(n))
local type = vm.declareGlobal('type', object.pattern and object.pattern:format(n[1]) or n[1], guide.getUri(n))
resolved[key] = vm.createNode(type, resolved[key])
end
end
Expand Down
21 changes: 21 additions & 0 deletions test/completion/common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4413,3 +4413,24 @@ new 'A' {
kind = define.CompletionItemKind.Property,
}
}

TEST [[
---@class namespace.A
---@overload fun(x: {id: string})
---@generic T
---@param t namespace.`T`
---@return T
local function new(t) end
new 'A' {
<??>
}
]]
{
{
label = 'id',
kind = define.CompletionItemKind.Property,
}
}

30 changes: 30 additions & 0 deletions test/definition/luadoc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,21 @@ local v1 = Generic(Foo)
print(v1.<?bar1?>)
]]


TEST [[
---@class n.Foo
local Foo = {}
function Foo:bar1() end
---@generic T
---@param arg1 n.`T`
---@return T
function Generic(arg1) print(arg1) end
local v1 = Generic(Foo)
print(v1.<?bar1?>)
]]

TEST [[
---@class Foo
local Foo = {}
Expand All @@ -318,6 +333,21 @@ local v1 = Generic("Foo")
print(v1.<?bar1?>)
]]


TEST [[
---@class n.Foo
local Foo = {}
function Foo:<!bar1!>() end
---@generic T
---@param arg1 n.`T`
---@return T
function Generic(arg1) print(arg1) end
local v1 = Generic("Foo")
print(v1.<?bar1?>)
]]

TEST [[
---@class A
local t
Expand Down

0 comments on commit e930179

Please sign in to comment.