Skip to content

Commit 8a6d9db

Browse files
authored
Merge pull request #3028 from TIMONz1535/TIMONz1535-patch-3
LuaDoc. Fixed the start position of the comment first symbol in docs
2 parents a582427 + fc81682 commit 8a6d9db

File tree

5 files changed

+34
-29
lines changed

5 files changed

+34
-29
lines changed

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
```
1414
* `NEW` Test CLI: `--name=<testname>` `-n=<testname>`: run specify unit test
1515
* `FIX` Fixed the error that the configuration file pointed to by the `--configpath` option was not read and loaded.
16+
* `FIX` Fixed the comment calculating in docs `---@param a string?Comment` - now its `Comment` instead of `omment`.
1617
* `NEW` `---@class` supports attribute `partial`, which will not check missing inherited fields [#3023](https://github.com/LuaLS/lua-language-server/issues/3023)
1718
```lua
1819
---@class Config

script/core/semantic-tokens.lua

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -902,23 +902,23 @@ return function (uri, start, finish)
902902
return
903903
end
904904
if start <= comm.start and comm.finish <= finish then
905+
-- the same logic as in buildLuaDoc
905906
local headPos = (comm.type == 'comment.short' and comm.text:match '^%-%s*[@|]()')
906-
or (comm.type == 'comment.long' and comm.text:match '^@()')
907+
or (comm.type == 'comment.long' and comm.text:match '^%s*@()')
907908
if headPos then
908-
local atPos
909-
if comm.type == 'comment.short' then
910-
atPos = headPos + 2
911-
else
912-
atPos = headPos + #comm.mark
909+
-- absolute position of `@` symbol
910+
local startOffset = comm.start + headPos
911+
if comm.type == 'comment.long' then
912+
startOffset = comm.start + headPos + #comm.mark - 2
913913
end
914914
results[#results+1] = {
915915
start = comm.start,
916-
finish = comm.start + atPos - 2,
916+
finish = startOffset,
917917
type = define.TokenTypes.comment,
918918
}
919919
results[#results+1] = {
920-
start = comm.start + atPos - 2,
921-
finish = comm.start + atPos - 1 + #comm.text:match('%S*', headPos),
920+
start = startOffset,
921+
finish = startOffset + #comm.text:match('%S*', headPos) + 1,
922922
type = define.TokenTypes.keyword,
923923
modifieres = define.TokenModifiers.documentation,
924924
}

script/parser/compile.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ local function skipComment(isAction)
590590
local result, right = resolveLongString '*/'
591591
pushLongCommentError(left, right)
592592
State.comms[#State.comms+1] = {
593-
type = 'comment.long',
593+
type = 'comment.clong',
594594
start = left,
595595
finish = right,
596596
text = result,

script/parser/luadoc.lua

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1713,10 +1713,9 @@ local function trimTailComment(text)
17131713
end
17141714

17151715
local function buildLuaDoc(comment)
1716-
local text = comment.text
1717-
local startPos = (comment.type == 'comment.short' and text:match '^%-%s*@()')
1718-
or (comment.type == 'comment.long' and text:match '^@()')
1719-
if not startPos then
1716+
local headPos = (comment.type == 'comment.short' and comment.text:match '^%-%s*@()')
1717+
or (comment.type == 'comment.long' and comment.text:match '^%s*@()')
1718+
if not headPos then
17201719
return {
17211720
type = 'doc.comment',
17221721
start = comment.start,
@@ -1725,42 +1724,47 @@ local function buildLuaDoc(comment)
17251724
comment = comment,
17261725
}
17271726
end
1728-
local startOffset = comment.start
1727+
-- absolute position of `@` symbol
1728+
local startOffset = comment.start + headPos
17291729
if comment.type == 'comment.long' then
1730-
startOffset = startOffset + #comment.mark - 2
1730+
startOffset = comment.start + headPos + #comment.mark - 2
17311731
end
17321732

1733-
local doc = text:sub(startPos)
1733+
local doc = comment.text:sub(headPos)
17341734

1735-
parseTokens(doc, startOffset + startPos)
1735+
parseTokens(doc, startOffset)
17361736
local result, rests = convertTokens(doc)
17371737
if result then
17381738
result.range = math.max(comment.finish, result.finish)
17391739
local finish = result.firstFinish or result.finish
17401740
if rests then
17411741
for _, rest in ipairs(rests) do
1742-
rest.range = comment.finish
1743-
finish = rest.firstFinish or result.finish
1742+
rest.range = math.max(comment.finish, rest.finish)
1743+
finish = rest.firstFinish or rest.finish
17441744
end
17451745
end
1746-
local cstart = text:find('%S', finish - comment.start)
1747-
if cstart and cstart < comment.finish then
1746+
1747+
-- `result` can be a multiline annotation or an alias, while `doc` is the first line, so we can't parse comment
1748+
if finish >= comment.finish then
1749+
return result, rests
1750+
end
1751+
1752+
local cstart = doc:find('%S', finish - startOffset)
1753+
if cstart then
17481754
result.comment = {
17491755
type = 'doc.tailcomment',
1750-
start = cstart + comment.start,
1756+
start = startOffset + cstart,
17511757
finish = comment.finish,
17521758
parent = result,
1753-
text = trimTailComment(text:sub(cstart)),
1759+
text = trimTailComment(doc:sub(cstart)),
17541760
}
17551761
if rests then
17561762
for _, rest in ipairs(rests) do
17571763
rest.comment = result.comment
17581764
end
17591765
end
17601766
end
1761-
end
17621767

1763-
if result then
17641768
return result, rests
17651769
end
17661770

script/vm/sign.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ local vm = require 'vm.vm'
55
---@class vm.sign
66
---@field parent parser.object
77
---@field signList vm.node[]
8-
---@field docGenric parser.object[]
8+
---@field docGeneric parser.object[]
99
local mt = {}
1010
mt.__index = mt
1111
mt.type = 'sign'
@@ -17,7 +17,7 @@ end
1717

1818
---@param doc parser.object
1919
function mt:addDocGeneric(doc)
20-
self.docGenric[#self.docGenric+1] = doc
20+
self.docGeneric[#self.docGeneric+1] = doc
2121
end
2222

2323
---@param uri uri
@@ -268,7 +268,7 @@ end
268268
function vm.createSign()
269269
local genericMgr = setmetatable({
270270
signList = {},
271-
docGenric = {},
271+
docGeneric = {},
272272
}, mt)
273273
return genericMgr
274274
end

0 commit comments

Comments
 (0)