Skip to content

Commit

Permalink
Merge pull request #3031 from TIMONz1535/TIMONz1535-patch-4
Browse files Browse the repository at this point in the history
Improve generic pattern to support optional, union, array. Fixed regression.
  • Loading branch information
sumneko authored Jan 14, 2025
2 parents 8a6d9db + 35810de commit 12013ba
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 26 deletions.
9 changes: 5 additions & 4 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@

## 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.
* `FIX` Generic return can be optional.
* `FIX` Fixed the comment calculating in docs `---@param a string?Comment` - now its `Comment` instead of `omment`.
* `NEW` `---@class` supports attribute `partial`, which will not check missing inherited fields [#3023](https://github.com/LuaLS/lua-language-server/issues/3023)
```lua
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
3 changes: 3 additions & 0 deletions script/vm/generic.lua
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ function mt:resolve(uri, args)
end
end
end
if protoNode:isOptional() then
result:addOptional()
end
return result
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
18 changes: 18 additions & 0 deletions test/diagnostics/missing-return.lua
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,21 @@ function F()
end
]]

TEST [[
---@generic T
---@param t T
---@return T
function F(t)
return t
end
]]

TEST [[
---@generic T
---@param t T
---@return T?
function F(t)
end
]]
26 changes: 25 additions & 1 deletion test/type_inference/common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -359,8 +359,32 @@ end
local _, _, _, <?b?>, _ = x(nil, true, 1, 'yy')
]]

TEST 'nil' [[
local <?k?>, <?v?> = next()
local <?k?>, <?v?> = next({})
]]

TEST 'integer?' [[
local <?k?>, <?v?> = next({1})
]]

TEST 'integer?' [[
local a
local <?k?>, v = next({a})
]]

-- probably explicit unknown generic should be unknown
TEST 'nil' [[
local a
local k, <?v?> = next({a})
]]

TEST 'unknown' [[
local <?x?> = next()
---@generic T
---@param t T?
---@return T
local function F(t) end
local <?x?> = F()
]]

TEST 'unknown' [[
Expand Down

0 comments on commit 12013ba

Please sign in to comment.