diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2019-11-10 22:20:17 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2019-11-10 22:20:17 +0800 |
commit | 4cc750f1c150930558bb721b5a73948b33549e42 (patch) | |
tree | c689710cd1faf57fcbdeba1bf3c668170fcc7a6d | |
parent | 5c3b733fd7ff097d576a618c78b48f75dcace49e (diff) | |
download | lua-language-server-4cc750f1c150930558bb721b5a73948b33549e42.zip |
查找引用
-rw-r--r-- | server-beta/src/core/reference.lua | 65 | ||||
-rw-r--r-- | server-beta/src/provider/diagnostic.lua | 16 | ||||
-rw-r--r-- | server-beta/src/searcher/eachRef.lua | 15 | ||||
-rw-r--r-- | server-beta/test.lua | 2 | ||||
-rw-r--r-- | server-beta/test/references/init.lua | 41 |
5 files changed, 108 insertions, 31 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 diff --git a/server-beta/src/provider/diagnostic.lua b/server-beta/src/provider/diagnostic.lua index ba9f1caa..74c6d87a 100644 --- a/server-beta/src/provider/diagnostic.lua +++ b/server-beta/src/provider/diagnostic.lua @@ -156,13 +156,17 @@ end function m.refresh(uri) m.version = m.version + 1 local myVersion = m.version - if uri then - m.doDiagnostic(uri, true) - end - if not m._start then - return - end await.create(function () + await.delay() + if myVersion ~= m.version then + return + end + if uri then + m.doDiagnostic(uri, true) + end + if not m._start then + return + end await.sleep(0.2) if myVersion ~= m.version then return diff --git a/server-beta/src/searcher/eachRef.lua b/server-beta/src/searcher/eachRef.lua index b1023f52..7f43cbac 100644 --- a/server-beta/src/searcher/eachRef.lua +++ b/server-beta/src/searcher/eachRef.lua @@ -75,10 +75,17 @@ local function ofValue(value, callback) return end - callback { - source = value, - mode = 'value', - } + if value.type == 'table' + or value.type == 'string' + or value.type == 'number' + or value.type == 'boolean' + or value.type == 'nil' + or value.type == 'function' then + callback { + source = value, + mode = 'value', + } + end searcher.eachRef(value, callback) diff --git a/server-beta/test.lua b/server-beta/test.lua index 3ebfca70..ad282687 100644 --- a/server-beta/test.lua +++ b/server-beta/test.lua @@ -38,7 +38,7 @@ local function main() test 'definition' test 'diagnostics' - --test 'references' + test 'references' --test 'highlight' --test 'rename' --test 'type_inference' diff --git a/server-beta/test/references/init.lua b/server-beta/test/references/init.lua index 44cd5ab0..6f0f74fc 100644 --- a/server-beta/test/references/init.lua +++ b/server-beta/test/references/init.lua @@ -1,6 +1,5 @@ -local core = require 'core' -local parser = require 'parser' -local buildVM = require 'vm' +local core = require 'core.reference' +local files = require 'files' local function catch_target(script) local list = {} @@ -33,18 +32,20 @@ local function founded(targets, results) end function TEST(script) + files.removeAll() local target = catch_target(script) local start = script:find('<?', 1, true) local finish = script:find('?>', 1, true) local pos = (start + finish) // 2 + 1 local new_script = script:gsub('<[!?]', ' '):gsub('[!?]>', ' ') - local ast = parser:parse(new_script, 'lua', 'Lua 5.3') - assert(ast) - local vm = buildVM(ast) - assert(vm) + files.setText('', new_script) - local positions = core.definition(vm, pos, 'reference') - if positions then + local results = core('', pos) + if results then + local positions = {} + for i, result in ipairs(results) do + positions[i] = { result.target.start, result.target.finish } + end assert(founded(target, positions)) else assert(#target == 0) @@ -101,14 +102,14 @@ function table.<?dump?>() end ]] -TEST [[ ----@class <!Class!> ----@type <?Class?> ----@type <!Class!> -]] - -TEST [[ ----@class <?Class?> ----@type <!Class!> ----@type <!Class!> -]] +--TEST [[ +-----@class <!Class!> +-----@type <?Class?> +-----@type <!Class!> +--]] +-- +--TEST [[ +-----@class <?Class?> +-----@type <!Class!> +-----@type <!Class!> +--]] |