From 4ad097f786df90286c6212c8fc3c6472e8fd1368 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Fri, 30 Apr 2021 17:59:09 +0800 Subject: stash --- script/config.lua | 1 + script/core/completion.lua | 36 ++++++++++++++++++++++++++++++++---- test/completion/init.lua | 12 ++++++------ 3 files changed, 39 insertions(+), 10 deletions(-) diff --git a/script/config.lua b/script/config.lua index 8bc05b98..97df7c17 100644 --- a/script/config.lua +++ b/script/config.lua @@ -165,6 +165,7 @@ local ConfigTemplate = { displayContext = {6, Integer}, workspaceWord = {true, Boolean}, autoRequire = {true, Boolean}, + showParams = {true, Boolean}, }, signatureHelp = { enable = {true, Boolean}, diff --git a/script/core/completion.lua b/script/core/completion.lua index 98a9250f..6f1a2952 100644 --- a/script/core/completion.lua +++ b/script/core/completion.lua @@ -199,7 +199,6 @@ local function buildFunction(results, source, oop, data) if snipType == 'Both' or snipType == 'Replace' then local snipData = util.deepCopy(data) snipData.kind = define.CompletionItemKind.Snippet - snipData.label = snipData.label .. '()' snipData.insertText = buildFunctionSnip(source, oop) snipData.insertTextFormat = 2 snipData.id = stack(function () @@ -401,7 +400,8 @@ end local function checkFieldThen(name, src, word, start, offset, parent, oop, results) local value = guide.getObjectValue(src) or src local kind = define.CompletionItemKind.Field - if value.type == 'function' then + if value.type == 'function' + or value.type == 'doc.type.function' then if oop then kind = define.CompletionItemKind.Method else @@ -453,6 +453,20 @@ local function checkFieldThen(name, src, word, start, offset, parent, oop, resul } end +local function getParams(func) + local args = {} + for _, arg in ipairs(func.args) do + if arg.type == '...' then + args[#args+1] = '...' + elseif arg.type == 'doc.type.arg' then + args[#args+1] = arg.name[1] + else + args[#args+1] = arg[1] + end + end + return '(' .. table.concat(args, ', ') .. ')' +end + local function checkFieldOfRefs(refs, ast, word, start, offset, parent, oop, results, locals, isGlobal) local fields = {} local count = 0 @@ -470,8 +484,20 @@ local function checkFieldOfRefs(refs, ast, word, start, offset, parent, oop, res if not matchKey(word, name, count >= 100) then goto CONTINUE end + local funcLabel + if config.config.completion.showParams then + local value = guide.getObjectValue(src) or src + if value.type == 'function' + or value.type == 'doc.type.function' then + funcLabel = name .. getParams(value) + fields[funcLabel] = src + fields[name] = false + count = count + 1 + goto CONTINUE + end + end local last = fields[name] - if not last then + if last == nil then fields[name] = src count = count + 1 goto CONTINUE @@ -491,7 +517,9 @@ local function checkFieldOfRefs(refs, ast, word, start, offset, parent, oop, res ::CONTINUE:: end for name, src in util.sortPairs(fields) do - checkFieldThen(name, src, word, start, offset, parent, oop, results) + if src then + checkFieldThen(name, src, word, start, offset, parent, oop, results) + end end end diff --git a/test/completion/init.lua b/test/completion/init.lua index 22103896..1344d3af 100644 --- a/test/completion/init.lua +++ b/test/completion/init.lua @@ -175,11 +175,11 @@ ass$ ]] { { - label = 'assert', + label = 'assert(v, message)', kind = define.CompletionItemKind.Function, }, { - label = 'assert()', + label = 'assert(v, message)', kind = define.CompletionItemKind.Snippet, }, } @@ -201,11 +201,11 @@ _G.ass$ ]] { { - label = 'assert', + label = 'assert(v, message)', kind = define.CompletionItemKind.Function, }, { - label = 'assert()', + label = 'assert(v, message)', kind = define.CompletionItemKind.Snippet, }, } @@ -217,11 +217,11 @@ ff$ ]] { { - label = 'ffff', + label = 'assert(a, b)', kind = define.CompletionItemKind.Function, }, { - label = 'ffff()', + label = 'assert(a, b)', kind = define.CompletionItemKind.Snippet, } } -- cgit v1.2.3 From d948291c63ea17804b37bd4e3e5f123ac63730d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Fri, 7 May 2021 15:24:22 +0800 Subject: stash --- script/core/completion.lua | 64 ++++++++++++--------- test/completion/init.lua | 138 +++++++++++---------------------------------- 2 files changed, 70 insertions(+), 132 deletions(-) diff --git a/script/core/completion.lua b/script/core/completion.lua index 6f1a2952..bab0f452 100644 --- a/script/core/completion.lua +++ b/script/core/completion.lua @@ -252,6 +252,26 @@ local function isSameSource(ast, source, pos) return source.start <= pos and source.finish >= pos end +local function getParams(func, oop) + if not func.args then + return '' + end + local args = {} + for _, arg in ipairs(func.args) do + if arg.type == '...' then + args[#args+1] = '...' + elseif arg.type == 'doc.type.arg' then + args[#args+1] = arg.name[1] + else + args[#args+1] = arg[1] + end + end + if oop and args[1] ~= '...' then + table.remove(args, 1) + end + return '(' .. table.concat(args, ', ') .. ')' +end + local function checkLocal(ast, word, offset, results) local locals = guide.getVisibleLocals(ast.ast, offset) for name, source in pairs(locals) do @@ -262,16 +282,22 @@ local function checkLocal(ast, word, offset, results) goto CONTINUE end if vm.hasType(source, 'function') then - buildFunction(results, source, false, { - label = name, - kind = define.CompletionItemKind.Function, - id = stack(function () - return { - detail = buildDetail(source), - description = buildDesc(source), - } - end), - }) + for _, def in ipairs(vm.getDefs(source, 0)) do + if def.type == 'function' + or def.type == 'doc.type.function' then + local funcLabel = name .. getParams(def, false) + buildFunction(results, source, false, { + label = funcLabel, + kind = define.CompletionItemKind.Function, + id = stack(function () + return { + detail = buildDetail(source), + description = buildDesc(source), + } + end), + }) + end + end else results[#results+1] = { label = name, @@ -453,20 +479,6 @@ local function checkFieldThen(name, src, word, start, offset, parent, oop, resul } end -local function getParams(func) - local args = {} - for _, arg in ipairs(func.args) do - if arg.type == '...' then - args[#args+1] = '...' - elseif arg.type == 'doc.type.arg' then - args[#args+1] = arg.name[1] - else - args[#args+1] = arg[1] - end - end - return '(' .. table.concat(args, ', ') .. ')' -end - local function checkFieldOfRefs(refs, ast, word, start, offset, parent, oop, results, locals, isGlobal) local fields = {} local count = 0 @@ -489,7 +501,7 @@ local function checkFieldOfRefs(refs, ast, word, start, offset, parent, oop, res local value = guide.getObjectValue(src) or src if value.type == 'function' or value.type == 'doc.type.function' then - funcLabel = name .. getParams(value) + funcLabel = name .. getParams(value, oop) fields[funcLabel] = src fields[name] = false count = count + 1 @@ -572,7 +584,7 @@ local function checkCommon(myUri, word, text, offset, results) results.enableCommon = true local used = {} for _, result in ipairs(results) do - used[result.label] = true + used[result.label:match '^[^(]*'] = true end for _, data in ipairs(keyWordMap) do used[data[1]] = true diff --git a/test/completion/init.lua b/test/completion/init.lua index 1344d3af..ebdac49a 100644 --- a/test/completion/init.lua +++ b/test/completion/init.lua @@ -60,6 +60,8 @@ local Cared = { ['deprecated'] = true, } +local IgnoreFunction = false + function TEST(script) return function (expect) files.removeAll() @@ -86,6 +88,16 @@ function TEST(script) end end end + if IgnoreFunction then + for i = #result, 1, -1 do + local item = result[i] + if item.label:find '%(' + and not item.label:find 'function' then + result[i] = result[#result] + result[#result] = nil + end + end + end assert(result) if expect.include then expect.include = nil @@ -217,11 +229,11 @@ ff$ ]] { { - label = 'assert(a, b)', + label = 'ffff(a, b)', kind = define.CompletionItemKind.Function, }, { - label = 'assert(a, b)', + label = 'ffff(a, b)', kind = define.CompletionItemKind.Snippet, } } @@ -285,11 +297,11 @@ mt:g$ ]] { { - label = 'get', + label = 'get(a, b)', kind = define.CompletionItemKind.Method, }, { - label = 'get()', + label = 'get(a, b)', kind = define.CompletionItemKind.Snippet, }, { @@ -311,15 +323,16 @@ loc$ kind = define.CompletionItemKind.Snippet, }, { - label = 'collectgarbage', + label = 'collectgarbage(opt, ...)', kind = define.CompletionItemKind.Function, }, { - label = 'collectgarbage()', + label = 'collectgarbage(opt, ...)', kind = define.CompletionItemKind.Snippet, }, } +IgnoreFunction = true TEST [[ do$ ]] @@ -332,50 +345,6 @@ do$ label = 'do .. end', kind = define.CompletionItemKind.Snippet, }, - { - label = 'dofile', - kind = define.CompletionItemKind.Function, - }, - { - label = 'dofile()', - kind = define.CompletionItemKind.Snippet, - }, - { - label = 'load', - kind = define.CompletionItemKind.Function, - }, - { - label = 'load()', - kind = define.CompletionItemKind.Snippet, - }, - { - label = 'loadfile', - kind = define.CompletionItemKind.Function, - }, - { - label = 'loadfile()', - kind = define.CompletionItemKind.Snippet, - }, - { - label = 'loadstring', - kind = define.CompletionItemKind.Function, - deprecated = true, - }, - { - label = 'loadstring()', - kind = define.CompletionItemKind.Snippet, - deprecated = true, - }, - { - label = 'module', - kind = define.CompletionItemKind.Function, - deprecated = true, - }, - { - label = 'module()', - kind = define.CompletionItemKind.Snippet, - deprecated = true, - }, } TEST [[ @@ -454,6 +423,7 @@ t. $ }, } +IgnoreFunction = false TEST [[ t.a = {} function t:b() @@ -462,7 +432,7 @@ t:$ ]] { { - label = 'b', + label = 'b()', kind = define.CompletionItemKind.Method, }, { @@ -507,6 +477,7 @@ TEST 'local s = "a:$"' (nil) TEST 'debug.$' (EXISTS) +IgnoreFunction = true TEST [[ local xxxx = { xxyy = 1, @@ -530,22 +501,6 @@ local t = { label = 'xxzz', kind = define.CompletionItemKind.Property, }, - { - label = 'next', - kind = define.CompletionItemKind.Function, - }, - { - label = 'next()', - kind = define.CompletionItemKind.Snippet, - }, - { - label = 'xpcall', - kind = define.CompletionItemKind.Function, - }, - { - label = 'xpcall()', - kind = define.CompletionItemKind.Snippet, - }, } TEST [[ @@ -1030,25 +985,10 @@ else$ label = 'ELSE', kind = define.CompletionItemKind.Enum, }, - { - label = 'select', - kind = define.CompletionItemKind.Function, - }, - { - label = 'select()', - kind = define.CompletionItemKind.Snippet, - }, - { - label = 'setmetatable', - kind = define.CompletionItemKind.Function, - }, - { - label = 'setmetatable()', - kind = define.CompletionItemKind.Snippet, - }, } Cared['insertText'] = true +IgnoreFunction = false TEST [[ local xpcal xpcal$ @@ -1059,11 +999,11 @@ xpcal$ kind = define.CompletionItemKind.Variable, }, { - label = 'xpcall', + label = 'xpcall(f, msgh, arg1, ...)', kind = define.CompletionItemKind.Function, }, { - label = 'xpcall()', + label = 'xpcall(f, msgh, arg1, ...)', kind = define.CompletionItemKind.Snippet, insertText = EXISTS, }, @@ -1077,11 +1017,11 @@ mt:f$ ]] { { - label = 'f', + label = 'f(a, b, c)', kind = define.CompletionItemKind.Method, }, { - label = 'f()', + label = 'f(a, b, c)', kind = define.CompletionItemKind.Snippet, insertText = 'f(${1:a: any}, ${2:b: any}, ${3:c: any})', }, @@ -1123,6 +1063,7 @@ end", }, } Cared['insertText'] = false +IgnoreFunction = true TEST [[ local function f() @@ -1143,22 +1084,6 @@ end label = 'elseif .. then', kind = define.CompletionItemKind.Snippet, }, - { - label = 'select', - kind = define.CompletionItemKind.Function, - }, - { - label = 'select()', - kind = define.CompletionItemKind.Snippet, - }, - { - label = 'setmetatable', - kind = define.CompletionItemKind.Function, - }, - { - label = 'setmetatable()', - kind = define.CompletionItemKind.Snippet, - }, } TEST [[ @@ -1259,17 +1184,18 @@ io$ ]] (EXISTS) +IgnoreFunction = false TEST [[ loadstring$ ]] { { - label = 'loadstring', + label = 'loadstring(text, chunkname)', kind = define.CompletionItemKind.Function, deprecated = true, }, { - label = 'loadstring()', + label = 'loadstring(text, chunkname)', kind = define.CompletionItemKind.Snippet, deprecated = true, }, @@ -1293,7 +1219,7 @@ loadstring$ ]] { { - label = 'loadstring', + label = 'loadstring()', kind = define.CompletionItemKind.Function, }, { -- cgit v1.2.3 From 6c0fc0d438154f2164e10674f5c1a4b983382039 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Fri, 7 May 2021 15:28:37 +0800 Subject: stash --- script/core/completion.lua | 2 +- test/completion/init.lua | 20 +++++++++++++++----- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/script/core/completion.lua b/script/core/completion.lua index bab0f452..624ea0a6 100644 --- a/script/core/completion.lua +++ b/script/core/completion.lua @@ -254,7 +254,7 @@ end local function getParams(func, oop) if not func.args then - return '' + return '()' end local args = {} for _, arg in ipairs(func.args) do diff --git a/test/completion/init.lua b/test/completion/init.lua index ebdac49a..ad60b538 100644 --- a/test/completion/init.lua +++ b/test/completion/init.lua @@ -1226,6 +1226,16 @@ loadstring$ label = 'loadstring()', kind = define.CompletionItemKind.Snippet, }, + { + label = 'loadstring(text, chunkname)', + deprecated = true, + kind = define.CompletionItemKind.Function, + }, + { + label = 'loadstring(text, chunkname)', + deprecated = true, + kind = define.CompletionItemKind.Snippet, + }, } TEST [[ @@ -1233,12 +1243,12 @@ debug.setcsta$ ]] { { - label = 'setcstacklimit', + label = 'setcstacklimit(limit)', kind = define.CompletionItemKind.Function, deprecated = true, }, { - label = 'setcstacklimit()', + label = 'setcstacklimit(limit)', kind = define.CompletionItemKind.Snippet, deprecated = true, }, @@ -1777,11 +1787,11 @@ zzz$ ]] { { - label = 'zzzzz', + label = 'zzzzz(list, sep, i, j)', kind = define.CompletionItemKind.Function, }, { - label = 'zzzzz()', + label = 'zzzzz(list, sep, i, j)', kind = define.CompletionItemKind.Snippet, insertText = EXISTS, } @@ -2260,7 +2270,7 @@ end m.f$ ]]{ { - label = "f", + label = "f()", kind = define.CompletionItemKind.Function, }, { -- cgit v1.2.3 From d0f2fff90e56a6e2b1448217174e29fb0d17d05a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Fri, 7 May 2021 16:20:35 +0800 Subject: resolve #478 new setting: `completion.showParams` --- changelog.md | 3 +++ script/core/completion.lua | 8 ++++++++ script/core/guide.lua | 2 ++ test/completion/init.lua | 27 ++++++++++++++++++++++++--- 4 files changed, 37 insertions(+), 3 deletions(-) diff --git a/changelog.md b/changelog.md index 371d77cd..f7a1ae3d 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,8 @@ # changelog +## 1.21.0 +* `NEW` setting: `completion.showParams` + ## 1.20.5 `2021-4-30` * `NEW` setting: `completion.autoRequire` diff --git a/script/core/completion.lua b/script/core/completion.lua index 624ea0a6..03d17a22 100644 --- a/script/core/completion.lua +++ b/script/core/completion.lua @@ -505,6 +505,14 @@ local function checkFieldOfRefs(refs, ast, word, start, offset, parent, oop, res fields[funcLabel] = src fields[name] = false count = count + 1 + if value.type == 'function' and value.bindDocs then + for _, doc in ipairs(value.bindDocs) do + if doc.type == 'doc.overload' then + funcLabel = name .. getParams(doc.overload, oop) + fields[funcLabel] = doc.overload + end + end + end goto CONTINUE end end diff --git a/script/core/guide.lua b/script/core/guide.lua index 367b91e2..e4871060 100644 --- a/script/core/guide.lua +++ b/script/core/guide.lua @@ -1842,6 +1842,8 @@ function m.checkSameSimpleByBindDocs(status, obj, start, pushQueue, mode) if obj.type == '...' then results[#results+1] = doc end + elseif doc.type == 'doc.overload' then + results[#results+1] = doc.overload end end for _, res in ipairs(results) do diff --git a/test/completion/init.lua b/test/completion/init.lua index ad60b538..d5631564 100644 --- a/test/completion/init.lua +++ b/test/completion/init.lua @@ -1773,9 +1773,6 @@ end) TEST [[ --- JustTest ----@overload fun(list:table):string ----@overload fun(list:table, sep:string):string ----@overload fun(list:table, sep:string, i:number):string ---@param list table ---@param sep string ---@param i number @@ -2477,3 +2474,27 @@ TEST [[ }, } } + +TEST [[ +---@overload fun(a: any, b: any) +local function zzzz(a) end +zzzz$ +]] +{ + { + label = 'zzzz(a)', + kind = define.CompletionItemKind.Function, + }, + { + label = 'zzzz(a)', + kind = define.CompletionItemKind.Snippet, + }, + { + label = 'zzzz(a, b)', + kind = define.CompletionItemKind.Function, + }, + { + label = 'zzzz(a, b)', + kind = define.CompletionItemKind.Snippet, + }, +} -- cgit v1.2.3 From 24b83882cfca469a797bf5536f2d5c68762189bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Fri, 7 May 2021 16:25:30 +0800 Subject: fix insertText --- script/core/completion.lua | 2 ++ test/completion/init.lua | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/script/core/completion.lua b/script/core/completion.lua index 03d17a22..60c3e16d 100644 --- a/script/core/completion.lua +++ b/script/core/completion.lua @@ -288,6 +288,7 @@ local function checkLocal(ast, word, offset, results) local funcLabel = name .. getParams(def, false) buildFunction(results, source, false, { label = funcLabel, + insertText = name, kind = define.CompletionItemKind.Function, id = stack(function () return { @@ -436,6 +437,7 @@ local function checkFieldThen(name, src, word, start, offset, parent, oop, resul buildFunction(results, src, oop, { label = name, kind = kind, + insertText = name:match '^[^(]+', deprecated = vm.isDeprecated(src) or nil, id = stack(function () return { diff --git a/test/completion/init.lua b/test/completion/init.lua index d5631564..b2cac99d 100644 --- a/test/completion/init.lua +++ b/test/completion/init.lua @@ -1001,6 +1001,7 @@ xpcal$ { label = 'xpcall(f, msgh, arg1, ...)', kind = define.CompletionItemKind.Function, + insertText = EXISTS, }, { label = 'xpcall(f, msgh, arg1, ...)', @@ -1019,6 +1020,7 @@ mt:f$ { label = 'f(a, b, c)', kind = define.CompletionItemKind.Method, + insertText = EXISTS, }, { label = 'f(a, b, c)', @@ -1786,6 +1788,7 @@ zzz$ { label = 'zzzzz(list, sep, i, j)', kind = define.CompletionItemKind.Function, + insertText = EXISTS, }, { label = 'zzzzz(list, sep, i, j)', @@ -2269,6 +2272,7 @@ m.f$ { label = "f()", kind = define.CompletionItemKind.Function, + insertText = EXISTS, }, { label = "f()", -- cgit v1.2.3 From a8c95b64d7e8a8481cbe961b8b872ef0710d618f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Fri, 7 May 2021 16:37:29 +0800 Subject: fix call snip --- script/core/completion.lua | 20 ++++++-------------- test/completion/init.lua | 5 +++++ 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/script/core/completion.lua b/script/core/completion.lua index 60c3e16d..ee61029d 100644 --- a/script/core/completion.lua +++ b/script/core/completion.lua @@ -121,17 +121,9 @@ local function findParentInStringIndex(ast, text, offset) return parent.node, false end -local function buildFunctionSnip(source, oop) +local function buildFunctionSnip(source, value, oop) local name = getName(source):gsub('^.+[$.:]', '') - local defs = vm.getDefs(source, 0) - local args = '' - for _, def in ipairs(defs) do - local defArgs = getArg(def, oop) - if defArgs ~= '' then - args = defArgs - break - end - end + local args = getArg(value, oop) local id = 0 args = args:gsub('[^,]+', function (arg) id = id + 1 @@ -191,7 +183,7 @@ local function buildDesc(source) return md:string() end -local function buildFunction(results, source, oop, data) +local function buildFunction(results, source, value, oop, data) local snipType = config.config.completion.callSnippet if snipType == 'Disable' or snipType == 'Both' then results[#results+1] = data @@ -199,7 +191,7 @@ local function buildFunction(results, source, oop, data) if snipType == 'Both' or snipType == 'Replace' then local snipData = util.deepCopy(data) snipData.kind = define.CompletionItemKind.Snippet - snipData.insertText = buildFunctionSnip(source, oop) + snipData.insertText = buildFunctionSnip(source, value, oop) snipData.insertTextFormat = 2 snipData.id = stack(function () return { @@ -286,7 +278,7 @@ local function checkLocal(ast, word, offset, results) if def.type == 'function' or def.type == 'doc.type.function' then local funcLabel = name .. getParams(def, false) - buildFunction(results, source, false, { + buildFunction(results, source, def, false, { label = funcLabel, insertText = name, kind = define.CompletionItemKind.Function, @@ -434,7 +426,7 @@ local function checkFieldThen(name, src, word, start, offset, parent, oop, resul else kind = define.CompletionItemKind.Function end - buildFunction(results, src, oop, { + buildFunction(results, src, value, oop, { label = name, kind = kind, insertText = name:match '^[^(]+', diff --git a/test/completion/init.lua b/test/completion/init.lua index b2cac99d..89772853 100644 --- a/test/completion/init.lua +++ b/test/completion/init.lua @@ -2479,6 +2479,7 @@ TEST [[ } } +Cared['insertText'] = true TEST [[ ---@overload fun(a: any, b: any) local function zzzz(a) end @@ -2488,17 +2489,21 @@ zzzz$ { label = 'zzzz(a)', kind = define.CompletionItemKind.Function, + insertText = 'zzzz', }, { label = 'zzzz(a)', kind = define.CompletionItemKind.Snippet, + insertText = 'zzzz(${1:a: any})', }, { label = 'zzzz(a, b)', kind = define.CompletionItemKind.Function, + insertText = 'zzzz', }, { label = 'zzzz(a, b)', kind = define.CompletionItemKind.Snippet, + insertText = 'zzzz(${1:a: any}, ${2:b: any})', }, } -- cgit v1.2.3 From 56e30b07fffee79e6c0612f0f099eb590c7be558 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Fri, 7 May 2021 17:10:37 +0800 Subject: resolve #476 supports multiline comments --- changelog.md | 1 + script/parser/ast.lua | 6 +++--- script/parser/grammar.lua | 6 +++--- test/crossfile/hover.lua | 15 +++++++++++++++ 4 files changed, 22 insertions(+), 6 deletions(-) diff --git a/changelog.md b/changelog.md index f7a1ae3d..3ce84910 100644 --- a/changelog.md +++ b/changelog.md @@ -2,6 +2,7 @@ ## 1.21.0 * `NEW` setting: `completion.showParams` +* `NEW` `LuaDoc`: supports multiline comments ## 1.20.5 `2021-4-30` diff --git a/script/parser/ast.lua b/script/parser/ast.lua index 38c4e51b..3ac4d27f 100644 --- a/script/parser/ast.lua +++ b/script/parser/ast.lua @@ -277,7 +277,7 @@ local Defs = { type = 'comment.long', start = start, finish = finish - 1, - text = '', + text = str, } if not close then local endSymbol = ']' .. ('='):rep(afterEq-beforeEq) .. ']' @@ -318,7 +318,7 @@ local Defs = { } end end, - CLongComment = function (start1, finish1, start2, finish2) + CLongComment = function (start1, finish1, str, start2, finish2) if State.options.nonstandardSymbol and State.options.nonstandardSymbol['/**/'] then else PushError { @@ -344,7 +344,7 @@ local Defs = { type = 'comment.clong', start = start1, finish = finish2 - 1, - text = '', + text = str, } end, CCommentPrefix = function (start, finish, commentFinish) diff --git a/script/parser/grammar.lua b/script/parser/grammar.lua index ea9a25e0..01756c2a 100644 --- a/script/parser/grammar.lua +++ b/script/parser/grammar.lua @@ -88,13 +88,13 @@ end grammar 'Comment' [[ Comment <- LongComment / '--' ShortComment -LongComment <- ({} '--[' {} {:eq: '='* :} {} '[' +LongComment <- ({} '--[' {} {:eq: '='* :} {} '[' %nl? {(!CommentClose .)*} ((CommentClose / %nil) {})) -> LongComment / ( - {} '/*' {} - (!'*/' .)* + {} '/*' {} %nl? + {(!'*/' .)*} {} '*/' {} ) -> CLongComment diff --git a/test/crossfile/hover.lua b/test/crossfile/hover.lua index 521f5a49..dacf1ab2 100644 --- a/test/crossfile/hover.lua +++ b/test/crossfile/hover.lua @@ -731,3 +731,18 @@ hover = { label = 'field Food.firstField: integer = 0', name = 'food.firstField', }} + +TEST {{ path = 'a.lua', content = '', }, { + path = 'b.lua', + content = [[ +--[=[ +I'm a multiline comment +]=] +local +]] +}, +hover = { + label = 'local food: any', + name = 'food', + description = "I'm a multiline comment\n" +}} -- cgit v1.2.3 From 8123dd362c1991ac87a26822729f24a53ef6b35e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Fri, 7 May 2021 17:48:31 +0800 Subject: resolve #455 tail comments support lua string --- changelog.md | 1 + script/parser/luadoc.lua | 18 +++++++++++++----- test/crossfile/hover.lua | 15 +++++++++++++++ 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/changelog.md b/changelog.md index 3ce84910..4f7ac9c2 100644 --- a/changelog.md +++ b/changelog.md @@ -3,6 +3,7 @@ ## 1.21.0 * `NEW` setting: `completion.showParams` * `NEW` `LuaDoc`: supports multiline comments +* `NEW` `LuaDoc`: tail comments support lua string ## 1.20.5 `2021-4-30` diff --git a/script/parser/luadoc.lua b/script/parser/luadoc.lua index 74e53718..3ab9f6a1 100644 --- a/script/parser/luadoc.lua +++ b/script/parser/luadoc.lua @@ -2,6 +2,7 @@ local m = require 'lpeglabel' local re = require 'parser.relabel' local lines = require 'parser.lines' local guide = require 'core.guide' +local grammar = require 'parser.grammar' local TokenTypes, TokenStarts, TokenFinishs, TokenContents local Ci, Offset, pushError, Ct, NextComment, Lines @@ -999,16 +1000,23 @@ local function convertTokens() end local function trimTailComment(text) + local comment = text if text:sub(1, 1) == '@' then - return text:sub(2) + comment = text:sub(2) end if text:sub(1, 1) == '#' then - return text:sub(2) + comment = text:sub(2) end if text:sub(1, 2) == '--' then - return text:sub(3) + comment = text:sub(3) end - return text + if comment:find '^%s*[\'"[]' then + local result = grammar(nil, comment:gsub('^%s+', ''), 'string') + if result then + comment = result[1][1] + end + end + return comment end local function buildLuaDoc(comment) @@ -1185,7 +1193,7 @@ local function bindDoc(sources, lns, binded) end bindGeneric(binded) local row = guide.positionOf(lns, lastDoc.finish) - local cstart, cfinish = guide.lineRange(lns, row) + local cstart, cfinish = guide.lineRange(lns, row) local nstart, nfinish = guide.lineRange(lns, row + 1) bindDocsBetween(sources, binded, bindSources, cstart, cfinish) if #bindSources == 0 then diff --git a/test/crossfile/hover.lua b/test/crossfile/hover.lua index dacf1ab2..bf56b39a 100644 --- a/test/crossfile/hover.lua +++ b/test/crossfile/hover.lua @@ -746,3 +746,18 @@ hover = { name = 'food', description = "I'm a multiline comment\n" }} + +TEST {{ path = 'a.lua', content = '', }, { + path = 'b.lua', + content = [[ +---@return string # 'this is a tab `\t`' +local function () end +]] +}, +hover = { + label = [[ +function f() + -> string]], + name = 'food', + description = "@*return* — this is a tab `\t`" +}} -- cgit v1.2.3