diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2018-11-29 16:55:59 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2018-11-29 16:55:59 +0800 |
commit | 9992c649cde19d4db8c9a9a7b47eb71bb2eaa283 (patch) | |
tree | 1c85e8f75ad10c9c5febea94d837a5cfce773072 | |
parent | b13e39260cc47b90c9b5b095964ea56c1a3d7cd5 (diff) | |
download | lua-language-server-9992c649cde19d4db8c9a9a7b47eb71bb2eaa283.zip |
除了表以外的转到定义能用了
-rw-r--r-- | server/src/matcher/definition.lua | 19 | ||||
-rw-r--r-- | server/src/method/initialize.lua | 2 | ||||
-rw-r--r-- | server/src/method/textDocument/definition.lua | 13 | ||||
-rw-r--r-- | server/test/definition/bug.lua | 17 | ||||
-rw-r--r-- | server/test/definition/init.lua | 20 |
5 files changed, 55 insertions, 16 deletions
diff --git a/server/src/matcher/definition.lua b/server/src/matcher/definition.lua index 0a774a25..9897bcbc 100644 --- a/server/src/matcher/definition.lua +++ b/server/src/matcher/definition.lua @@ -35,7 +35,7 @@ end function mt:searchCall(call) for _, exp in ipairs(call) do - searchExp(exp) + self:searchExp(exp) end return nil end @@ -47,15 +47,12 @@ function mt:searchSimple(simple) local tp = obj.type if tp == 'call' then self:searchCall(obj) - elseif tp == ':' then - self:markLocal(obj) else if obj.index then self:searchExp(obj) end end end - return nil end function mt:searchBinary(exp) @@ -94,6 +91,16 @@ function mt:searchReturn(action) end end +function mt:markSimple(simple) + for i = 2, #simple do + local obj = simple[i] + local tp = obj.type + if tp == ':' then + self:createLocal('self', obj) + end + end +end + function mt:markSet(simple) if simple.type == 'name' then local str = simple[1] @@ -114,6 +121,7 @@ function mt:markSet(simple) self:checkName(simple) else self:searchSimple(simple) + self:markSimple(simple) end end @@ -233,6 +241,7 @@ end function mt:searchFunction(func) self.env:push() + self.env:cut 'dots' if func.name then self:markSet(func.name) end @@ -270,6 +279,8 @@ function mt:searchAction(action) self:markSets(action) elseif tp == 'local' then self:markLocals(action) + elseif tp == 'simple' then + self:searchSimple(action) elseif tp == 'if' then self:searchIfs(action) elseif tp == 'loop' then diff --git a/server/src/method/initialize.lua b/server/src/method/initialize.lua index 0bdd1b7c..ea5bb3c4 100644 --- a/server/src/method/initialize.lua +++ b/server/src/method/initialize.lua @@ -3,7 +3,7 @@ return function (lsp, data) return { capabilities = { -- 支持“转到定义” - --definitionProvider = true, + definitionProvider = true, -- 支持“转到实现” --implementationProvider = true, -- 文本同步方式 diff --git a/server/src/method/textDocument/definition.lua b/server/src/method/textDocument/definition.lua index 3a17b463..0878e29c 100644 --- a/server/src/method/textDocument/definition.lua +++ b/server/src/method/textDocument/definition.lua @@ -8,9 +8,14 @@ return function (lsp, params) return nil, '找不到文件:' .. uri end local start_clock = os.clock() + local ast, err = parser:ast(text) + local lines = parser:lines(text) + if not ast then + return nil, err + end -- lua是从1开始的,因此都要+1 - local pos = parser.calcline.position_utf8(text, params.position.line + 1, params.position.character + 1) - local suc, start, finish = matcher.definition(text, pos) + local position = lines:position(params.position.line + 1, params.position.character + 1) + local suc, start, finish = matcher.definition(ast, position, 'utf8') if not suc then if finish then log.debug(start, uri) @@ -20,8 +25,8 @@ return function (lsp, params) return {} end - local start_row, start_col = parser.calcline.rowcol_utf8(text, start) - local finish_row, finish_col = parser.calcline.rowcol_utf8(text, finish) + local start_row, start_col = lines:rowcol(start, 'utf8') + local finish_row, finish_col = lines:rowcol(finish, 'utf8') local response = { uri = uri, diff --git a/server/test/definition/bug.lua b/server/test/definition/bug.lua index b0e890ca..4ad2a05a 100644 --- a/server/test/definition/bug.lua +++ b/server/test/definition/bug.lua @@ -3,7 +3,7 @@ local <!x!> function _(x) end function _() - <?x?> + <?x?>() end ]] @@ -13,3 +13,18 @@ function _(<!x!>) <?x?> = 1 end ]] + +TEST [[ +function a<!:!>b() + a:b() + <?self?>() +end +]] + +TEST [[ +function _(...) + function _() + print(<?...?>) + end +end +]] diff --git a/server/test/definition/init.lua b/server/test/definition/init.lua index 8bf2411d..17c23b0e 100644 --- a/server/test/definition/init.lua +++ b/server/test/definition/init.lua @@ -4,22 +4,30 @@ local parser = require 'parser' rawset(_G, 'TEST', true) function TEST(script) - local start = script:find('<!', 1, true) + 2 - local finish = script:find('!>', 1, true) - 1 + local start = script:find('<!', 1, true) + local finish = script:find('!>', 1, true) + if start and finish then + start = start + 2 + finish = finish - 1 + end local pos = script:find('<?', 1, true) + 2 local new_script = script:gsub('<[!?]', ' '):gsub('[!?]>', ' ') local ast, err = parser:ast(new_script) assert(ast) local suc, a, b = matcher.definition(ast, pos) - assert(suc) - assert(a == start) - assert(b == finish) + if start and finish then + assert(suc == true) + assert(a == start) + assert(b == finish) + else + assert(suc == false) + end end require 'definition.set' require 'definition.local' require 'definition.arg' require 'definition.function' -require 'definition.table' +--require 'definition.table' require 'definition.bug' |