diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2018-11-20 13:35:16 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2018-11-20 13:35:16 +0800 |
commit | c6e00ba82ab0e15fbbaad59ef91cfb08caeb6f7d (patch) | |
tree | 808fde71dbcd2a3d6436d381e2e6100e78281c29 | |
parent | 4321271853b484150d37b9908fdab46fde0ad8fa (diff) | |
download | lua-language-server-c6e00ba82ab0e15fbbaad59ef91cfb08caeb6f7d.zip |
支持多个实现
-rw-r--r-- | src/matcher/implementation.lua | 47 | ||||
-rw-r--r-- | src/method/textDocument/implementation.lua | 39 | ||||
-rw-r--r-- | test/implementation/bug.lua | 9 | ||||
-rw-r--r-- | test/implementation/init.lua | 39 |
4 files changed, 95 insertions, 39 deletions
diff --git a/src/matcher/implementation.lua b/src/matcher/implementation.lua index e245ef90..2d601bde 100644 --- a/src/matcher/implementation.lua +++ b/src/matcher/implementation.lua @@ -16,9 +16,9 @@ end local function scopeGet(name) for i = #scopes, 1, -1 do local scope = scopes[i] - local obj = scope[name] - if obj then - return obj + local list = scope[name] + if list then + return list end end return nil @@ -27,21 +27,26 @@ end local function scopeSet(obj) local name = obj[1] local scope = scopes[#scopes] - scope[name] = obj + local list = scope[name] + if list then + list[#list+1] = obj + else + scope[name] = {obj} + end end local function globalSet(obj) local name = obj[1] for i = #scopes, 1, -1 do local scope = scopes[i] - local old = scope[name] - if old then - scope[name] = obj + local list = scope[name] + if list then + list[#list+1] = obj return end end local scope = scopes[1] - scope[name] = obj + scope[name] = {obj} end local function scopePush() @@ -53,10 +58,25 @@ local function scopePop() end local function checkImplementation(name, p) + if result ~= nil then + return + end if pos < p or pos > p + #name then return end - result = scopeGet(name) + local list = scopeGet(name) + if list then + result = {} + for i, obj in ipairs(list) do + local name, start, finish = obj[1], obj[2], obj[3] + if not finish then + finish = start + #name - 1 + end + result[i] = {start, finish} + end + else + result = false + end end function defs.NamePos(p) @@ -269,12 +289,5 @@ return function (buf, pos_) if not result then return false, 'No word' end - local name, start, finish = result[1], result[2], result[3] - if not start then - return false, 'No match' - end - if not finish then - finish = start + #name - 1 - end - return true, start, finish + return true, result end diff --git a/src/method/textDocument/implementation.lua b/src/method/textDocument/implementation.lua index 95bd0c20..6008efb7 100644 --- a/src/method/textDocument/implementation.lua +++ b/src/method/textDocument/implementation.lua @@ -10,7 +10,7 @@ return function (lsp, params) local start_clock = os.clock() -- lua是从1开始的,因此都要+1 local pos = parser.calcline.position_utf8(text, params.position.line + 1, params.position.character + 1) - local suc, start, finish = matcher.implementation(text, pos) + local suc, results = matcher.implementation(text, pos) if not suc then if finish then log.debug(start, uri) @@ -20,23 +20,28 @@ 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 locations = {} + for i, result in ipairs(results) do + local start, finish = result[1], result[2] + local start_row, start_col = parser.calcline.rowcol_utf8(text, start) + local finish_row, finish_col = parser.calcline.rowcol_utf8(text, finish) + locations[i] = { + uri = uri, + range = { + start = { + line = start_row - 1, + character = start_col - 1, + }, + ['end'] = { + line = finish_row - 1, + -- 这里不用-1,因为前端期待的是匹配完成后的位置 + character = finish_col, + }, + } + } + end - local response = { - uri = uri, - range = { - start = { - line = start_row - 1, - character = start_col - 1, - }, - ['end'] = { - line = finish_row - 1, - -- 这里不用-1,因为前端期待的是匹配完成后的位置 - character = finish_col, - }, - }, - } + local response = locations local passed_clock = os.clock() - start_clock if passed_clock >= 0.01 then log.warn(('[转到实现]耗时[%.3f]秒,文件大小[%s]字节'):format(passed_clock, #text)) diff --git a/test/implementation/bug.lua b/test/implementation/bug.lua index b0e890ca..8f80030b 100644 --- a/test/implementation/bug.lua +++ b/test/implementation/bug.lua @@ -13,3 +13,12 @@ function _(<!x!>) <?x?> = 1 end ]] + +TEST [[ +local <!x!> +if 1 then + x = 1 +else + <?x?> = 2 +end +]] diff --git a/test/implementation/init.lua b/test/implementation/init.lua index 3033f340..855bf255 100644 --- a/test/implementation/init.lua +++ b/test/implementation/init.lua @@ -2,16 +2,45 @@ local matcher = require 'matcher' rawset(_G, 'TEST', true) +local function catch_target(script) + local list = {} + local cur = 1 + while true do + local start, finish = script:find('<!.-!>', cur) + if not start then + break + end + list[#list+1] = { start + 2, finish - 2 } + cur = finish + 1 + end + return list +end + +local function founded(targets, results) + while true do + local target = table.remove(targets) + if not target then + return true + end + for i, result in ipairs(results) do + if target[1] == result[1] and target[2] == result[2] then + table.remove(results, i) + goto CONTINUE + end + end + do return false end + ::CONTINUE:: + end +end + function TEST(script) - local start = script:find('<!', 1, true) + 2 - local finish = script:find('!>', 1, true) - 1 + local target = catch_target(script) local pos = script:find('<?', 1, true) + 2 local new_script = script:gsub('<[!?]', ' '):gsub('[!?]>', ' ') - local suc, a, b = matcher.implementation(new_script, pos) + local suc, result = matcher.implementation(new_script, pos) assert(suc) - assert(a == start) - assert(b == finish) + assert(founded(target, result)) end require 'implementation.set' |