diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2022-04-22 01:19:42 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2022-04-22 01:19:42 +0800 |
commit | 5b148c7f76e9bbdbbee28e0dac51c2b14f7e7ebd (patch) | |
tree | 2440f19ea2caa935fb8aea3224a8a978e9a2c165 /script | |
parent | bac01d19d230594106cca59ff8abbd294a17ced4 (diff) | |
download | lua-language-server-5b148c7f76e9bbdbbee28e0dac51c2b14f7e7ebd.zip |
update optional of parameter
Diffstat (limited to 'script')
-rw-r--r-- | script/core/hover/args.lua | 24 | ||||
-rw-r--r-- | script/vm/compiler.lua | 6 | ||||
-rw-r--r-- | script/vm/infer.lua | 35 |
3 files changed, 34 insertions, 31 deletions
diff --git a/script/core/hover/args.lua b/script/core/hover/args.lua index a53136b0..d943d11f 100644 --- a/script/core/hover/args.lua +++ b/script/core/hover/args.lua @@ -1,17 +1,6 @@ local guide = require 'parser.guide' local infer = require 'vm.infer' - -local function optionalArg(arg) - if not arg.bindDocs then - return false - end - local name = arg[1] - for _, doc in ipairs(arg.bindDocs) do - if doc.type == 'doc.param' and doc.param[1] == name then - return doc.optional - end - end -end +local vm = require 'vm' local function asFunction(source) local args = {} @@ -31,10 +20,17 @@ local function asFunction(source) end local name = arg.name or guide.getKeyName(arg) if name then + local argNode = vm.compileNode(arg) + local optional + if argNode:isOptional() then + optional = true + argNode = argNode:copy() + argNode:removeOptional() + end args[#args+1] = ('%s%s: %s'):format( name, - optionalArg(arg) and '?' or '', - infer.getInfer(arg):view 'any' + optional and '?' or '', + infer.getInfer(argNode):view('any', guide.getUri(source)) ) elseif arg.type == '...' then args[#args+1] = ('%s: %s'):format( diff --git a/script/vm/compiler.lua b/script/vm/compiler.lua index c83262ab..883d89b9 100644 --- a/script/vm/compiler.lua +++ b/script/vm/compiler.lua @@ -517,7 +517,11 @@ local function bindDocs(source) end if doc.type == 'doc.param' then if isParam and source[1] == doc.param[1] then - vm.setNode(source, vm.compileNode(doc)) + local node = vm.compileNode(doc) + if doc.optional then + node:addOptional() + end + vm.setNode(source, node) return true end end diff --git a/script/vm/infer.lua b/script/vm/infer.lua index 7bb581cf..b5e717d5 100644 --- a/script/vm/infer.lua +++ b/script/vm/infer.lua @@ -207,24 +207,24 @@ function mt:_trim() if self._hasTable and not self._hasClass then self.views['table'] = true end - if self._hasClass then - self:_eraseAlias() - end end -function mt:_eraseAlias() - local expandAlias = config.get(self.uri, 'Lua.hover.expandAlias') +---@param uri uri +---@return table<string, true> +function mt:_eraseAlias(uri) + local drop = {} + local expandAlias = config.get(uri, 'Lua.hover.expandAlias') for n in self.node:eachObject() do if n.type == 'global' and n.cate == 'type' then - for _, set in ipairs(n:getSets(self.uri)) do + for _, set in ipairs(n:getSets(uri)) do if set.type == 'doc.alias' then if expandAlias then - self.views[n.name] = nil + drop[n.name] = true else for _, ext in ipairs(set.extends.types) do local view = viewNodeSwitch(ext.type, ext, {}) if view and view ~= n.name then - self.views[view] = nil + drop[view] = true end end end @@ -232,6 +232,7 @@ function mt:_eraseAlias() end end end + return drop end ---@param tp string @@ -281,17 +282,20 @@ function mt:view(default, uri) return 'any' end - if not next(self.views) then - return default or 'unknown' - end - - if self.cachedView then - return self.cachedView + local drop + if self._hasClass then + drop = self:_eraseAlias(uri or self.uri) end local array = {} for view in pairs(self.views) do - array[#array+1] = view + if not drop or not drop[view] then + array[#array+1] = view + end + end + + if #array == 0 then + return default or 'unknown' end table.sort(array, function (a, b) @@ -323,7 +327,6 @@ function mt:view(default, uri) view = view .. '?' end end - self.cachedView = view return view end |