summaryrefslogtreecommitdiff
path: root/server-beta/src/core
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2019-11-10 22:20:17 +0800
committer最萌小汐 <sumneko@hotmail.com>2019-11-10 22:20:17 +0800
commit4cc750f1c150930558bb721b5a73948b33549e42 (patch)
treec689710cd1faf57fcbdeba1bf3c668170fcc7a6d /server-beta/src/core
parent5c3b733fd7ff097d576a618c78b48f75dcace49e (diff)
downloadlua-language-server-4cc750f1c150930558bb721b5a73948b33549e42.zip
查找引用
Diffstat (limited to 'server-beta/src/core')
-rw-r--r--server-beta/src/core/reference.lua65
1 files changed, 65 insertions, 0 deletions
diff --git a/server-beta/src/core/reference.lua b/server-beta/src/core/reference.lua
new file mode 100644
index 00000000..96d1b635
--- /dev/null
+++ b/server-beta/src/core/reference.lua
@@ -0,0 +1,65 @@
+local guide = require 'parser.guide'
+local workspace = require 'workspace'
+local files = require 'files'
+local searcher = require 'searcher'
+
+local function findDef(source, callback)
+ if source.type ~= 'local'
+ and source.type ~= 'getlocal'
+ and source.type ~= 'setlocal'
+ and source.type ~= 'setglobal'
+ and source.type ~= 'getglobal'
+ and source.type ~= 'field'
+ and source.type ~= 'method'
+ and source.type ~= 'string'
+ and source.type ~= 'number'
+ and source.type ~= 'boolean'
+ and source.type ~= 'goto' then
+ return
+ end
+ searcher.eachRef(source, function (info)
+ if info.mode == 'declare'
+ or info.mode == 'set'
+ or info.mode == 'get'
+ or info.mode == 'return' then
+ local src = info.source
+ local root = guide.getRoot(src)
+ local uri = root.uri
+ if src.type == 'setfield'
+ or src.type == 'getfield'
+ or src.type == 'tablefield' then
+ callback(src.field, uri)
+ elseif src.type == 'setindex'
+ or src.type == 'getindex'
+ or src.type == 'tableindex' then
+ callback(src.index, uri)
+ elseif src.type == 'getmethod'
+ or src.type == 'setmethod' then
+ callback(src.method, uri)
+ else
+ callback(src, uri)
+ end
+ end
+ end)
+end
+
+return function (uri, offset)
+ local ast = files.getAst(uri)
+ if not ast then
+ return nil
+ end
+ local results = {}
+ guide.eachSourceContain(ast.ast, offset, function (source)
+ findDef(source, function (target, uri)
+ results[#results+1] = {
+ target = target,
+ uri = files.getOriginUri(uri),
+ source = source,
+ }
+ end)
+ end)
+ if #results == 0 then
+ return nil
+ end
+ return results
+end