diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2021-06-23 17:50:17 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2021-06-23 17:50:17 +0800 |
commit | 146a7c38ecad0918f5e4d1a132701064d0118260 (patch) | |
tree | e72e4989716a5476719bd84d3f5c9991ba5a6c06 | |
parent | f9157be94e64051e319d0f44e00995376fdb7598 (diff) | |
download | lua-language-server-146a7c38ecad0918f5e4d1a132701064d0118260.zip |
resolve #560
-rw-r--r-- | changelog.md | 8 | ||||
-rw-r--r-- | script/core/hover/init.lua | 13 | ||||
-rw-r--r-- | script/core/infer.lua | 9 | ||||
-rw-r--r-- | test/crossfile/hover.lua | 12 | ||||
-rw-r--r-- | test/hover/init.lua | 13 | ||||
-rw-r--r-- | test/type_inference/init.lua | 9 |
6 files changed, 51 insertions, 13 deletions
diff --git a/changelog.md b/changelog.md index 404239c9..7e911e25 100644 --- a/changelog.md +++ b/changelog.md @@ -1,6 +1,14 @@ # changelog ## 2.0.2 +* `CHG` view `local f ---@type fun(x:number):boolean` + ```lua + ---before + function f(x: number) + -> boolean + ---after + local f: fun(x: number): boolean + ``` * `FIX` [#558](https://github.com/sumneko/lua-language-server/issues/558) * `FIX` [#567](https://github.com/sumneko/lua-language-server/issues/567) * `FIX` [#568](https://github.com/sumneko/lua-language-server/issues/568) diff --git a/script/core/hover/init.lua b/script/core/hover/init.lua index 41616bc9..06c84c86 100644 --- a/script/core/hover/init.lua +++ b/script/core/hover/init.lua @@ -120,12 +120,21 @@ local function getHoverAsDocName(source) } end +local function isFunction(source) + local defs = vm.getAllDefs(source) + for _, def in ipairs(defs) do + if def.type == 'function' then + return true + end + end + return false +end + local function getHover(source) if source.type == 'doc.type.name' then return getHoverAsDocName(source) end - local isFunction = infer.hasType(source, 'function') - if isFunction then + if isFunction(source) then return getHoverAsFunction(source) else return getHoverAsValue(source) diff --git a/script/core/infer.lua b/script/core/infer.lua index d3145a2e..1111cb55 100644 --- a/script/core/infer.lua +++ b/script/core/infer.lua @@ -261,6 +261,13 @@ local function cleanInfers(infers) infers[BE_RETURN] = nil infers['nil'] = nil end + if infers['function'] then + for k in pairs(infers) do + if k:sub(1, 4) == 'fun(' then + infers[k] = nil + end + end + end infers['any'] = nil end @@ -365,7 +372,7 @@ function m.getDocName(doc) return ('table<%s, %s>'):format(key, value) end if doc.type == 'doc.type.function' then - return 'function' + return m.viewDocFunction(doc) end if doc.type == 'doc.type.enum' or doc.type == 'doc.resume' then diff --git a/test/crossfile/hover.lua b/test/crossfile/hover.lua index 54d8d93c..aaf69e7a 100644 --- a/test/crossfile/hover.lua +++ b/test/crossfile/hover.lua @@ -850,3 +850,15 @@ hover = { name = 'bthci.rawhci', description = " Sends a raw HCI command to the BlueTooth controller." }} + +TEST {{ path = 'a.lua', content = '', }, { + path = 'b.lua', + content = [[ +---@type string | fun(): string +local <?t?> +]] +}, +hover = { + label = 'local t: string|fun():string', + name = 't', +}} diff --git a/test/hover/init.lua b/test/hover/init.lua index a7af112d..13cc87e0 100644 --- a/test/hover/init.lua +++ b/test/hover/init.lua @@ -1196,8 +1196,7 @@ TEST [[ local <?f?> ]] [[ -function f(x: number, y: number) - -> boolean +local f: fun(x: number, y: number):boolean ]] TEST [[ @@ -1216,8 +1215,7 @@ TEST [[ function t(<?f?>) end ]] [[ -function () - -> void +local f: fun():void ]] TEST [[ @@ -1227,7 +1225,7 @@ local t = {f = f} t:<?f?>() ]] [[ -function f(a: any, b: any) +field t:f: fun(a: any, b: any) ]] TEST [[ @@ -1286,8 +1284,7 @@ TEST [[ local <?f?> ]] [[ -function f(x?: boolean) - -> boolean? +local f: fun(x: boolean):boolean ]] TEST [[ @@ -1445,7 +1442,7 @@ TEST [[ local function f(<?callback?>) end ]] [[ -function (x: integer, ...) +local callback: fun(x: integer, ...: nil) ]] TEST [[ diff --git a/test/type_inference/init.lua b/test/type_inference/init.lua index 3ca46cda..c23d12f5 100644 --- a/test/type_inference/init.lua +++ b/test/type_inference/init.lua @@ -343,7 +343,7 @@ TEST '"enum1"|"enum2"' [[ local <?x?> ]] -TEST 'function' [[ +TEST 'fun()' [[ ---@type fun() local <?x?> ]] @@ -508,7 +508,7 @@ local t local k, <?v?> = f(t) ]] -TEST 'function' [[ +TEST 'fun()' [[ ---@return fun() local function f() end @@ -767,3 +767,8 @@ local <?t?> ]] config.config.hover.enumsLimit = 5 + +TEST 'string|fun():string' [[ +---@type string | fun(): string +local <?t?> +]] |