Skip to content

Commit 537aaed

Browse files
committed
LuaDoc. Conversion functions and better readability. Fixed condition check.
1 parent 88c252c commit 537aaed

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

script/parser/luadoc.lua

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1691,6 +1691,18 @@ local function convertTokens(doc)
16911691
return docSwitch(text, doc)
16921692
end
16931693

1694+
local function convertLinePosToCommentPos(comment, linePos)
1695+
-- line pos is 0-based relative to original line of text before `--` prefix is removed
1696+
-- while comment pos is 1-based and with `--` prefix removed
1697+
-- but even if we subtracted 1-based line poses, we would get 0-based "length", so convert #3028
1698+
return linePos - comment.start + 1 - 2
1699+
end
1700+
1701+
local function convertCommentPosToLinePos(comment, commentPos)
1702+
-- the reverse of convertLinePosToCommentPos()
1703+
return commentPos + 2 - 1 + comment.start
1704+
end
1705+
16941706
local function trimTailComment(text)
16951707
local comment = text
16961708
if text:sub(1, 1) == '@' then
@@ -1743,15 +1755,11 @@ local function buildLuaDoc(comment)
17431755
finish = rest.firstFinish or result.finish
17441756
end
17451757
end
1746-
-- the subtraction is the length of the skipped string, so the start position is `+ 1`
1747-
local cpos = finish - comment.start + 1
1748-
-- actual text start is `comment.start + 2` because of `--` for some reason
1749-
local textOffset = 2
1750-
local cstart = text:find('%S', cpos - textOffset)
1751-
if cstart and cstart < comment.finish - textOffset then
1758+
local cstart = text:find('%S', convertLinePosToCommentPos(comment, finish))
1759+
if cstart and convertCommentPosToLinePos(comment, cstart) < comment.finish then
17521760
result.comment = {
17531761
type = 'doc.tailcomment',
1754-
start = comment.start + cstart - 1 + textOffset,
1762+
start = convertCommentPosToLinePos(comment, cstart),
17551763
finish = comment.finish,
17561764
parent = result,
17571765
text = trimTailComment(text:sub(cstart)),

0 commit comments

Comments
 (0)