diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2020-05-29 16:16:13 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2020-05-29 16:16:13 +0800 |
commit | 59afbeaed024d2610d4f514d9c1255e3394008c9 (patch) | |
tree | 1caaaf1548ac586c19e732031414c3c3dbac5ed0 /script-beta/core/definition.lua | |
parent | 532e77ce9965c7aebd02c906297990ad48a19449 (diff) | |
download | lua-language-server-59afbeaed024d2610d4f514d9c1255e3394008c9.zip |
接入部分
Diffstat (limited to 'script-beta/core/definition.lua')
-rw-r--r-- | script-beta/core/definition.lua | 110 |
1 files changed, 54 insertions, 56 deletions
diff --git a/script-beta/core/definition.lua b/script-beta/core/definition.lua index ded2fb1c..02383ee6 100644 --- a/script-beta/core/definition.lua +++ b/script-beta/core/definition.lua @@ -3,72 +3,60 @@ local workspace = require 'workspace' local files = require 'files' local vm = require 'vm' -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 - vm.eachDef(source, function (src) - 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) +local accept = { + ['local'] = true, + ['setlocal'] = true, + ['getlocal'] = true, + ['label'] = true, + ['goto'] = true, + ['field'] = true, + ['method'] = true, + ['setindex'] = true, + ['getindex'] = true, + ['tableindex'] = true, + ['setglobal'] = true, + ['getglobal'] = true, +} + +local function findSource(ast, offset) + local len = 999 + local result + guide.eachSourceContain(ast.ast, offset, function (source) + if source.finish - source.start < len and accept[source.type] then + result = source + len = source.finish - source.start end end) + return result end -local function checkRequire(source, offset, callback) +local function checkRequire(source, offset) if source.type ~= 'call' then - return + return nil end local func = source.node local pathSource = source.args and source.args[1] if not pathSource then - return + return nil end if not guide.isContain(pathSource, offset) then - return + return nil end local literal = guide.getLiteral(pathSource) if type(literal) ~= 'string' then - return + return nil end local lib = vm.getLibrary(func) if not lib then - return + return nil end if lib.name == 'require' then - local result = workspace.findUrisByRequirePath(literal, true) - for _, uri in ipairs(result) do - callback(uri) - end + return workspace.findUrisByRequirePath(literal, true) elseif lib.name == 'dofile' or lib.name == 'loadfile' then - local result = workspace.findUrisByFilePath(literal, true) - for _, uri in ipairs(result) do - callback(uri) - end + return workspace.findUrisByFilePath(literal, true) end + return nil end return function (uri, offset) @@ -76,10 +64,17 @@ return function (uri, offset) if not ast then return nil end + + local source = findSource(ast, offset) + if not source then + return nil + end + local results = {} - guide.eachSourceContain(ast.ast, offset, function (source) - checkRequire(source, offset, function (uri) - results[#results+1] = { + local uris = checkRequire(source) + if uris then + for i, uri in ipairs(uris) do + results[#uris+1] = { uri = files.getOriginUri(uri), source = source, target = { @@ -87,15 +82,18 @@ return function (uri, offset) finish = 0, } } - end) - findDef(source, function (target, uri) - results[#results+1] = { - target = target, - uri = files.getOriginUri(uri), - source = source, - } - end) - end) + end + end + + local defs = guide.requestDefinition(source) + for _, src in ipairs(defs) do + results[#results+1] = { + target = src, + uri = files.getOriginUri(uri), + source = source, + } + end + if #results == 0 then return nil end |