From 3771e70b48e9a088cbdc11a8b952d525eea1e7c9 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, 8 Apr 2022 13:12:31 +0800 Subject: cleanup --- script/core/hover/label.lua | 5 +--- script/core/hover/table.lua | 7 +++++ script/gc.lua | 1 + script/vm/getLibrary.lua | 36 -------------------------- script/vm/getLinks.lua | 63 --------------------------------------------- script/vm/infer.lua | 5 ++++ script/vm/init.lua | 4 +-- script/vm/library.lua | 36 ++++++++++++++++++++++++++ script/vm/sign.lua | 11 ++++---- script/vm/type.lua | 45 ++++++++++++++++++++------------ test/hover/init.lua | 22 ++++++++++++++++ 11 files changed, 108 insertions(+), 127 deletions(-) delete mode 100644 script/vm/getLibrary.lua delete mode 100644 script/vm/getLinks.lua create mode 100644 script/vm/library.lua diff --git a/script/core/hover/label.lua b/script/core/hover/label.lua index 995c3294..c2239344 100644 --- a/script/core/hover/label.lua +++ b/script/core/hover/label.lua @@ -45,10 +45,7 @@ local function asValue(source, title) local ifr = infer.getInfer(source) local type = ifr:view() local literal = ifr:viewLiterals() - local cont - if not ifr:hasType 'string' then - cont = buildTable(source) - end + local cont = buildTable(source) local pack = {} pack[#pack+1] = title pack[#pack+1] = name .. ':' diff --git a/script/core/hover/table.lua b/script/core/hover/table.lua index 88808daa..8f9beefe 100644 --- a/script/core/hover/table.lua +++ b/script/core/hover/table.lua @@ -163,6 +163,13 @@ return function (source) return nil end + for view in infer.getInfer(source):eachView() do + if view == 'string' + or vm.isSubType(view, 'string') then + return nil + end + end + local fields = vm.getFields(source) local keys, map = getKeyMap(fields) if #keys == 0 then diff --git a/script/gc.lua b/script/gc.lua index 24a72f15..7bb81569 100644 --- a/script/gc.lua +++ b/script/gc.lua @@ -5,6 +5,7 @@ local util = require 'utility' local mt = {} mt.__index = mt mt.type = 'gc' +mt._removed = false mt._max = 10 diff --git a/script/vm/getLibrary.lua b/script/vm/getLibrary.lua deleted file mode 100644 index 1a8d8ffd..00000000 --- a/script/vm/getLibrary.lua +++ /dev/null @@ -1,36 +0,0 @@ ----@class vm -local vm = require 'vm.vm' - -function vm.getLibraryName(source) - if source.special then - return source.special - end - local defs = vm.getDefs(source) - for _, def in ipairs(defs) do - if def.special then - return def.special - end - end - return nil -end - -local globalLibraryNames = { - 'arg', 'assert', 'error', 'collectgarbage', 'dofile', '_G', 'getfenv', - 'getmetatable', 'ipairs', 'load', 'loadfile', 'loadstring', - 'module', 'next', 'pairs', 'pcall', 'print', 'rawequal', - 'rawget', 'rawlen', 'rawset', 'select', 'setfenv', - 'setmetatable', 'tonumber', 'tostring', 'type', '_VERSION', - 'warn', 'xpcall', 'require', 'unpack', 'bit32', 'coroutine', - 'debug', 'io', 'math', 'os', 'package', 'string', 'table', - 'utf8', -} -local globalLibraryNamesMap -function vm.isGlobalLibraryName(name) - if not globalLibraryNamesMap then - globalLibraryNamesMap = {} - for _, v in ipairs(globalLibraryNames) do - globalLibraryNamesMap[v] = true - end - end - return globalLibraryNamesMap[name] or false -end diff --git a/script/vm/getLinks.lua b/script/vm/getLinks.lua deleted file mode 100644 index 8571fb46..00000000 --- a/script/vm/getLinks.lua +++ /dev/null @@ -1,63 +0,0 @@ -local guide = require 'parser.guide' ----@class vm -local vm = require 'vm.vm' -local files = require 'files' -local rpath = require 'workspace.require-path' - -local function getFileLinks(uri) - local links = {} - local state = files.getState(uri) - if not state then - return links - end - tracy.ZoneBeginN('getFileLinks') - guide.eachSpecialOf(state.ast, 'require', function (source) - local call = source.parent - if not call or call.type ~= 'call' then - return - end - local args = call.args - if not args or not args[1] or args[1].type ~= 'string' then - return - end - local uris = rpath.findUrisByRequirePath(uri, args[1][1]) - for _, u in ipairs(uris) do - if not links[u] then - links[u] = {} - end - links[u][#links[u]+1] = call - end - end) - tracy.ZoneEnd() - return links -end - -local function getFileLinksOrCache(uri) - local cache = files.getCache(uri) - cache.links = cache.links or getFileLinks(uri) - return cache.links -end - -local function getLinksTo(uri) - local links = {} - for u in files.eachFile(uri) do - local ls = getFileLinksOrCache(u) - if ls[uri] then - for _, l in ipairs(ls[uri]) do - links[#links+1] = l - end - end - end - return links -end - --- 获取所有 require(uri) 的文件 -function vm.getLinksTo(uri) - local cache = vm.getCache('getLinksTo')[uri] - if cache ~= nil then - return cache - end - cache = getLinksTo(uri) - vm.getCache('getLinksTo')[uri] = cache - return cache -end diff --git a/script/vm/infer.lua b/script/vm/infer.lua index 853bd7de..1ccc54aa 100644 --- a/script/vm/infer.lua +++ b/script/vm/infer.lua @@ -309,6 +309,11 @@ function mt:view(default, uri) end end +function mt:eachView() + self:_computeViews() + return next, self.views +end + ---@param other vm.infer ---@return vm.infer function mt:merge(other) diff --git a/script/vm/init.lua b/script/vm/init.lua index aa1b908f..4f527930 100644 --- a/script/vm/init.lua +++ b/script/vm/init.lua @@ -4,6 +4,6 @@ require 'vm.def' require 'vm.ref' require 'vm.field' require 'vm.doc' -require 'vm.getLibrary' -require 'vm.getLinks' +require 'vm.type' +require 'vm.library' return vm diff --git a/script/vm/library.lua b/script/vm/library.lua new file mode 100644 index 00000000..1a8d8ffd --- /dev/null +++ b/script/vm/library.lua @@ -0,0 +1,36 @@ +---@class vm +local vm = require 'vm.vm' + +function vm.getLibraryName(source) + if source.special then + return source.special + end + local defs = vm.getDefs(source) + for _, def in ipairs(defs) do + if def.special then + return def.special + end + end + return nil +end + +local globalLibraryNames = { + 'arg', 'assert', 'error', 'collectgarbage', 'dofile', '_G', 'getfenv', + 'getmetatable', 'ipairs', 'load', 'loadfile', 'loadstring', + 'module', 'next', 'pairs', 'pcall', 'print', 'rawequal', + 'rawget', 'rawlen', 'rawset', 'select', 'setfenv', + 'setmetatable', 'tonumber', 'tostring', 'type', '_VERSION', + 'warn', 'xpcall', 'require', 'unpack', 'bit32', 'coroutine', + 'debug', 'io', 'math', 'os', 'package', 'string', 'table', + 'utf8', +} +local globalLibraryNamesMap +function vm.isGlobalLibraryName(name) + if not globalLibraryNamesMap then + globalLibraryNamesMap = {} + for _, v in ipairs(globalLibraryNames) do + globalLibraryNamesMap[v] = true + end + end + return globalLibraryNamesMap[name] or false +end diff --git a/script/vm/sign.lua b/script/vm/sign.lua index 2c440902..eae549e4 100644 --- a/script/vm/sign.lua +++ b/script/vm/sign.lua @@ -1,5 +1,7 @@ local guide = require 'parser.guide' local nodeMgr = require 'vm.node' +---@class vm +local vm = require 'vm.vm' ---@class vm.sign ---@field parent parser.object @@ -19,7 +21,6 @@ function mt:resolve(argNodes) if not argNodes then return nil end - local typeMgr = require 'vm.type' local compiler = require 'vm.compiler' local globalMgr = require 'vm.global-manager' local resolved = {} @@ -56,18 +57,18 @@ function mt:resolve(argNodes) local uvalueNode = compiler.compileNode(ufield.extends) if ufieldNode.type == 'doc.generic.name' and uvalueNode.type == 'doc.generic.name' then -- { [number]: number} -> { [K]: V } - local tfieldNode = typeMgr.getTableKey(node, 'any') - local tvalueNode = typeMgr.getTableValue(node, 'any') + local tfieldNode = vm.getTableKey(node, 'any') + local tvalueNode = vm.getTableValue(node, 'any') resolve(ufieldNode, tfieldNode) resolve(uvalueNode, tvalueNode) else if ufieldNode.type == 'doc.generic.name' then -- { [number]: number}|number[] -> { [K]: number } - local tnode = typeMgr.getTableKey(node, uvalueNode) + local tnode = vm.getTableKey(node, uvalueNode) resolve(ufieldNode, tnode) else -- { [number]: number}|number[] -> { [number]: V } - local tnode = typeMgr.getTableValue(node, ufieldNode) + local tnode = vm.getTableValue(node, ufieldNode) resolve(uvalueNode, tnode) end end diff --git a/script/vm/type.lua b/script/vm/type.lua index 8b80d4f3..4cac9723 100644 --- a/script/vm/type.lua +++ b/script/vm/type.lua @@ -1,14 +1,14 @@ local nodeMgr = require 'vm.node' local compiler = require 'vm.compiler' local globalMgr = require 'vm.global-manager' +---@class vm +local vm = require 'vm.vm' ----@class vm.type-manager -local m = {} - ----@param child vm.node ----@param parent vm.node +---@param child vm.node|string +---@param parent vm.node|string +---@param mark? table ---@return boolean -function m.isSubType(child, parent, mark) +function vm.isSubType(child, parent, mark) if type(parent) == 'string' then parent = globalMgr.getGlobal('type', parent) end @@ -16,13 +16,17 @@ function m.isSubType(child, parent, mark) child = globalMgr.getGlobal('type', child) end + if not child or not parent then + return false + end + if parent.type == 'global' and parent.cate == 'type' and parent.name == 'any' then return true end if child.type == 'doc.type' then for _, typeUnit in ipairs(child.types) do - if not m.isSubType(typeUnit, parent) then + if not vm.isSubType(typeUnit, parent) then return false end end @@ -36,7 +40,7 @@ function m.isSubType(child, parent, mark) if child.type == 'global' and child.cate == 'type' then if parent.type == 'doc.type' then for _, typeUnit in ipairs(parent.types) do - if m.isSubType(child, typeUnit) then + if vm.isSubType(child, typeUnit) then return true end end @@ -58,7 +62,16 @@ function m.isSubType(child, parent, mark) for _, set in ipairs(child:getSets()) do if set.type == 'doc.class' and set.extends then for _, ext in ipairs(set.extends) do - if m.isSubType(globalMgr.getGlobal('type', ext[1]), parent, mark) then + if ext.type == 'doc.extends.name' + and vm.isSubType(globalMgr.getGlobal('type', ext[1]), parent, mark) then + return true + end + end + end + if set.type == 'doc.alias' and set.extends then + for _, ext in ipairs(set.extends.types) do + if ext.type == 'doc.type.name' + and vm.isSubType(globalMgr.getGlobal('type', ext[1]), parent, mark) then return true end end @@ -72,18 +85,18 @@ end ---@param tnode vm.node ---@param knode vm.node -function m.getTableValue(tnode, knode) +function vm.getTableValue(tnode, knode) local result for tn in nodeMgr.eachNode(tnode) do if tn.type == 'doc.type.table' then for _, field in ipairs(tn.fields) do - if m.isSubType(field.name, knode) then + if vm.isSubType(field.name, knode) then result = nodeMgr.mergeNode(result, compiler.compileNode(field.extends)) end end end if tn.type == 'doc.type.array' then - if m.isSubType(globalMgr.getGlobal('type', 'integer'), knode) then + if vm.isSubType(globalMgr.getGlobal('type', 'integer'), knode) then result = nodeMgr.mergeNode(result, compiler.compileNode(tn.node)) end end @@ -106,18 +119,18 @@ end ---@param tnode vm.node ---@param vnode vm.node -function m.getTableKey(tnode, vnode) +function vm.getTableKey(tnode, vnode) local result for tn in nodeMgr.eachNode(tnode) do if tn.type == 'doc.type.table' then for _, field in ipairs(tn.fields) do - if m.isSubType(field.extends, vnode) then + if vm.isSubType(field.extends, vnode) then result = nodeMgr.mergeNode(result, compiler.compileNode(field.name)) end end end if tn.type == 'doc.type.array' then - if m.isSubType(tn.node, vnode) then + if vm.isSubType(tn.node, vnode) then result = nodeMgr.mergeNode(result, globalMgr.getGlobal('type', 'integer')) end end @@ -137,5 +150,3 @@ function m.getTableKey(tnode, vnode) end return result end - -return m diff --git a/test/hover/init.lua b/test/hover/init.lua index 61ba392e..5ee4db51 100644 --- a/test/hover/init.lua +++ b/test/hover/init.lua @@ -1787,3 +1787,25 @@ local = 1 // 2 local x: integer = 1 ]] config.set(nil, 'Lua.runtime.nonstandardSymbol', {}) + +config.set(nil, 'Lua.hover.expandAlias', false) +TEST [[ +---@alias uri string + +---@type uri +local +]] +[[ +local uri: uri +]] + +config.set(nil, 'Lua.hover.expandAlias', true) +TEST [[ +---@alias uri string + +---@type uri +local +]] +[[ +local uri: string +]] -- cgit v1.2.3