Skip to content

Commit 46910e4

Browse files
committed
resolve #1248 supports spaces before --[[@as]]
1 parent 72fccb7 commit 46910e4

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

script/parser/luadoc.lua

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ Symbol <- ({} {
142142
---@field signs parser.object[]
143143
---@field originalComment parser.object
144144
---@field as? parser.object
145+
---@field touch? integer
145146

146147
local function trim(str)
147148
return str:match '^%s*(%S+)%s*$'
@@ -1671,6 +1672,22 @@ local function bindDocs(state)
16711672
end
16721673
end
16731674

1675+
local function findTouch(state, doc)
1676+
local text = state.lua
1677+
local pos = guide.positionToOffset(state, doc.originalComment.start)
1678+
for i = pos - 2, 1, -1 do
1679+
local c = text:sub(i, i)
1680+
if c == '\r'
1681+
or c == '\n' then
1682+
break
1683+
elseif c ~= ' '
1684+
and c ~= '\t' then
1685+
doc.touch = guide.offsetToPosition(state, i)
1686+
break
1687+
end
1688+
end
1689+
end
1690+
16741691
return function (state)
16751692
local ast = state.ast
16761693
local comments = state.comms
@@ -1714,6 +1731,9 @@ return function (state)
17141731
ast.finish = doc.finish
17151732
end
17161733
doc.originalComment = comment
1734+
if comment.type == 'comment.long' then
1735+
findTouch(state, doc)
1736+
end
17171737
end
17181738
end
17191739

script/vm/compiler.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -585,12 +585,12 @@ local function bindAs(source)
585585
ases = {}
586586
docs._asCache = ases
587587
for _, doc in ipairs(docs) do
588-
if doc.type == 'doc.as' and doc.as then
588+
if doc.type == 'doc.as' and doc.as and doc.touch then
589589
ases[#ases+1] = doc
590590
end
591591
end
592592
table.sort(ases, function (a, b)
593-
return a.start < b.start
593+
return a.touch < b.touch
594594
end)
595595
end
596596

@@ -609,15 +609,15 @@ local function bindAs(source)
609609
end
610610
index = left + (right - left) // 2
611611
local doc = ases[index]
612-
if doc.originalComment.start < source.finish + 2 then
612+
if doc.touch < source.finish then
613613
left = index + 1
614614
else
615615
right = index
616616
end
617617
end
618618

619619
local doc = ases[index]
620-
if doc and doc.originalComment.start == source.finish + 2 then
620+
if doc and doc.touch == source.finish then
621621
vm.setNode(source, vm.compileNode(doc.as), true)
622622
return true
623623
end

test/type_inference/init.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3230,3 +3230,7 @@ end
32303230
32313231
local <?x?> = f()
32323232
]=]
3233+
3234+
TEST 'number' [=[
3235+
local <?x?> = X --[[@as number]]
3236+
]=]

0 commit comments

Comments
 (0)