diff options
-rw-r--r-- | script/config/config.lua | 1 | ||||
-rw-r--r-- | script/parser/luadoc.lua | 10 | ||||
-rw-r--r-- | script/vm/compiler.lua | 4 | ||||
-rw-r--r-- | script/vm/infer.lua | 25 | ||||
-rw-r--r-- | test/type_inference/init.lua | 34 |
5 files changed, 67 insertions, 7 deletions
diff --git a/script/config/config.lua b/script/config/config.lua index 8c06278f..6be80cc9 100644 --- a/script/config/config.lua +++ b/script/config/config.lua @@ -196,6 +196,7 @@ local Template = { ['Lua.hover.viewNumber'] = Type.Boolean >> true, ['Lua.hover.previewFields'] = Type.Integer >> 20, ['Lua.hover.enumsLimit'] = Type.Integer >> 5, + ['Lua.hover.expandAlias'] = Type.Boolean >> true, ['Lua.semantic.enable'] = Type.Boolean >> true, ['Lua.semantic.variable'] = Type.Boolean >> true, ['Lua.semantic.annotation'] = Type.Boolean >> true, diff --git a/script/parser/luadoc.lua b/script/parser/luadoc.lua index 810faa8c..ebbc8bcb 100644 --- a/script/parser/luadoc.lua +++ b/script/parser/luadoc.lua @@ -678,6 +678,16 @@ function parseType(parent) if not result.start then result.start = vararg.start end + elseif tp == 'integer' then + nextToken() + local integer = { + type = 'doc.type.integer', + start = getStart(), + finish = getFinish(), + parent = result, + [1] = content, + } + result.types[#result.types+1] = integer end if not checkToken('symbol', '|', 1) then break diff --git a/script/vm/compiler.lua b/script/vm/compiler.lua index f0735ad3..390387b4 100644 --- a/script/vm/compiler.lua +++ b/script/vm/compiler.lua @@ -548,6 +548,10 @@ local compilerMap = util.switch() nodeMgr.setNode(source, m.compileNode(typeUnit)) end end) + : case 'doc.type.integer' + : call(function (source) + nodeMgr.setNode(source, source) + end) : case 'doc.type.enum' : call(function (source) nodeMgr.setNode(source, source) diff --git a/script/vm/infer.lua b/script/vm/infer.lua index c396f3ec..80558eaf 100644 --- a/script/vm/infer.lua +++ b/script/vm/infer.lua @@ -1,5 +1,7 @@ local util = require 'utility' local nodeMgr = require 'vm.node' +local config = require 'config' +local guide = require 'parser.guide' ---@class vm.infer-manager local m = {} @@ -33,6 +35,10 @@ local viewNodeMap = util.switch() return source.name end end) + : case 'doc.type.integer' + : call(function (source, options) + return ('%d'):format(source[1]) + end) : case 'doc.type.name' : call(function (source, options) return source[1] @@ -56,15 +62,19 @@ local function viewNode(node, options) end end -local function eraseAlias(node, viewMap) +local function eraseAlias(node, viewMap, options) for n in nodeMgr.eachNode(node) do if n.type == 'global' and n.cate == 'type' then for _, set in ipairs(n:getSets()) do if set.type == 'doc.alias' then - for _, ext in ipairs(set.extends.types) do - local view = viewNode(ext, {}) - if view and view ~= n.name then - viewMap[view] = nil + if options['expandAlias'] then + viewMap[n.name] = nil + else + for _, ext in ipairs(set.extends.types) do + local view = viewNode(ext, {}) + if view and view ~= n.name then + viewMap[view] = nil + end end end end @@ -81,8 +91,9 @@ function m.getViews(source) if node.type == 'union' and node.lastViews then return node.lastViews end - local views = {} + local views = {} local options = {} + options['expandAlias'] = config.get(guide.getUri(source), 'Lua.hover.expandAlias') if node.type == 'union' then node.lastViews = views end @@ -99,7 +110,7 @@ function m.getViews(source) views['table'] = true end if options['hasClass'] then - eraseAlias(node, views) + eraseAlias(node, views, options) end return views end diff --git a/test/type_inference/init.lua b/test/type_inference/init.lua index 37aa24b0..dead9fab 100644 --- a/test/type_inference/init.lua +++ b/test/type_inference/init.lua @@ -365,6 +365,11 @@ TEST 'string' [[ local <?x?> ]] +TEST '1' [[ +---@type 1 +local <?v?> +]] + TEST 'string[]' [[ ---@class string @@ -390,6 +395,7 @@ TEST '"enum1"|"enum2"' [[ local <?x?> ]] +config.set(nil, 'Lua.hover.expandAlias', false) TEST 'A' [[ ---@alias A 'enum1' | 'enum2' @@ -410,6 +416,34 @@ TEST 'A' [[ ---@type A local <?x?> ]] +config.set(nil, 'Lua.hover.expandAlias', true) +TEST '"enum1"|"enum2"' [[ +---@alias A 'enum1' | 'enum2' + +---@type A +local <?x?> +]] + +TEST '"enum1"|"enum2"' [[ +---@alias A 'enum1' | 'enum2' | A + +---@type A +local <?x?> +]] + +TEST '"enum1"|"enum2"|B' [[ +---@alias A 'enum1' | 'enum2' | B + +---@type A +local <?x?> +]] + +TEST '1|true' [[ +---@alias A 1 | true + +---@type A +local <?x?> +]] TEST 'fun()' [[ ---@type fun() |