diff options
-rw-r--r-- | server/src/matcher/definition.lua | 30 | ||||
-rw-r--r-- | server/src/method/textDocument/definition.lua | 2 | ||||
-rw-r--r-- | server/test/definition/init.lua | 1 | ||||
-rw-r--r-- | server/test/definition/label.lua | 23 |
4 files changed, 54 insertions, 2 deletions
diff --git a/server/src/matcher/definition.lua b/server/src/matcher/definition.lua index 045f8171..8e9dfca8 100644 --- a/server/src/matcher/definition.lua +++ b/server/src/matcher/definition.lua @@ -402,6 +402,7 @@ end function mt:searchFunction(func) self.env:push() self.env:cut 'dots' + self.env.label = {} if func.name then self:markSet(func.name) end @@ -422,6 +423,26 @@ function mt:searchLocalFunction(func) self.env:pop() end +function mt:markLabel(label) + local str = label[1] + if not self.env.label[str] then + self.env.label[str] = { + type = 'label', + } + end + table.insert(self.env.label[str], label) +end + +function mt:searchGoTo(obj) + local str = obj[1] + if not self.env.label[str] then + self.env.label[str] = { + type = 'label', + } + end + self.result = self.env.label[str] +end + function mt:searchAction(action) local tp = action.type if tp == 'do' then @@ -430,7 +451,9 @@ function mt:searchAction(action) elseif tp == 'return' then self:searchReturn(action) elseif tp == 'label' then + self:markLabel(action) elseif tp == 'goto' then + self:searchGoTo(action) elseif tp == 'set' then self:markSets(action) elseif tp == 'local' then @@ -487,6 +510,10 @@ local function parseResult(result) elseif tp == 'dots' then local dots = result.dots results[1] = {dots.start, dots.finish} + elseif tp == 'label' then + for i, label in ipairs(result) do + results[i] = {label.start, label.finish} + end else error('unknow result.type:' .. result.type) end @@ -496,8 +523,9 @@ end return function (ast, pos) local searcher = setmetatable({ pos = pos, - env = env {var = {}, usable = {}, label = {}}, + env = env {var = {}, usable = {}}, }, mt) + searcher.env.label = {} searcher:createLocal('_ENV') searcher:searchActions(ast) diff --git a/server/src/method/textDocument/definition.lua b/server/src/method/textDocument/definition.lua index f6557061..bb8918d6 100644 --- a/server/src/method/textDocument/definition.lua +++ b/server/src/method/textDocument/definition.lua @@ -6,7 +6,7 @@ return function (lsp, params) local uri = params.textDocument.uri local ast, lines = lsp:loadText(uri) if not ast then - return nil, '找不到文件:' .. uri + return {} end -- lua是从1开始的,因此都要+1 local position = lines:position(params.position.line + 1, params.position.character + 1, 'utf8') diff --git a/server/test/definition/init.lua b/server/test/definition/init.lua index c13f1fb1..7d4549a1 100644 --- a/server/test/definition/init.lua +++ b/server/test/definition/init.lua @@ -61,4 +61,5 @@ require 'definition.arg' require 'definition.function' require 'definition.table' require 'definition.method' +require 'definition.label' require 'definition.bug' diff --git a/server/test/definition/label.lua b/server/test/definition/label.lua new file mode 100644 index 00000000..27a1ac7f --- /dev/null +++ b/server/test/definition/label.lua @@ -0,0 +1,23 @@ +TEST [[ +::<!LABEL!>:: +goto <?LABEL?> +]] + +TEST [[ +goto <?LABEL?> +::<!LABEL!>:: +]] + +TEST [[ +::LABEL:: +function _() + goto <?LABEL?> +end +]] + +TEST [[ +do + goto <?LABEL?> +end +::<!LABEL!>:: +]] |