diff options
author | fesily <fesil@foxmail.com> | 2024-02-21 15:08:38 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-21 15:08:38 +0800 |
commit | 74e74d2a5564f44abdfb31a0a23bf3aa7ec16fed (patch) | |
tree | 67b0df14295e7b3d3f37762781cc0a031965fedc /script/vm | |
parent | 647971cb339d0c3d5aa1da26041ab9ddb71e963b (diff) | |
parent | 3e6fd3ce1f2f0528336ded939d776a29bbfaf2eb (diff) | |
download | lua-language-server-74e74d2a5564f44abdfb31a0a23bf3aa7ec16fed.zip |
Merge branch 'master' into generic-pattern1
Diffstat (limited to 'script/vm')
-rw-r--r-- | script/vm/compiler.lua | 80 | ||||
-rw-r--r-- | script/vm/infer.lua | 20 |
2 files changed, 61 insertions, 39 deletions
diff --git a/script/vm/compiler.lua b/script/vm/compiler.lua index 2222fa9b..2253c83a 100644 --- a/script/vm/compiler.lua +++ b/script/vm/compiler.lua @@ -5,6 +5,7 @@ local rpath = require 'workspace.require-path' local files = require 'files' ---@class vm local vm = require 'vm.vm' +local plugin = require 'plugin' ---@class parser.object ---@field _compiledNodes boolean @@ -1031,6 +1032,55 @@ local function compileForVars(source, target) end ---@param source parser.object +local function compileFunctionParam(func, source) + -- local call ---@type fun(f: fun(x: number));call(function (x) end) --> x -> number + local funcNode = vm.compileNode(func) + for n in funcNode:eachObject() do + if n.type == 'doc.type.function' then + for index, arg in ipairs(n.args) do + if func.args[index] == source then + local argNode = vm.compileNode(arg) + for an in argNode:eachObject() do + if an.type ~= 'doc.generic.name' then + vm.setNode(source, an) + end + end + return true + end + end + end + end + if func.parent.type == 'local' then + local refs = func.parent.ref + local findCall + if refs then + for i, ref in ipairs(refs) do + if ref.parent.type == 'call' then + findCall = ref.parent + break + end + end + end + if findCall and findCall.args then + local index + for i, arg in ipairs(source.parent) do + if arg == source then + index = i + break + end + end + if index then + local callerArg = findCall.args[index] + if callerArg then + vm.setNode(source, vm.compileNode(callerArg)) + return true + end + end + end + end +end + +---@param source parser.object local function compileLocal(source) local myNode = vm.setNode(source, source) @@ -1069,7 +1119,6 @@ local function compileLocal(source) vm.setNode(source, vm.compileNode(setfield.node)) end end - if source.parent.type == 'funcargs' and not hasMarkDoc and not hasMarkParam then local func = source.parent.parent -- local call ---@type fun(f: fun(x: number));call(function (x) end) --> x -> number @@ -1090,35 +1139,6 @@ local function compileLocal(source) end end end - if not hasDocArg - and func.parent.type == 'local' then - local refs = func.parent.ref - local findCall - if refs then - for i, ref in ipairs(refs) do - if ref.parent.type == 'call' then - findCall = ref.parent - break - end - end - end - if findCall and findCall.args then - local index - for i, arg in ipairs(source.parent) do - if arg == source then - index = i - break - end - end - if index then - local callerArg = findCall.args[index] - if callerArg then - hasDocArg = true - vm.setNode(source, vm.compileNode(callerArg)) - end - end - end - end if not hasDocArg then vm.setNode(source, vm.declareGlobal('type', 'any')) end diff --git a/script/vm/infer.lua b/script/vm/infer.lua index f2673ed3..3f3d0e3a 100644 --- a/script/vm/infer.lua +++ b/script/vm/infer.lua @@ -157,22 +157,24 @@ local viewNodeSwitch;viewNodeSwitch = util.switch() end infer._hasClass = true local buf = {} - buf[#buf+1] = '{ ' + buf[#buf+1] = source.isTuple and '[' or '{ ' 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]) + if not source.isTuple then + 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 end buf[#buf+1] = vm.getInfer(field.extends):view(uri) end - buf[#buf+1] = ' }' + buf[#buf+1] = source.isTuple and ']' or ' }' return table.concat(buf) end) : case 'doc.type.string' |