diff options
Diffstat (limited to 'server/src')
-rw-r--r-- | server/src/matcher/compile.lua | 3 | ||||
-rw-r--r-- | server/src/matcher/definition.lua | 8 | ||||
-rw-r--r-- | server/src/matcher/init.lua | 1 | ||||
-rw-r--r-- | server/src/matcher/references.lua | 105 | ||||
-rw-r--r-- | server/src/method/init.lua | 1 | ||||
-rw-r--r-- | server/src/method/initialize.lua | 2 | ||||
-rw-r--r-- | server/src/method/textDocument/references.lua | 47 |
7 files changed, 162 insertions, 5 deletions
diff --git a/server/src/matcher/compile.lua b/server/src/matcher/compile.lua index eb85bc61..4e0018a9 100644 --- a/server/src/matcher/compile.lua +++ b/server/src/matcher/compile.lua @@ -62,6 +62,7 @@ function mt:createLocal(key, source, var) end if not var then var = self:createVar('local', key, source) + self:addInfo(var, 'local', source) end self.env.var[key] = var return var @@ -514,7 +515,7 @@ return function (ast) } }, mt) searcher.env.label = {} - searcher:createLocal('_ENV') + searcher:createLocal('_ENV', { start = -1, finish = -1 }) searcher:searchActions(ast) return searcher.results diff --git a/server/src/matcher/definition.lua b/server/src/matcher/definition.lua index 4dec7430..27dfe450 100644 --- a/server/src/matcher/definition.lua +++ b/server/src/matcher/definition.lua @@ -63,11 +63,11 @@ local function parseResult(result) if tp == 'var' then local var = result.var if var.type == 'local' then - local source = var.source - if not source then - return false + for _, info in ipairs(var) do + if info.type == 'local' then + positions[#positions+1] = {info.source.start, info.source.finish} + end end - positions[1] = {source.start, source.finish} elseif var.type == 'field' then for _, info in ipairs(var) do if info.type == 'set' then diff --git a/server/src/matcher/init.lua b/server/src/matcher/init.lua index 14a95ed2..af604289 100644 --- a/server/src/matcher/init.lua +++ b/server/src/matcher/init.lua @@ -1,6 +1,7 @@ local api = { definition = require 'matcher.definition', implementation = require 'matcher.implementation', + references = require 'matcher.references', compile = require 'matcher.compile', } diff --git a/server/src/matcher/references.lua b/server/src/matcher/references.lua new file mode 100644 index 00000000..c0a98fe3 --- /dev/null +++ b/server/src/matcher/references.lua @@ -0,0 +1,105 @@ + +local function isContainPos(obj, pos) + return obj.start <= pos and obj.finish + 1 >= pos +end + +local function findResult(results, pos) + for _, var in ipairs(results.vars) do + for _, info in ipairs(var) do + if isContainPos(info.source, pos) then + return { + type = 'var', + var = var, + } + end + end + end + for _, dots in ipairs(results.dots) do + for _, info in ipairs(dots) do + if isContainPos(info.source, pos) then + return { + type = 'dots', + dots = dots, + } + end + end + end + for _, label in ipairs(results.labels) do + for _, info in ipairs(label) do + if isContainPos(info.source, pos) then + return { + type = 'label', + label = label, + } + end + end + end + return nil +end + +local function tryMeta(var) + local keys = {} + repeat + if var.childs.meta then + local metavar = var.childs.meta + for i = #keys, 1, -1 do + local key = keys[i] + metavar = metavar.childs[key] + if not metavar then + return nil + end + end + return metavar + end + keys[#keys+1] = var.key + var = var.parent + until not var + return nil +end + +local function parseResult(result, declarat) + local positions = {} + local tp = result.type + if tp == 'var' then + local var = result.var + for _, info in ipairs(var) do + if declarat or info.type == 'get' then + positions[#positions+1] = {info.source.start, info.source.finish} + end + end + local metavar = tryMeta(var) + if metavar then + for _, info in ipairs(metavar) do + if declarat or info.type == 'get' then + positions[#positions+1] = {info.source.start, info.source.finish} + end + end + end + elseif tp == 'dots' then + local dots = result.dots + for _, info in ipairs(dots) do + if declarat or info.type == 'get' then + positions[#positions+1] = {info.source.start, info.source.finish} + end + end + elseif tp == 'label' then + local label = result.label + for _, info in ipairs(label) do + if declarat or info.type == 'goto' then + positions[#positions+1] = {info.source.start, info.source.finish} + end + end + else + error('Unknow result type:' .. result.type) + end + return positions +end + +return function (results, pos, declarat) + local result = findResult(results, pos) + if not result then + return nil + end + local positions = parseResult(result, declarat) + return positions +end diff --git a/server/src/method/init.lua b/server/src/method/init.lua index a99c3ed5..bee00c90 100644 --- a/server/src/method/init.lua +++ b/server/src/method/init.lua @@ -13,5 +13,6 @@ init 'textDocument/definition' init 'textDocument/didOpen' init 'textDocument/didChange' init 'textDocument/didClose' +init 'textDocument/references' return method diff --git a/server/src/method/initialize.lua b/server/src/method/initialize.lua index 866ded66..e1baa216 100644 --- a/server/src/method/initialize.lua +++ b/server/src/method/initialize.lua @@ -6,6 +6,8 @@ return function (lsp, data) definitionProvider = true, -- 支持“转到实现” implementationProvider = true, + -- 支持“查找引用” + referencesProvider = true, -- 文本同步方式 textDocumentSync = { -- 打开关闭文本时通知 diff --git a/server/src/method/textDocument/references.lua b/server/src/method/textDocument/references.lua new file mode 100644 index 00000000..22504d34 --- /dev/null +++ b/server/src/method/textDocument/references.lua @@ -0,0 +1,47 @@ +local parser = require 'parser' +local matcher = require 'matcher' + +return function (lsp, params) + local start_clock = os.clock() + local uri = params.textDocument.uri + local declarat = params.context.includeDeclaration + local results, lines = lsp:loadText(uri) + if not results then + return {} + end + -- lua是从1开始的,因此都要+1 + local position = lines:position(params.position.line + 1, params.position.character + 1, 'utf8') + local positions = matcher.references(results, position, declarat) + if not positions then + return {} + end + + local locations = {} + for i, position in ipairs(positions) do + local start, finish = position[1], position[2] + local start_row, start_col = lines:rowcol(start, 'utf8') + local finish_row, finish_col = lines:rowcol(finish, 'utf8') + 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 = locations + local passed_clock = os.clock() - start_clock + if passed_clock >= 0.01 then + log.warn(('[Find References] takes [%.3f] sec, size [%s] bits.'):format(passed_clock, #lines.buf)) + end + + return response +end |