summaryrefslogtreecommitdiff
path: root/script/method/textDocument/definition.lua
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2019-11-23 00:05:30 +0800
committer最萌小汐 <sumneko@hotmail.com>2019-11-23 00:05:30 +0800
commit6da2b175e20ed3c03b0dfcfc9046de1e0e5d4444 (patch)
treefdc22d78150fd1c5edc46732c8b151ccfefb519f /script/method/textDocument/definition.lua
parentd0ff66c9abe9d6abbca12fd811e0c3cb69c1033a (diff)
downloadlua-language-server-6da2b175e20ed3c03b0dfcfc9046de1e0e5d4444.zip
正路目录
Diffstat (limited to 'script/method/textDocument/definition.lua')
-rw-r--r--script/method/textDocument/definition.lua88
1 files changed, 88 insertions, 0 deletions
diff --git a/script/method/textDocument/definition.lua b/script/method/textDocument/definition.lua
new file mode 100644
index 00000000..dbf9e41c
--- /dev/null
+++ b/script/method/textDocument/definition.lua
@@ -0,0 +1,88 @@
+local core = require 'core'
+
+local function findResult(lsp, uri, position)
+ local vm = lsp:getVM(uri)
+
+ local positions, isGlobal = core.definition(vm, position, 'definition')
+ if not positions then
+ return nil, isGlobal
+ end
+
+ local locations = {}
+ for i, position in ipairs(positions) do
+ local start, finish, valueUri = position[1], position[2], (position[3] or uri)
+ local vm, valueLines = lsp:getVM(valueUri)
+ if valueLines then
+ local start_row, start_col = valueLines:rowcol(start)
+ local finish_row, finish_col = valueLines:rowcol(finish)
+ locations[#locations+1] = {
+ uri = valueUri,
+ range = {
+ start = {
+ line = start_row - 1,
+ character = start_col - 1,
+ },
+ ['end'] = {
+ line = finish_row - 1,
+ -- 这里不用-1,因为前端期待的是匹配完成后的位置
+ character = finish_col,
+ },
+ }
+ }
+ elseif vm then
+ locations[#locations+1] = {
+ uri = valueUri,
+ range = {
+ start = {
+ line = 0,
+ character = 0,
+ },
+ ['end'] = {
+ line = 0,
+ character = 0,
+ },
+ }
+ }
+ end
+ end
+
+ if #locations == 0 then
+ return nil, isGlobal
+ end
+
+ return locations, isGlobal
+end
+
+local LastTask
+
+---@param lsp LSP
+---@param params table
+return function (lsp, params)
+ local uri = params.textDocument.uri
+ local vm, lines = lsp:loadVM(uri)
+ if not vm then
+ return nil
+ end
+
+ if LastTask then
+ LastTask:remove()
+ LastTask = nil
+ end
+
+ -- lua是从1开始的,因此都要+1
+ local position = lines:positionAsChar(params.position.line + 1, params.position.character)
+
+ return function (response)
+ local clock = os.clock()
+ LastTask = ac.loop(0.1, function ()
+ local result, isGlobal = findResult(lsp, uri, position)
+ if isGlobal and lsp:isWaitingCompile() and os.clock() - clock < 1 then
+ return
+ end
+ response(result)
+ LastTask:remove()
+ LastTask = nil
+ end)
+ LastTask:onTimer()
+ end
+end