summaryrefslogtreecommitdiff
path: root/server-beta/src/core
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2019-10-22 17:39:47 +0800
committer最萌小汐 <sumneko@hotmail.com>2019-10-22 17:39:47 +0800
commite92ea4b611e8291a2b6ae6904147917179140fae (patch)
tree4bc372c345d58ba78ee14365f3a296cd48dcbb66 /server-beta/src/core
parent4b3d9412560935d11e63487d6be7d3bfb8b11b91 (diff)
downloadlua-language-server-e92ea4b611e8291a2b6ae6904147917179140fae.zip
require路径跳转
Diffstat (limited to 'server-beta/src/core')
-rw-r--r--server-beta/src/core/definition.lua80
-rw-r--r--server-beta/src/core/engineer.lua3
2 files changed, 68 insertions, 15 deletions
diff --git a/server-beta/src/core/definition.lua b/server-beta/src/core/definition.lua
index e0589752..2fe726e0 100644
--- a/server-beta/src/core/definition.lua
+++ b/server-beta/src/core/definition.lua
@@ -1,23 +1,73 @@
-local guide = require 'parser.guide'
-local engineer = require 'core.engineer'
+local guide = require 'parser.guide'
+local engineer = require 'core.engineer'
+local workspace = require 'workspace'
+
+local function findDef(searcher, source, callback)
+ searcher:eachDef(source, function (src)
+ if src.type == 'setfield'
+ or src.type == 'getfield'
+ or src.type == 'tablefield' then
+ callback(src.field)
+ elseif src.type == 'setindex'
+ or src.type == 'getindex'
+ or src.type == 'tableindex' then
+ callback(src.index)
+ elseif src.type == 'getmethod'
+ or src.type == 'setmethod' then
+ callback(src.method)
+ else
+ callback(src)
+ end
+ end)
+end
+
+---@param searcher engineer
+local function checkRequire(searcher, source, offset, callback)
+ if source.type ~= 'call' then
+ return
+ end
+ local func = source.node
+ local pathSource = source.args and source.args[1]
+ if not pathSource then
+ return
+ end
+ if not guide.isContain(pathSource, offset) then
+ return
+ end
+ local literal = guide.getLiteral(pathSource)
+ if type(literal) ~= 'string' then
+ return
+ end
+ local name = searcher:getSpecialName(func)
+ if name == 'require' then
+ local result = workspace.findUrisByRequirePath(literal, true)
+ for _, uri in ipairs(result) do
+ callback(uri)
+ end
+ elseif name == 'dofile'
+ or name == 'loadfile' then
+ local result = workspace.findUrisByFilePath(literal, true)
+ for _, uri in ipairs(result) do
+ callback(uri)
+ end
+ end
+end
return function (ast, offset)
local results = {}
local searcher = engineer(ast)
guide.eachSourceContain(ast.ast, offset, function (source)
- searcher:eachDef(source, function (src)
- if src.type == 'setfield'
- or src.type == 'getfield'
- or src.type == 'tablefield' then
- src = src.field
- elseif src.type == 'setindex'
- or src.type == 'getindex'
- or src.type == 'tableindex' then
- src = src.index
- elseif src.type == 'getmethod'
- or src.type == 'setmethod' then
- src = src.method
- end
+ checkRequire(searcher, source, offset, function (uri)
+ results[#results+1] = {
+ uri = uri,
+ source = source,
+ target = {
+ start = 0,
+ finish = 0,
+ }
+ }
+ end)
+ findDef(searcher, source, function (src)
results[#results+1] = {
uri = ast.uri,
source = source,
diff --git a/server-beta/src/core/engineer.lua b/server-beta/src/core/engineer.lua
index 656c29bf..a52bddc2 100644
--- a/server-beta/src/core/engineer.lua
+++ b/server-beta/src/core/engineer.lua
@@ -39,6 +39,9 @@ local specials = {
['rawset'] = true,
['rawget'] = true,
['setmetatable'] = true,
+ ['require'] = true,
+ ['dofile'] = true,
+ ['loadfile'] = true,
}
function mt:getSpecialName(source)