diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2020-11-11 13:24:00 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2020-11-11 13:24:00 +0800 |
commit | d4c83bca160fd4215aad241f466bb2485d9e00e5 (patch) | |
tree | bbca4ba9e3f1b9136a2d539fa08086c208321b8b | |
parent | c226b214cbbf23a39ec3a065c9cadc5e17acd883 (diff) | |
download | lua-language-server-d4c83bca160fd4215aad241f466bb2485d9e00e5.zip |
doc.resume
-rw-r--r-- | script-beta/core/hover/description.lua | 63 | ||||
-rw-r--r-- | script-beta/parser/luadoc.lua | 102 | ||||
-rw-r--r-- | script-beta/vm/getDocs.lua | 5 | ||||
-rw-r--r-- | test-beta/crossfile/hover.lua | 23 |
4 files changed, 123 insertions, 70 deletions
diff --git a/script-beta/core/hover/description.lua b/script-beta/core/hover/description.lua index dfb260b0..afe90c05 100644 --- a/script-beta/core/hover/description.lua +++ b/script-beta/core/hover/description.lua @@ -180,14 +180,7 @@ local function tryLibrary(source) return md:string() end -local function tryDocComment(source) - if source.type == 'field' - or source.type == 'method' then - source = source.parent - end - if not source.bindDocs then - return - end +local function getBindComment(source) local lines = {} for _, doc in ipairs(source.bindDocs) do if doc.type == 'doc.comment' then @@ -197,10 +190,62 @@ local function tryDocComment(source) end end if #lines == 0 then + return nil + end + return table.concat(lines, '\n') +end + +local function buildEnumChunk(docType, name) + local enums = vm.getDocEnums(docType) + if #enums == 0 then + return + end + local types = {} + for _, tp in ipairs(docType.types) do + types[#types+1] = tp[1] + end + local lines = {} + lines[#lines+1] = ('%s: %s'):format(name, table.concat(types)) + for _, enum in ipairs(enums) do + lines[#lines+1] = (' |%s%s%s'):format( + enum.default and '>' or ' ', + enum[1], + enum.comment and (' -- %s'):format(enum.comment) or '' + ) + end + return table.concat(lines, '\n') +end + +local function getBindEnums(source) + local chunks = {} + for _, doc in ipairs(source.bindDocs) do + if doc.type == 'doc.param' then + chunks[#chunks+1] = buildEnumChunk(doc.extends, doc.param[1]) + end + end + if #chunks == 0 then + return nil + end + return table.concat(chunks, '\n\n') +end + +local function tryDocComment(source) + if source.type == 'field' + or source.type == 'method' then + source = source.parent + end + if not source.bindDocs then return end + local comment = getBindComment(source) + local enums = getBindEnums(source) local md = markdown() - md:add('md', table.concat(lines, '\n')) + if comment then + md:add('md', comment) + end + if enums then + md:add('lua', enums) + end return md:string() end diff --git a/script-beta/parser/luadoc.lua b/script-beta/parser/luadoc.lua index 6cdd7491..416bce20 100644 --- a/script-beta/parser/luadoc.lua +++ b/script-beta/parser/luadoc.lua @@ -4,7 +4,7 @@ local lines = require 'parser.lines' local guide = require 'parser.guide' local TokenTypes, TokenStarts, TokenFinishs, TokenContents -local Ci, Offset, pushError, Ct +local Ci, Offset, pushError, Ct, NextComment local parseType local Parser = re.compile([[ Main <- (Token / Sp)* @@ -53,7 +53,6 @@ Symbol <- ({} { / '(' / ')' / '?' - / '#' } {}) -> Symbol ]], { @@ -362,6 +361,32 @@ local function parseTypeUnit(parent, content) return typeUnit end +local function parseResume() + local result = { + type = 'doc.resume' + } + + if checkToken('symbol', '>', 1) then + nextToken() + result.default = true + end + + local tp = peekToken() + if tp ~= 'string' then + pushError { + type = 'LUADOC_MISS_STRING', + start = getFinish(), + finish = getFinish(), + } + return nil + end + local _, str = nextToken() + result[1] = str + result.start = getStart() + result.finish = getFinish() + return result +end + function parseType(parent) if not peekToken() then pushError { @@ -412,6 +437,27 @@ function parseType(parent) nextToken() end result.finish = getFinish() + + while true do + local nextComm = NextComment('peek') + if nextComm and nextComm.text:sub(1, 2) == '-|' then + NextComment() + if not result.resumes then + result.resumes = {} + end + local finishPos = nextComm.text:find('#', 3) or #nextComm.text + parseTokens(nextComm.text:sub(3, finishPos), nextComm.start + 1) + local resume = parseResume() + if resume then + resume.comment = nextComm.text:match('#%s*(.+)', 3) + result.resumes[#result.resumes+1] = resume + result.finish = resume.finish + end + else + break + end + end + if #result.types == 0 and #result.enums == 0 then return nil end @@ -630,32 +676,6 @@ local function parseOverload() return result end -local function parseResume() - local result = { - type = 'doc.resume' - } - - if checkToken('symbol', '>', 1) then - nextToken() - result.default = true - end - - local tp = peekToken() - if tp ~= 'string' then - pushError { - type = 'LUADOC_MISS_STRING', - start = getFinish(), - finish = getFinish(), - } - return nil - end - local _, str = nextToken() - result[1] = str - result.start = getStart() - result.finish = getFinish() - return result -end - local function convertTokens() local tp, text = nextToken() if not tp then @@ -690,7 +710,7 @@ local function convertTokens() end end -local function buildLuaDoc(comment, nextComment) +local function buildLuaDoc(comment) local text = comment.text if text:sub(1, 1) ~= '-' then return @@ -718,24 +738,6 @@ local function buildLuaDoc(comment, nextComment) result.comment = lastComment end - while true do - local nextComm = nextComment('peek') - if nextComm and nextComm.text:sub(1, 2) == '-|' then - nextComment() - if not result.resumes then - result.resumes = {} - end - parseTokens(nextComm.text:sub(3), nextComm.start + 1) - local resume = parseResume() - if resume then - result.resumes[#result.resumes+1] = resume - result.finish = resume.finish - end - else - break - end - end - return result end @@ -839,7 +841,7 @@ return function (_, state) pushError = state.pushError local ci = 1 - local function nextComment(peek) + NextComment = function (peek) local comment = comments[ci] if not peek then ci = ci + 1 @@ -848,11 +850,11 @@ return function (_, state) end while true do - local comment = nextComment() + local comment = NextComment() if not comment then break end - local doc = buildLuaDoc(comment, nextComment) + local doc = buildLuaDoc(comment) if doc then ast.docs[#ast.docs+1] = doc doc.parent = ast.docs diff --git a/script-beta/vm/getDocs.lua b/script-beta/vm/getDocs.lua index 2763addb..b1641cec 100644 --- a/script-beta/vm/getDocs.lua +++ b/script-beta/vm/getDocs.lua @@ -87,6 +87,11 @@ function vm.getDocEnums(doc, mark, results) for _, enum in ipairs(doc.enums) do results[#results+1] = enum end + if doc.resumes then + for _, resume in ipairs(doc.resumes) do + results[#results+1] = resume + end + end for _, unit in ipairs(doc.types) do if unit.type == 'doc.type.name' then for _, other in ipairs(vm.getDocTypes(unit[1])) do diff --git a/test-beta/crossfile/hover.lua b/test-beta/crossfile/hover.lua index c743f7a5..5e915803 100644 --- a/test-beta/crossfile/hover.lua +++ b/test-beta/crossfile/hover.lua @@ -309,15 +309,17 @@ TEST { ]] }, hover = { - label = 'function f(x: string)', + label = "function f(x: string|'选项1'|'选项2')", name = 'f', description = [[ +```lua x: string - | '选项1' -- 注释1 - |>'选项2' -- 注释2]] + | '选项1' -- 注释1 + |>'选项2' -- 注释2 +```]] } } - +do return end TEST { { path = 'a.lua', @@ -336,16 +338,15 @@ TEST { hover = { label = 'function f(x: option)', name = 'f', - args = EXISTS, - rawEnum = EXISTS, - enum = [[ - + description = [[ +```lua x: option - | '选项1' -- 注释1 - |>'选项2' -- 注释2]] + | '选项1' -- 注释1 + |>'选项2' -- 注释2 +```]] } } - +do return end TEST { { path = 'a.lua', |