diff options
-rw-r--r-- | changelog.md | 5 | ||||
-rw-r--r-- | script/core/hover/label.lua | 3 | ||||
-rw-r--r-- | script/vm/infer.lua | 40 | ||||
-rw-r--r-- | test/type_inference/init.lua | 11 |
4 files changed, 51 insertions, 8 deletions
diff --git a/changelog.md b/changelog.md index 14cf621e..33ded285 100644 --- a/changelog.md +++ b/changelog.md @@ -1,8 +1,9 @@ # changelog ## 3.5.0 -* `CHG` `---@diagnostic disable: <ERR_NAME>` can suppress syntax errors -* `CHG` `completion.callSnippet` no longer generate parameter types +* `CHG` diagnostic: `---@diagnostic disable: <ERR_NAME>` can suppress syntax errors +* `CHG` completion: `completion.callSnippet` no longer generate parameter types +* `CHG` hover: show `---@type {x: number, y: number}` as detail instead of `table` * `FIX` [#1278](https://github.com/sumneko/lua-language-server/issues/1278) ## 3.4.1 diff --git a/script/core/hover/label.lua b/script/core/hover/label.lua index a91ca074..5befe84c 100644 --- a/script/core/hover/label.lua +++ b/script/core/hover/label.lua @@ -55,7 +55,8 @@ local function asValue(source, title) and ( type == 'table' or type == 'any' or type == 'unknown' - or type == 'nil') then + or type == 'nil' + or type:sub(1, 1) == '{') then else pack[#pack+1] = type end diff --git a/script/vm/infer.lua b/script/vm/infer.lua index d30ba36c..b1b7183e 100644 --- a/script/vm/infer.lua +++ b/script/vm/infer.lua @@ -8,6 +8,7 @@ local vm = require 'vm.vm' ---@field views table<string, boolean> ---@field cachedView? string ---@field node? vm.node +---@field _drop table local mt = {} mt.__index = mt mt._hasNumber = false @@ -122,11 +123,45 @@ local viewNodeSwitch = util.switch() for i, sign in ipairs(source.signs) do buf[i] = vm.getInfer(sign):view(uri) end + if infer._drop then + local node = vm.compileNode(source) + for c in node:eachObject() do + if guide.isLiteral(c) then + infer._drop[c] = true + end + end + end return ('%s<%s>'):format(source.node[1], table.concat(buf, ', ')) end) : case 'doc.type.table' - : call(function (source, infer) - infer._hasTable = true + : call(function (source, infer, uri) + if #source.fields == 0 then + infer._hasTable = true + return + end + if infer._drop and infer._drop[source] then + infer._hasTable = true + return + end + infer._hasClass = true + local buf = {} + buf[#buf+1] = '{ ' + for i, field in ipairs(source.fields) do + if i > 1 then + buf[#buf+1] = ', ' + end + local key = field.name + if key.type == 'doc.type' then + buf[#buf+1] = ('[%s]: '):format(vm.getInfer(key):view(uri)) + elseif type(key[1]) == 'string' then + buf[#buf+1] = key[1] .. ': ' + else + buf[#buf+1] = ('[%q]: '):format(key[1]) + end + buf[#buf+1] = vm.getInfer(field.extends):view(uri) + end + buf[#buf+1] = ' }' + return table.concat(buf) end) : case 'doc.type.string' : call(function (source, infer) @@ -209,6 +244,7 @@ function vm.getInfer(source) end local infer = setmetatable({ node = node, + _drop = {}, }, mt) node.lastInfer = infer diff --git a/test/type_inference/init.lua b/test/type_inference/init.lua index 94ee5638..8e9e134f 100644 --- a/test/type_inference/init.lua +++ b/test/type_inference/init.lua @@ -520,7 +520,7 @@ TEST 'fun(a: string, b: any, c?: boolean, ...any):c, d?, ...unknown' [[ local <?x?> ]] -TEST 'table' [[ +TEST '{ [string]: string }' [[ ---@type { [string]: string } local <?x?> ]] @@ -2439,14 +2439,14 @@ TEST 'table' [[ local <?x?> ]] -TEST 'table' [[ +TEST '{ name: boolean }' [[ ---@alias tp {name: boolean} ---@type tp local <?x?> ]] -TEST 'boolean|table' [[ +TEST 'boolean|{ name: boolean }' [[ ---@alias tp boolean | {name: boolean} ---@type tp @@ -3376,3 +3376,8 @@ TEST 'A' [[ ---@diagnostic disable local <?t?> ]] + +TEST '{ [string]: number, [true]: string, [1]: boolean, tag: integer }' [[ +---@type {[string]: number, [true]: string, [1]: boolean, tag: integer} +local <?t?> +]] |