diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2018-12-20 20:30:00 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2018-12-20 20:30:00 +0800 |
commit | ce7775f054c07c0ab8f13ed84ca1694659f2ea90 (patch) | |
tree | e9853915e2a841a4f9998308826f2610e5a63f4d /server | |
parent | 57c35307702b66554ff1b3f2ff3996ce9aabab93 (diff) | |
download | lua-language-server-ce7775f054c07c0ab8f13ed84ca1694659f2ea90.zip |
支持从require 'xxx'跳转
Diffstat (limited to 'server')
-rw-r--r-- | server/src/matcher/definition.lua | 7 | ||||
-rw-r--r-- | server/src/matcher/vm.lua | 27 |
2 files changed, 27 insertions, 7 deletions
diff --git a/server/src/matcher/definition.lua b/server/src/matcher/definition.lua index c31f817c..18804516 100644 --- a/server/src/matcher/definition.lua +++ b/server/src/matcher/definition.lua @@ -64,6 +64,13 @@ local function parseResult(vm, result) } end end + elseif tp == 'string' then + -- require 'XXX' 专用 + positions[#positions+1] = { + 0, + 0, + result.uri, + } end return positions end diff --git a/server/src/matcher/vm.lua b/server/src/matcher/vm.lua index 2eef1da2..b9907804 100644 --- a/server/src/matcher/vm.lua +++ b/server/src/matcher/vm.lua @@ -476,7 +476,7 @@ function mt:callRequire(func, values) end local requireValue = self:createValue('boolean', nil, true) self:setFunctionReturn(func, 1, requireValue) - self.requires[requireValue] = str + self.requires[requireValue] = values[1] end function mt:call(func, values) @@ -1176,17 +1176,28 @@ function mt:createEnvironment() gValue.child = envValue.child end -function mt:mergeRequire(value, destVM) +function mt:mergeRequire(value, strValue, destVM) -- 取出对方的主函数 local main = destVM.results.main -- 获取主函数返回值,注意不能修改对方的环境 local mainValue if not main.returns then - mainValue = self:createValue('nil') + mainValue = self:createValue('nil', nil, { + type = 'name', + start = 0, + finish = 0, + [1] = '', + uri = destVM.uri, + }) else mainValue = deepCopy(main.returns[1]) end self:mergeValue(value, mainValue) + + -- 支持 require 'xxx' 的转到定义 + local strSource = strValue.source + self.results.sources[strSource] = strValue + strValue.uri = destVM.uri end function mt:loadRequires() @@ -1198,14 +1209,15 @@ function mt:loadRequires() self.requires[k] = nil copy[k] = v end - for value, str in pairs(copy) do + for value, strValue in pairs(copy) do + local str = strValue.value if type(str) == 'string' then local uri = self.lsp.workspace:searchPath(str) -- 如果循环require,这里会返回nil -- 会当场编译VM local destVM = self.lsp:loadVM(uri) if destVM then - self:mergeRequire(value, destVM) + self:mergeRequire(value, strValue, destVM) end end end @@ -1215,13 +1227,14 @@ function mt:tryLoadRequires() if not self.lsp or not self.lsp.workspace then return end - for value, str in pairs(self.requires) do + for value, strValue in pairs(self.requires) do + local str = strValue.value if type(str) == 'string' then local uri = self.lsp.workspace:searchPath(str) -- 如果取不到VM(不编译),则做个标记,之后再取一次 local destVM = self.lsp:getVM(uri) if destVM then - self:mergeRequire(value, destVM) + self:mergeRequire(value, strValue, destVM) self.requires[value] = nil else self.lsp:needRequires(self.uri) |