@@ -1691,6 +1691,18 @@ local function convertTokens(doc)
1691
1691
return docSwitch (text , doc )
1692
1692
end
1693
1693
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
+
1694
1706
local function trimTailComment (text )
1695
1707
local comment = text
1696
1708
if text :sub (1 , 1 ) == ' @' then
@@ -1743,15 +1755,11 @@ local function buildLuaDoc(comment)
1743
1755
finish = rest .firstFinish or result .finish
1744
1756
end
1745
1757
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
1752
1760
result .comment = {
1753
1761
type = ' doc.tailcomment' ,
1754
- start = comment . start + cstart - 1 + textOffset ,
1762
+ start = convertCommentPosToLinePos ( comment , cstart ) ,
1755
1763
finish = comment .finish ,
1756
1764
parent = result ,
1757
1765
text = trimTailComment (text :sub (cstart )),
0 commit comments