Skip to content

Commit cb964c6

Browse files
authored
Merge pull request #3015 from TIMONz1535/TIMONz1535-patch-2
Improve generic pattern to support "`T`.*-"
2 parents 208542c + 002f4a8 commit cb964c6

File tree

3 files changed

+103
-34
lines changed

3 files changed

+103
-34
lines changed

changelog.md

+9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22

33
## Unreleased
44
<!-- Add all new changes here. They will be moved under a version at release -->
5+
* `CHG` [#3014] Generic pattern now supports definition after capture
6+
```lua
7+
---@generic T
8+
---@param t `T`.Cat
9+
---@return T
10+
local function f(t) end
11+
12+
local t = f('Smile') --> t is `Smile.Cat`
13+
```
514
* `NEW` Test CLI: `--name=<testname>` `-n=<testname>`: run specify unit test
615
* `FIX` Fixed the error that the configuration file pointed to by the `--configpath` option was not read and loaded.
716

script/parser/luadoc.lua

+35-29
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,13 @@ EChar <- 'a' -> ea
5454
/ ([0-9] [0-9]? [0-9]?) -> Char10
5555
/ ('u{' {X16*} '}') -> CharUtf8
5656
Symbol <- ({} {
57-
[:|,;<>()?+#{}]
57+
[:|,;<>()?+#{}*]
5858
/ '[]'
5959
/ '...'
6060
/ '['
6161
/ ']'
6262
/ '-' !'-'
63+
/ '.' !'..'
6364
} {})
6465
-> Symbol
6566
]], {
@@ -720,57 +721,63 @@ local function parseString(parent)
720721
return str
721722
end
722723

723-
local function parseCode(parent)
724-
local tp, content = peekToken()
725-
if not tp or tp ~= 'code' then
726-
return nil
727-
end
728-
nextToken()
729-
local code = {
730-
type = 'doc.type.code',
731-
start = getStart(),
732-
finish = getFinish(),
733-
parent = parent,
734-
[1] = content,
735-
}
736-
return code
737-
end
738-
739724
local function parseCodePattern(parent)
740725
local tp, pattern = peekToken()
741-
if not tp or tp ~= 'name' then
726+
if not tp or (tp ~= 'name' and tp ~= 'code') then
742727
return nil
743728
end
744729
local codeOffset
745730
local finishOffset
746731
local content
747-
for i = 2, 8 do
732+
local i = 1
733+
if tp == 'code' then
734+
codeOffset = i
735+
content = pattern
736+
pattern = '%s'
737+
end
738+
while true do
739+
i = i + 1
748740
local next, nextContent = peekToken(i)
749741
if not next or TokenFinishs[Ci+i-1] + 1 ~= TokenStarts[Ci+i] then
750742
if codeOffset then
751-
finishOffset = i
743+
finishOffset = i-1
752744
break
753745
end
754746
---不连续的name,无效的
755747
return nil
756748
end
757-
if next == 'code' then
758-
if codeOffset and content ~= nextContent then
749+
if next == 'name' then
750+
pattern = pattern .. nextContent
751+
elseif next == 'code' then
752+
if codeOffset then
759753
-- 暂时不支持多generic
760754
return nil
761755
end
762756
codeOffset = i
763-
pattern = pattern .. "%s"
757+
pattern = pattern .. '%s'
764758
content = nextContent
765-
elseif next ~= 'name' then
766-
return nil
759+
elseif codeOffset then
760+
-- should be match with Parser "name" mask
761+
if next == 'integer' then
762+
pattern = pattern .. nextContent
763+
elseif next == 'symbol' and (nextContent == '.' or nextContent == '*' or nextContent == '-') then
764+
pattern = pattern .. nextContent
765+
else
766+
return nil
767+
end
767768
else
768-
pattern = pattern .. nextContent
769+
return nil
769770
end
770771
end
772+
nextToken()
771773
local start = getStart()
772-
for _ = 2, finishOffset do
773-
nextToken()
774+
if finishOffset == 1 then
775+
-- code only, no pattern
776+
pattern = nil
777+
else
778+
for _ = 2, finishOffset do
779+
nextToken()
780+
end
774781
end
775782
local code = {
776783
type = 'doc.type.code',
@@ -834,7 +841,6 @@ function parseTypeUnit(parent)
834841
or parseTable(parent)
835842
or parseTuple(parent)
836843
or parseString(parent)
837-
or parseCode(parent)
838844
or parseInteger(parent)
839845
or parseBoolean(parent)
840846
or parseParen(parent)

test/definition/luadoc.lua

+59-5
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ TEST [[
239239
AAAA = {};
240240
241241
function AAAA:<!SSDF!>()
242-
242+
243243
end
244244
245245
AAAA.a.<?SSDF?>
@@ -304,6 +304,19 @@ local v1 = Generic(Foo)
304304
print(v1.<?bar1?>)
305305
]]
306306

307+
TEST [[
308+
---@class Foo
309+
local Foo = {}
310+
function Foo:<!bar1!>() end
311+
312+
---@generic T
313+
---@param arg1 `T`
314+
---@return T
315+
function Generic(arg1) print(arg1) end
316+
317+
local v1 = Generic("Foo")
318+
print(v1.<?bar1?>)
319+
]]
307320

308321
TEST [[
309322
---@class n.Foo
@@ -320,27 +333,68 @@ print(v1.<?bar1?>)
320333
]]
321334

322335
TEST [[
323-
---@class Foo
336+
---@class n.Foo
324337
local Foo = {}
325338
function Foo:<!bar1!>() end
326339
327340
---@generic T
328-
---@param arg1 `T`
341+
---@param arg1 n.`T`
329342
---@return T
330343
function Generic(arg1) print(arg1) end
331344
332345
local v1 = Generic("Foo")
333346
print(v1.<?bar1?>)
334347
]]
335348

349+
TEST [[
350+
---@class Foo*
351+
local Foo = {}
352+
function Foo:bar1() end
353+
354+
---@generic T
355+
---@param arg1 `T`*
356+
---@return T
357+
function Generic(arg1) print(arg1) end
358+
359+
local v1 = Generic(Foo)
360+
print(v1.<?bar1?>)
361+
]]
336362

337363
TEST [[
338-
---@class n.Foo
364+
---@class Foo*
339365
local Foo = {}
340366
function Foo:<!bar1!>() end
341367
342368
---@generic T
343-
---@param arg1 n.`T`
369+
---@param arg1 `T`*
370+
---@return T
371+
function Generic(arg1) print(arg1) end
372+
373+
local v1 = Generic("Foo")
374+
print(v1.<?bar1?>)
375+
]]
376+
377+
TEST [[
378+
---@class n.Foo.2
379+
local Foo = {}
380+
function Foo:bar1() end
381+
382+
---@generic T
383+
---@param arg1 n.`T`.2
384+
---@return T
385+
function Generic(arg1) print(arg1) end
386+
387+
local v1 = Generic(Foo)
388+
print(v1.<?bar1?>)
389+
]]
390+
391+
TEST [[
392+
---@class n.Foo.2
393+
local Foo = {}
394+
function Foo:<!bar1!>() end
395+
396+
---@generic T
397+
---@param arg1 n.`T`.2
344398
---@return T
345399
function Generic(arg1) print(arg1) end
346400

0 commit comments

Comments
 (0)