summaryrefslogtreecommitdiff
path: root/server/src/method/textDocument/references.lua
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2018-12-05 17:22:11 +0800
committer最萌小汐 <sumneko@hotmail.com>2018-12-05 17:22:11 +0800
commit3221c785e48107a6199fa601f3f44ffaf8d6e8f9 (patch)
treef892906f63ddbcc5212400d12b1e59bd3534f6d0 /server/src/method/textDocument/references.lua
parent34c2d6ac30f9d990d4008f52c6436aae68fb2844 (diff)
downloadlua-language-server-3221c785e48107a6199fa601f3f44ffaf8d6e8f9.zip
查找引用
Diffstat (limited to 'server/src/method/textDocument/references.lua')
-rw-r--r--server/src/method/textDocument/references.lua47
1 files changed, 47 insertions, 0 deletions
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