Skip to content

Commit f4a69bc

Browse files
committed
LuaLS#3014. Replace parseCode with parseCodePattern to support "T.*-" without name token before code. Added tests.
1 parent 4cfd030 commit f4a69bc

File tree

2 files changed

+75
-28
lines changed

2 files changed

+75
-28
lines changed

Diff for: script/parser/luadoc.lua

+16-23
Original file line numberDiff line numberDiff line change
@@ -720,32 +720,22 @@ local function parseString(parent)
720720
return str
721721
end
722722

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-
739723
local function parseCodePattern(parent)
740724
local tp, pattern = peekToken()
741-
if not tp or tp ~= 'name' then
725+
if not tp or (tp ~= 'name' and tp ~= 'code') then
742726
return nil
743727
end
744728
local codeOffset
745729
local finishOffset
746730
local content
747-
local i = 2
731+
local i = 1
732+
if tp == 'code' then
733+
codeOffset = i
734+
content = pattern
735+
pattern = '%s'
736+
end
748737
while true do
738+
i = i + 1
749739
local next, nextContent = peekToken(i)
750740
if not next or TokenFinishs[Ci+i-1] + 1 ~= TokenStarts[Ci+i] then
751741
if codeOffset then
@@ -763,7 +753,7 @@ local function parseCodePattern(parent)
763753
return nil
764754
end
765755
codeOffset = i
766-
pattern = pattern .. "%s"
756+
pattern = pattern .. '%s'
767757
content = nextContent
768758
elseif codeOffset then
769759
-- should be match with Parser "name" mask
@@ -777,12 +767,16 @@ local function parseCodePattern(parent)
777767
else
778768
return nil
779769
end
780-
i = i + 1
781770
end
782771
nextToken()
783772
local start = getStart()
784-
for _ = 2, finishOffset do
785-
nextToken()
773+
if finishOffset == 1 then
774+
-- code only, no pattern
775+
pattern = nil
776+
else
777+
for _ = 2, finishOffset do
778+
nextToken()
779+
end
786780
end
787781
local code = {
788782
type = 'doc.type.code',
@@ -846,7 +840,6 @@ function parseTypeUnit(parent)
846840
or parseTable(parent)
847841
or parseTuple(parent)
848842
or parseString(parent)
849-
or parseCode(parent)
850843
or parseInteger(parent)
851844
or parseBoolean(parent)
852845
or parseParen(parent)

Diff for: 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*
379+
local Foo = {}
380+
function Foo:bar1() end
381+
382+
---@generic T
383+
---@param arg1 n.`T`*
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*
393+
local Foo = {}
394+
function Foo:<!bar1!>() end
395+
396+
---@generic T
397+
---@param arg1 n.`T`*
344398
---@return T
345399
function Generic(arg1) print(arg1) end
346400

0 commit comments

Comments
 (0)