From 9ba27a6edd0ec0c73c3d59b3b1b028a1a46138c6 Mon Sep 17 00:00:00 2001 From: Shararvev Date: Sun, 5 Jan 2025 03:36:45 +0500 Subject: [PATCH 1/3] #3028. Fix calculating the comment over `doc` not `text` in buildLuaDoc, sync with semantic logic. Fix crash 'comment.clong' type for /*, fix typo docGeneric. --- changelog.md | 1 + script/core/semantic-tokens.lua | 16 ++++++++-------- script/parser/compile.lua | 2 +- script/parser/luadoc.lua | 24 ++++++++++++------------ script/vm/sign.lua | 6 +++--- 5 files changed, 25 insertions(+), 24 deletions(-) diff --git a/changelog.md b/changelog.md index 8b5f93f76..593038aed 100644 --- a/changelog.md +++ b/changelog.md @@ -13,6 +13,7 @@ ``` * `NEW` Test CLI: `--name=` `-n=`: run specify unit test * `FIX` Fixed the error that the configuration file pointed to by the `--configpath` option was not read and loaded. +* `FIX` Fixed the comment calculating in docs `---@param a string?Comment` - now its `Comment` instead of `omment`. ## 3.13.5 `2024-12-20` diff --git a/script/core/semantic-tokens.lua b/script/core/semantic-tokens.lua index 25df6b3c1..ad4162e6f 100644 --- a/script/core/semantic-tokens.lua +++ b/script/core/semantic-tokens.lua @@ -902,23 +902,23 @@ return function (uri, start, finish) return end if start <= comm.start and comm.finish <= finish then + -- the same logic as in buildLuaDoc local headPos = (comm.type == 'comment.short' and comm.text:match '^%-%s*[@|]()') or (comm.type == 'comment.long' and comm.text:match '^@()') if headPos then - local atPos - if comm.type == 'comment.short' then - atPos = headPos + 2 - else - atPos = headPos + #comm.mark + -- absolute position of `@` symbol + local startOffset = comm.start + headPos + if comm.type == 'comment.long' then + startOffset = comm.start + headPos + #comm.mark - 2 end results[#results+1] = { start = comm.start, - finish = comm.start + atPos - 2, + finish = startOffset, type = define.TokenTypes.comment, } results[#results+1] = { - start = comm.start + atPos - 2, - finish = comm.start + atPos - 1 + #comm.text:match('%S*', headPos), + start = startOffset, + finish = startOffset + #comm.text:match('%S*', headPos) + 1, type = define.TokenTypes.keyword, modifieres = define.TokenModifiers.documentation, } diff --git a/script/parser/compile.lua b/script/parser/compile.lua index fc26bbcc3..41b0da17e 100644 --- a/script/parser/compile.lua +++ b/script/parser/compile.lua @@ -590,7 +590,7 @@ local function skipComment(isAction) local result, right = resolveLongString '*/' pushLongCommentError(left, right) State.comms[#State.comms+1] = { - type = 'comment.long', + type = 'comment.clong', start = left, finish = right, text = result, diff --git a/script/parser/luadoc.lua b/script/parser/luadoc.lua index 3fec5c182..8dc046f09 100644 --- a/script/parser/luadoc.lua +++ b/script/parser/luadoc.lua @@ -1713,10 +1713,9 @@ local function trimTailComment(text) end local function buildLuaDoc(comment) - local text = comment.text - local startPos = (comment.type == 'comment.short' and text:match '^%-%s*@()') - or (comment.type == 'comment.long' and text:match '^@()') - if not startPos then + local headPos = (comment.type == 'comment.short' and comment.text:match '^%-%s*@()') + or (comment.type == 'comment.long' and comment.text:match '^@()') + if not headPos then return { type = 'doc.comment', start = comment.start, @@ -1725,14 +1724,15 @@ local function buildLuaDoc(comment) comment = comment, } end - local startOffset = comment.start + -- absolute position of `@` symbol + local startOffset = comment.start + headPos if comment.type == 'comment.long' then - startOffset = startOffset + #comment.mark - 2 + startOffset = comment.start + headPos + #comment.mark - 2 end - local doc = text:sub(startPos) + local doc = comment.text:sub(headPos) - parseTokens(doc, startOffset + startPos) + parseTokens(doc, startOffset) local result, rests = convertTokens(doc) if result then result.range = math.max(comment.finish, result.finish) @@ -1743,14 +1743,14 @@ local function buildLuaDoc(comment) finish = rest.firstFinish or result.finish end end - local cstart = text:find('%S', finish - comment.start) - if cstart and cstart < comment.finish then + local cstart = doc:find('%S', finish - startOffset) + if cstart then result.comment = { type = 'doc.tailcomment', - start = cstart + comment.start, + start = startOffset + cstart, finish = comment.finish, parent = result, - text = trimTailComment(text:sub(cstart)), + text = trimTailComment(doc:sub(cstart)), } if rests then for _, rest in ipairs(rests) do diff --git a/script/vm/sign.lua b/script/vm/sign.lua index ddc8258f5..4f0fe1912 100644 --- a/script/vm/sign.lua +++ b/script/vm/sign.lua @@ -5,7 +5,7 @@ local vm = require 'vm.vm' ---@class vm.sign ---@field parent parser.object ---@field signList vm.node[] ----@field docGenric parser.object[] +---@field docGeneric parser.object[] local mt = {} mt.__index = mt mt.type = 'sign' @@ -17,7 +17,7 @@ end ---@param doc parser.object function mt:addDocGeneric(doc) - self.docGenric[#self.docGenric+1] = doc + self.docGeneric[#self.docGeneric+1] = doc end ---@param uri uri @@ -268,7 +268,7 @@ end function vm.createSign() local genericMgr = setmetatable({ signList = {}, - docGenric = {}, + docGeneric = {}, }, mt) return genericMgr end From cbe45e12aa5a22bad28bbf6867fe090b2fd63195 Mon Sep 17 00:00:00 2001 From: Shararvev Date: Thu, 9 Jan 2025 06:34:04 +0500 Subject: [PATCH 2/3] #3028. Fix incorrect rest.range and finish on multiline types. Do not try to process a multiline `result`, while `doc` is the first line. --- script/parser/luadoc.lua | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/script/parser/luadoc.lua b/script/parser/luadoc.lua index 8dc046f09..a96f9b061 100644 --- a/script/parser/luadoc.lua +++ b/script/parser/luadoc.lua @@ -1739,10 +1739,16 @@ local function buildLuaDoc(comment) local finish = result.firstFinish or result.finish if rests then for _, rest in ipairs(rests) do - rest.range = comment.finish - finish = rest.firstFinish or result.finish + rest.range = math.max(comment.finish, rest.finish) + finish = rest.firstFinish or rest.finish end end + + -- `result` can be a multiline annotation or an alias, while `doc` is the first line, so we can't parse comment + if finish >= comment.finish then + return result, rests + end + local cstart = doc:find('%S', finish - startOffset) if cstart then result.comment = { @@ -1758,9 +1764,7 @@ local function buildLuaDoc(comment) end end end - end - if result then return result, rests end From 4af79e396c0ca7acf2dbcf8419366fc4e86465f8 Mon Sep 17 00:00:00 2001 From: Shararvev Date: Thu, 9 Jan 2025 06:40:58 +0500 Subject: [PATCH 3/3] #3028. Support for a long strings with leading spaces #3034 (pattern is optional) --- script/core/semantic-tokens.lua | 2 +- script/parser/luadoc.lua | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/script/core/semantic-tokens.lua b/script/core/semantic-tokens.lua index ad4162e6f..3a34b758e 100644 --- a/script/core/semantic-tokens.lua +++ b/script/core/semantic-tokens.lua @@ -904,7 +904,7 @@ return function (uri, start, finish) if start <= comm.start and comm.finish <= finish then -- the same logic as in buildLuaDoc local headPos = (comm.type == 'comment.short' and comm.text:match '^%-%s*[@|]()') - or (comm.type == 'comment.long' and comm.text:match '^@()') + or (comm.type == 'comment.long' and comm.text:match '^%s*@()') if headPos then -- absolute position of `@` symbol local startOffset = comm.start + headPos diff --git a/script/parser/luadoc.lua b/script/parser/luadoc.lua index a96f9b061..ea47b63f0 100644 --- a/script/parser/luadoc.lua +++ b/script/parser/luadoc.lua @@ -1714,7 +1714,7 @@ end local function buildLuaDoc(comment) local headPos = (comment.type == 'comment.short' and comment.text:match '^%-%s*@()') - or (comment.type == 'comment.long' and comment.text:match '^@()') + or (comment.type == 'comment.long' and comment.text:match '^%s*@()') if not headPos then return { type = 'doc.comment',