diff options
-rw-r--r-- | script/core/hover/arg.lua | 10 | ||||
-rw-r--r-- | script/core/hover/init.lua | 20 | ||||
-rw-r--r-- | script/core/hover/label.lua | 22 | ||||
-rw-r--r-- | script/core/hover/name.lua | 6 | ||||
-rw-r--r-- | script/parser/guide.lua | 2 | ||||
-rw-r--r-- | script/parser/luadoc.lua | 22 | ||||
-rw-r--r-- | test/crossfile/hover.lua | 61 |
7 files changed, 103 insertions, 40 deletions
diff --git a/script/core/hover/arg.lua b/script/core/hover/arg.lua index 4e6a1ace..d03f55f2 100644 --- a/script/core/hover/arg.lua +++ b/script/core/hover/arg.lua @@ -55,7 +55,7 @@ local function asFunction(source, oop) end end -local function asDocFunction(source) +local function asDocFunction(source, oop) if not source.args then return '' end @@ -69,7 +69,11 @@ local function asDocFunction(source) arg.extends and infer.searchAndViewInfers(arg.extends) or 'any' ) end - return table.concat(args, ', ') + if oop then + return table.concat(args, ', ', 2) + else + return table.concat(args, ', ') + end end return function (source, oop) @@ -77,7 +81,7 @@ return function (source, oop) return asFunction(source, oop) end if source.type == 'doc.type.function' then - return asDocFunction(source) + return asDocFunction(source, oop) end return '' end diff --git a/script/core/hover/init.lua b/script/core/hover/init.lua index 7d99a006..baa24139 100644 --- a/script/core/hover/init.lua +++ b/script/core/hover/init.lua @@ -6,6 +6,7 @@ local util = require 'utility' local findSource = require 'core.find-source' local markdown = require 'provider.markdown' local infer = require 'core.infer' +local guide = require 'parser.guide' ---@async local function getHover(source) @@ -15,14 +16,14 @@ local function getHover(source) local descMark = {} ---@async - local function addHover(def, checkLable) + local function addHover(def, checkLable, oop) if defMark[def] then return end defMark[def] = true if checkLable then - local label = getLabel(def) + local label = getLabel(def, oop) if not labelMark[tostring(label)] then labelMark[tostring(label)] = true md:add('lua', label) @@ -38,27 +39,34 @@ local function getHover(source) end end + local oop if infer.searchAndViewInfers(source) == 'function' then local hasFunc for _, def in ipairs(vm.getDefs(source)) do + if guide.isOOP(def) then + oop = true + end if def.type == 'function' or def.type == 'doc.type.function' then hasFunc = true - addHover(def, true) + addHover(def, true, oop) end end if not hasFunc then - addHover(source, true) + addHover(source, true, oop) end else - addHover(source, true) + addHover(source, true, oop) for _, def in ipairs(vm.getDefs(source)) do + if guide.isOOP(def) then + oop = true + end local isFunction if def.type == 'function' or def.type == 'doc.type.function' then isFunction = true end - addHover(def, isFunction) + addHover(def, isFunction, oop) end end diff --git a/script/core/hover/label.lua b/script/core/hover/label.lua index 8531ebb9..78756ea8 100644 --- a/script/core/hover/label.lua +++ b/script/core/hover/label.lua @@ -11,9 +11,6 @@ local files = require 'files' local guide = require 'parser.guide' local function asFunction(source, oop) - if oop == nil then - oop = guide.isOOP(source, oop) - end local name = buildName(source, oop) local arg = buildArg(source, oop) local rtn = buildReturn(source) @@ -28,20 +25,6 @@ local function asFunction(source, oop) return table.concat(lines, '\n') end -local function asDocFunction(source, oop) - local name = buildName(source, oop) - local arg = buildArg(source, oop) - local rtn = buildReturn(source) - local lines = {} - lines[1] = string.format('%s%s %s(%s)' - , '' - , oop and 'method' or 'function' - , name or '' - , arg) - lines[2] = rtn - return table.concat(lines, '\n') -end - local function asDocTypeName(source) local defs = vm.getDefs(source) for _, doc in ipairs(defs) do @@ -195,7 +178,8 @@ end ---@async return function (source, oop) - if source.type == 'function' then + if source.type == 'function' + or source.type == 'doc.type.function' then return asFunction(source, oop) elseif source.type == 'local' or source.type == 'getlocal' @@ -217,8 +201,6 @@ return function (source, oop) elseif source.type == 'number' or source.type == 'integer' then return asNumber(source) - elseif source.type == 'doc.type.function' then - return asDocFunction(source, oop) elseif source.type == 'doc.type.name' then return asDocTypeName(source) elseif source.type == 'doc.field.name' then diff --git a/script/core/hover/name.lua b/script/core/hover/name.lua index 2d1e361c..5d8f0b3d 100644 --- a/script/core/hover/name.lua +++ b/script/core/hover/name.lua @@ -46,14 +46,14 @@ local function asGlobal(source) return guide.getKeyName(source) end -local function asDocFunction(source) +local function asDocFunction(source, oop) local doc = guide.getParentType(source, 'doc.type') or guide.getParentType(source, 'doc.overload') if not doc or not doc.bindSources then return '' end for _, src in ipairs(doc.bindSources) do - local name = buildName(src) + local name = buildName(src, oop) if name ~= '' then return name end @@ -89,7 +89,7 @@ function buildName(source, oop) return asTableField(source) or '', oop end if source.type == 'doc.type.function' then - return asDocFunction(source), oop + return asDocFunction(source, oop), oop end if source.type == 'doc.field' then return asDocField(source), oop diff --git a/script/parser/guide.lua b/script/parser/guide.lua index ac773d44..d55ba099 100644 --- a/script/parser/guide.lua +++ b/script/parser/guide.lua @@ -1160,7 +1160,7 @@ function m.isInString(ast, position) end) end -function m.isOOP(source, oop) +function m.isOOP(source) if source.type == 'setmethod' or source.type == 'getmethod' then return true diff --git a/script/parser/luadoc.lua b/script/parser/luadoc.lua index bfadcf8a..eef1d3bf 100644 --- a/script/parser/luadoc.lua +++ b/script/parser/luadoc.lua @@ -480,7 +480,9 @@ local function parseTypeUnitLiteralTable() return typeUnit end -local function parseTypeUnit(parent, content) +local parseTypeUnit + +local function parseDocFunction(parent, content) if content == 'async' then local tp, cont = peekToken() if tp == 'name' then @@ -494,12 +496,17 @@ local function parseTypeUnit(parent, content) end end end - local result if content == 'fun' then - result = parseTypeUnitFunction() + return parseTypeUnitFunction() end - if content == '{' then - result = parseTypeUnitLiteralTable() +end + +function parseTypeUnit(parent, content) + local result = parseDocFunction(parent, content) + if not result then + if content == '{' then + result = parseTypeUnitLiteralTable() + end end if not result then result = { @@ -916,7 +923,8 @@ end local function parseOverload() local tp, name = peekToken() - if tp ~= 'name' or name ~= 'fun' then + if tp ~= 'name' + or (name ~= 'fun' and name ~= 'async') then pushError { type = 'LUADOC_MISS_FUN_AFTER_OVERLOAD', start = getFinish(), @@ -928,7 +936,7 @@ local function parseOverload() local result = { type = 'doc.overload', } - result.overload = parseTypeUnitFunction() + result.overload = parseDocFunction(result, name) if not result.overload then return nil end diff --git a/test/crossfile/hover.lua b/test/crossfile/hover.lua index 82af9c32..492efe43 100644 --- a/test/crossfile/hover.lua +++ b/test/crossfile/hover.lua @@ -1072,3 +1072,64 @@ global G: A { } ```]] } + +TEST { + { + path = 'a.lua', + content = [[ + ---@overload fun(self, a) + function C:<?f?>(a, b) end + ]] + }, + hover = [[ +```lua +method C:f(a: any, b: any) +``` + +--- + +```lua +method C:f(a: any) +```]] +} + +TEST { + { + path = 'a.lua', + content = [[ + ---@overload fun(self, a) + function C.<?f?>(a, b) end + ]] + }, + hover = [[ +```lua +function C.f(a: any, b: any) +``` + +--- + +```lua +function C.f(self: any, a: any) +```]] +} + +TEST { + { + path = 'a.lua', + content = [[ + ---@async + ---@overload async fun(self, a) + function C:<?f?>(a, b) end + ]] + }, + hover = [[ +```lua +async method C:f(a: any, b: any) +``` + +--- + +```lua +async method C:f(a: any) +```]] +} |