diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2018-12-10 14:18:01 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2018-12-10 14:18:01 +0800 |
commit | 03651e86bd8a240317cf081a0d0c05dac64f1165 (patch) | |
tree | 424a296625d449fefdb133b4c5bc40ddce18436d /server | |
parent | 41aea3f8753e58ba460907f82e30bd093e3ee463 (diff) | |
download | lua-language-server-03651e86bd8a240317cf081a0d0c05dac64f1165.zip |
整理代码
Diffstat (limited to 'server')
-rw-r--r-- | server/src/matcher/find_lib.lua | 36 | ||||
-rw-r--r-- | server/src/matcher/library.lua | 68 |
2 files changed, 84 insertions, 20 deletions
diff --git a/server/src/matcher/find_lib.lua b/server/src/matcher/find_lib.lua index b9efacb2..090859db 100644 --- a/server/src/matcher/find_lib.lua +++ b/server/src/matcher/find_lib.lua @@ -28,6 +28,9 @@ local function checkSourceAsLibrary(value, name, showname) end local function checkSource(value, libname, lib) + if not lib then + return + end if not lib.source then return checkSourceAsGlobal(value, libname) end @@ -102,16 +105,39 @@ end local function findLib(var) local value = var.value or var - for libname, lib in pairs(libs) do - if lib.parent then + for libname, info in pairs(libs.global) do + local fullKey = checkSource(value, libname, info.lib) + if fullKey then + return info.lib, fullKey, false + end + for libname, lib in pairs(info.child) do + local fullKey, oo = checkParent(value, libname, lib) + if fullKey then + return lib, fullKey, oo + end + end + end + for libname, info in pairs(libs.library) do + local fullKey = checkSource(value, libname, info.lib) + if fullKey then + return info.lib, fullKey, false + end + for libname, lib in pairs(info.child) do local fullKey, oo = checkParent(value, libname, lib) if fullKey then return lib, fullKey, oo end - else - local fullKey = checkSource(value, libname, lib) + end + end + for libname, info in pairs(libs.object) do + local fullKey = checkSource(value, libname, info.lib) + if fullKey then + return info.lib, fullKey, false + end + for libname, lib in pairs(info.child) do + local fullKey, oo = checkParent(value, libname, lib) if fullKey then - return lib, fullKey, false + return lib, fullKey, oo end end end diff --git a/server/src/matcher/library.lua b/server/src/matcher/library.lua index 989ac1a4..dbc96af6 100644 --- a/server/src/matcher/library.lua +++ b/server/src/matcher/library.lua @@ -48,13 +48,58 @@ local function mergeLocale(libs, locale) end end -local function mergeLibs(target, libs) +local function mergeSource(alllibs, name, lib) + if not lib.source then + alllibs.global[name] = { lib = lib, child = {} } + return + end + for _, source in ipairs(lib.source) do + local sourceName = source.name or name + if source.type == 'global' then + alllibs.global[sourceName] = { lib = lib, child = {} } + elseif source.type == 'library' then + alllibs.library[sourceName] = { lib = lib, child = {} } + elseif source.type == 'object' then + alllibs.object[sourceName] = { lib = lib, child = {} } + end + end +end + +local function insert(tbl, name, key, value) + if not name or not key then + return + end + if not tbl[name] then + tbl[name] = {} + end + if not tbl[name].child then + tbl[name].child = {} + end + tbl[name].child[key] = value +end + +local function mergeParent(alllibs, name, lib) + for _, parent in ipairs(lib.parent) do + if parent.type == 'global' then + insert(alllibs.global, parent.name, name, lib) + elseif parent.type == 'library' then + insert(alllibs.library, parent.name, name, lib) + elseif parent.type == 'object' then + insert(alllibs.object, parent.name, name, lib) + end + end +end + +local function mergeLibs(alllibs, libs) if not libs then return end for name, lib in pairs(libs) do - target.names[#target.names+1] = name - target.libs[#target.libs+1] = lib + if lib.parent then + mergeParent(alllibs, name, lib) + else + mergeSource(alllibs, name, lib) + end end end @@ -71,18 +116,11 @@ end local function init() local language = require 'language' - local alllibs = setmetatable({ - names = {}, - libs = {}, - }, { - __pairs = function (self) - local i = 0 - return function () - i = i + 1 - return self.names[i], self.libs[i] - end - end, - }) + local alllibs = { + global = table.container(), + library = table.container(), + object = table.container(), + } for path in io.scan(ROOT / 'libs') do local libs local buf = io.load(path) |