diff options
-rw-r--r-- | server/src/matcher/vm.lua | 32 | ||||
-rw-r--r-- | server/src/utility.lua | 9 | ||||
-rw-r--r-- | server/src/workspace.lua | 5 | ||||
-rw-r--r-- | server/test/vm/normal.lua | 6 |
4 files changed, 49 insertions, 3 deletions
diff --git a/server/src/matcher/vm.lua b/server/src/matcher/vm.lua index c9fe1fd0..05b4f2e6 100644 --- a/server/src/matcher/vm.lua +++ b/server/src/matcher/vm.lua @@ -35,6 +35,24 @@ local function orderTable() }) end +local function deepCopy(t, mark, new) + mark = mark or {} + new = new or {} + for k, v in pairs(t) do + if type(v) == 'table' then + if mark[v] then + new[k] = mark[v] + else + mark[v] = {} + new[k] = deepCopy(v, mark, mark[v]) + end + else + new[k] = v + end + end + return new +end + local mt = {} mt.__index = mt @@ -1157,6 +1175,19 @@ function mt:createEnvironment() gValue.child = envValue.child end +function mt:mergeRequire(value, destVM) + -- 取出对方的主函数 + local main = destVM.results.main + -- 获取主函数返回值,注意不能修改对方的环境 + local mainValue + if not main.returns then + mainValue = self:createValue('nil') + else + mainValue = deepCopy(main.returns[1]) + end + self:mergeValue(value, mainValue) +end + function mt:loadRequires() if not self.lsp or not self.lsp.workspace then return @@ -1168,6 +1199,7 @@ function mt:loadRequires() -- 如果循环require,这里会返回nil local destVM = self.lsp:loadVM(uri) if destVM then + self:mergeRequire(req.value, destVM) end end end diff --git a/server/src/utility.lua b/server/src/utility.lua index 88c26668..9c330ff7 100644 --- a/server/src/utility.lua +++ b/server/src/utility.lua @@ -143,7 +143,7 @@ function io.save(file_path, content) end end -function io.scan(path) +function io.scan(path, ignore) local result = {path} local i = 0 return function () @@ -153,8 +153,11 @@ function io.scan(path) return nil end if fs.is_directory(current) then - for path in current:list_directory() do - result[#result+1] = path + local dirName = current:filename():string():lower() + if not ignore or not ignore[dirName] then + for path in current:list_directory() do + result[#result+1] = path + end end end return current diff --git a/server/src/workspace.lua b/server/src/workspace.lua index de1f417c..356960a2 100644 --- a/server/src/workspace.lua +++ b/server/src/workspace.lua @@ -53,6 +53,10 @@ function mt:init(rootUri) require 'utility' local fs = require 'bee.filesystem' local list = {} + local ignore = { + ['.git'] = true, + ['node_modules'] = true, + } for path in io.scan(fs.path(ROOT)) do if path:extension():string() == '.lua' then list[#list+1] = path:string() @@ -96,6 +100,7 @@ function mt:searchPath(str) for _, searcher in ipairs(searchers) do if filename:sub(-#searcher) == searcher then self.loaded[str] = uri + self.lsp:readText(uri, fs.path(filename)) return uri end end diff --git a/server/test/vm/normal.lua b/server/test/vm/normal.lua index de9c6ca2..20b73733 100644 --- a/server/test/vm/normal.lua +++ b/server/test/vm/normal.lua @@ -115,3 +115,9 @@ end local v = 2 xx() ]] + +TEST [[ +return { + 1, 2, 3 +} +]] |