diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2018-12-07 12:15:10 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2018-12-07 12:15:10 +0800 |
commit | c0138bc76ff80e9e7cc524c8b335f6bf8e6c1e28 (patch) | |
tree | d3e9b91971aab515d2a1e3f6be642fe06f683f6d /server | |
parent | 0414a4e0179c158dc66d46e22d2c7cb2c3b3608e (diff) | |
download | lua-language-server-c0138bc76ff80e9e7cc524c8b335f6bf8e6c1e28.zip |
支持库
Diffstat (limited to 'server')
-rw-r--r-- | server/src/matcher/compile.lua | 71 | ||||
-rw-r--r-- | server/src/matcher/find_lib.lua | 20 | ||||
-rw-r--r-- | server/test/find_lib/init.lua | 6 |
3 files changed, 80 insertions, 17 deletions
diff --git a/server/src/matcher/compile.lua b/server/src/matcher/compile.lua index 5ed3e97a..2c53f618 100644 --- a/server/src/matcher/compile.lua +++ b/server/src/matcher/compile.lua @@ -56,6 +56,14 @@ function mt:createDots() return dots end +function mt:createLib(name) + local lib = { + name = name, + type = 'lib', + } + return lib +end + function mt:createLocal(key, source, var) if key == nil then return nil @@ -132,6 +140,34 @@ function mt:checkDots(source) self:addInfo(dots, 'get', source) end +function mt:fixCallAsSetMetaTable(results) + local obj = results[1] + local metatable = results[2] + if metatable then + local index = self:getField(metatable, '__index') + if obj then + self:setMeta(obj, index) + return obj + else + return index + end + else + return obj + end +end + +function mt:fixCallAsRequire(results) + local libname = results[1] + if not libname then + return + end + libname = libname.value or libname + if libname.type ~= 'string' then + return + end + return self:createLib(libname.string) +end + function mt:searchCall(call, func) local results = {} for i, exp in ipairs(call) do @@ -141,20 +177,13 @@ function mt:searchCall(call, func) local api = self:getApi(func) if api == 'setmetatable' then - local obj = results[1] - local metatable = results[2] - if metatable then - local index = self:getField(metatable, '__index') - if obj then - self:setMeta(obj, index) - return obj - else - return index - end - else - return obj - end + return self:fixCallAsSetMetaTable(results) end + + if api == 'require' then + return self:fixCallAsRequire(results) + end + return nil end @@ -220,10 +249,18 @@ function mt:searchTable(exp) return tbl end +function mt:getString(exp) + return { + type = 'string', + string = exp[1], + } +end + function mt:searchExp(exp) local tp = exp.type if tp == 'nil' then elseif tp == 'string' then + return self:getString(exp) elseif tp == 'boolean' then elseif tp == 'number' then elseif tp == 'name' then @@ -254,10 +291,12 @@ function mt:setValue(var, value) if not var or not value then return end - var.childs = value.childs var.value = value.value or value - for _, child in pairs(value.childs) do - child.parent = var + if value.childs then + var.childs = value.childs + for _, child in pairs(value.childs) do + child.parent = var + end end end diff --git a/server/src/matcher/find_lib.lua b/server/src/matcher/find_lib.lua index 84cf44c5..7001117e 100644 --- a/server/src/matcher/find_lib.lua +++ b/server/src/matcher/find_lib.lua @@ -110,7 +110,6 @@ local function isGlobal(var) end local function checkLibAsGlobal(var, name) - -- 检查是否是全局变量 local value = var.value or var if value.key == name and isGlobal(value) then return name @@ -118,6 +117,20 @@ local function checkLibAsGlobal(var, name) return nil end +local function checkLibAsLibrary(var, name) + local value = var.value + if not value then + return nil + end + if value.type ~= 'lib' then + return nil + end + if value.name == name then + return name + end + return nil +end + local function checkLib(var, name, lib) if not lib.source then return checkLibAsGlobal(var, name) @@ -128,6 +141,11 @@ local function checkLib(var, name, lib) if fullkey then return fullkey end + elseif source.type == 'library' then + local fullkey = checkLibAsLibrary(var, name) + if fullkey then + return fullkey + end end end return nil diff --git a/server/test/find_lib/init.lua b/server/test/find_lib/init.lua index 29297bcf..afa9ec2a 100644 --- a/server/test/find_lib/init.lua +++ b/server/test/find_lib/init.lua @@ -49,3 +49,9 @@ TEST 'table' [[ TEST 'table' [[ local <?xx?> = require 'table' ]] + +TEST 'table' [[ +local rq = require +local lib = 'table' +local <?xx?> = rq(lib) +]] |