Skip to content

Commit

Permalink
LuaLS#3014, LuaLS#3015 Generic pattern supports optional, union, arra…
Browse files Browse the repository at this point in the history
…y and comment without a space. Fixed regression.
  • Loading branch information
TIMONz1535 committed Jan 6, 2025
1 parent cb964c6 commit 48f1f66
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 25 deletions.
8 changes: 4 additions & 4 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

## Unreleased
<!-- Add all new changes here. They will be moved under a version at release -->
* `CHG` [#3014] Generic pattern now supports definition after capture
* `CHG` [#3014] Generic pattern now supports definition after capture and optional, union, array
```lua
---@generic T
---@param t `T`.Cat
---@return T
---@param t `T`.Cat?
---@return T?
local function f(t) end

local t = f('Smile') --> t is `Smile.Cat`
local t = f('Smile') --> t is `(Smile.Cat)?`
```
* `NEW` Test CLI: `--name=<testname>` `-n=<testname>`: run specify unit test
* `FIX` Fixed the error that the configuration file pointed to by the `--configpath` option was not read and loaded.
Expand Down
35 changes: 17 additions & 18 deletions script/parser/luadoc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ Symbol <- ({} {
er = '\r',
et = '\t',
ev = '\v',
name = (m.R('az', 'AZ', '09', '\x80\xff') + m.S('_')) * (m.R('az', 'AZ', '__', '09', '\x80\xff') + m.S('_.*-'))^0,
name = (m.R('az', 'AZ', '09', '\x80\xff') + m.S('_')) * (m.R('az', 'AZ', '09', '\x80\xff') + m.S('_.*-'))^0,
Char10 = function (char)
---@type integer?
char = tonumber(char)
Expand Down Expand Up @@ -727,7 +727,6 @@ local function parseCodePattern(parent)
return nil
end
local codeOffset
local finishOffset
local content
local i = 1
if tp == 'code' then
Expand All @@ -736,41 +735,41 @@ local function parseCodePattern(parent)
pattern = '%s'
end
while true do
i = i + 1
local next, nextContent = peekToken(i)
if not next or TokenFinishs[Ci+i-1] + 1 ~= TokenStarts[Ci+i] then
if codeOffset then
finishOffset = i-1
break
end
i = i+1
local nextTp, nextContent = peekToken(i)
if not nextTp or TokenFinishs[Ci+i-1] + 1 ~= TokenStarts[Ci+i] then
---不连续的name,无效的
return nil
break
end
if next == 'name' then
if nextTp == 'name' then
pattern = pattern .. nextContent
elseif next == 'code' then
elseif nextTp == 'code' then
if codeOffset then
-- 暂时不支持多generic
return nil
break
end
codeOffset = i
pattern = pattern .. '%s'
content = nextContent
elseif codeOffset then
-- should be match with Parser "name" mask
if next == 'integer' then
if nextTp == 'integer' then
pattern = pattern .. nextContent
elseif next == 'symbol' and (nextContent == '.' or nextContent == '*' or nextContent == '-') then
elseif nextTp == 'symbol' and (nextContent == '.' or nextContent == '*' or nextContent == '-') then
pattern = pattern .. nextContent
else
return nil
break
end
else
return nil
break
end
end
if not codeOffset then
return nil
end
nextToken()
local start = getStart()
local finishOffset = i-1
if finishOffset == 1 then
-- code only, no pattern
pattern = nil
Expand Down Expand Up @@ -932,7 +931,7 @@ function parseType(parent)
local function pushResume()
local comments
for i = 0, 100 do
local nextComm = NextComment(i,'peek')
local nextComm = NextComment(i, true)
if not nextComm then
return false
end
Expand Down
34 changes: 31 additions & 3 deletions test/definition/luadoc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ TEST [[
AAAA = {};
function AAAA:<!SSDF!>()
end
AAAA.a.<?SSDF?>
Expand Down Expand Up @@ -352,7 +352,7 @@ local Foo = {}
function Foo:bar1() end
---@generic T
---@param arg1 `T`*
---@param arg1 `T`*?
---@return T
function Generic(arg1) print(arg1) end
Expand All @@ -366,7 +366,7 @@ local Foo = {}
function Foo:<!bar1!>() end
---@generic T
---@param arg1 `T`*
---@param arg1 `T`*?
---@return T
function Generic(arg1) print(arg1) end
Expand Down Expand Up @@ -402,6 +402,34 @@ local v1 = Generic("Foo")
print(v1.<?bar1?>)
]]

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

TEST [[
---@class n-Foo-2
local Foo = {}
function Foo:<!bar1!>() end
---@generic T
---@param arg1 n.`T`-2[]
---@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 48f1f66

Please sign in to comment.