diff options
Diffstat (limited to 'server/src/method')
-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 |
3 files changed, 50 insertions, 0 deletions
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 |