summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--script/utility.lua2
-rw-r--r--script/vm/ref.lua51
-rw-r--r--test/crossfile/allreferences.lua216
-rw-r--r--test/crossfile/init.lua1
-rw-r--r--test/crossfile/references.lua222
5 files changed, 248 insertions, 244 deletions
diff --git a/script/utility.lua b/script/utility.lua
index 1d207350..41229f53 100644
--- a/script/utility.lua
+++ b/script/utility.lua
@@ -687,7 +687,7 @@ function switchMT:case(name)
return self
end
----@param callback fun(...):...
+---@param callback async fun(...):...
---@return switch
function switchMT:call(callback)
for i = 1, #self.cachedCases do
diff --git a/script/vm/ref.lua b/script/vm/ref.lua
index b679ae4a..3f9262be 100644
--- a/script/vm/ref.lua
+++ b/script/vm/ref.lua
@@ -7,6 +7,7 @@ local localID = require 'vm.local-id'
local globalMgr = require 'vm.global-manager'
local nodeMgr = require 'vm.node'
local files = require 'files'
+local await = require 'await'
local simpleSwitch
@@ -14,6 +15,7 @@ local function searchGetLocal(source, node, pushResult)
local key = guide.getKeyName(source)
for _, ref in ipairs(node.node.ref) do
if ref.type == 'getlocal'
+ and ref.next
and not guide.isSet(ref.next)
and guide.getKeyName(ref.next) == key then
pushResult(ref.next)
@@ -79,6 +81,7 @@ simpleSwitch = util.switch()
end
end)
+---@async
local function searchField(source, pushResult)
local key = guide.getKeyName(source)
@@ -94,6 +97,7 @@ local function searchField(source, pushResult)
local pat = '[:.]%s*' .. key
+ ---@async
local function findWord(uri)
local text = files.getText(uri)
if not text then
@@ -106,19 +110,25 @@ local function searchField(source, pushResult)
if not state then
return
end
+ ---@async
guide.eachSourceType(state.ast, 'getfield', function (src)
if src.field[1] == key then
checkDef(src)
+ await.delay()
end
end)
+ ---@async
guide.eachSourceType(state.ast, 'getmethod', function (src)
if src.method[1] == key then
checkDef(src)
+ await.delay()
end
end)
+ ---@async
guide.eachSourceType(state.ast, 'getindex', function (src)
if src.index.type == 'string' and src.index[1] == key then
checkDef(src)
+ await.delay()
end
end)
end
@@ -130,6 +140,38 @@ local function searchField(source, pushResult)
end
end
+---@async
+local function searchFunction(source, pushResult)
+ ---@param src parser.object
+ local function checkDef(src)
+ for _, def in ipairs(vm.getDefs(src)) do
+ if def == source then
+ pushResult(src)
+ return
+ end
+ end
+ end
+
+ ---@async
+ local function findCall(uri)
+ local state = files.getState(uri)
+ if not state then
+ return
+ end
+ ---@async
+ guide.eachSourceType(state.ast, 'call', function (src)
+ checkDef(src.node)
+ await.delay()
+ end)
+ end
+
+ for uri in files.eachFile(guide.getUri(source)) do
+ if not vm.isMetaFile(uri) then
+ findCall(uri)
+ end
+ end
+end
+
local searchByParentNode
local nodeSwitch = util.switch()
: case 'field'
@@ -143,6 +185,7 @@ local nodeSwitch = util.switch()
: case 'setmethod'
: case 'getindex'
: case 'setindex'
+ ---@async
: call(function (source, pushResult)
local key = guide.getKeyName(source)
if type(key) ~= 'string' then
@@ -158,9 +201,16 @@ local nodeSwitch = util.switch()
end)
: case 'tablefield'
: case 'tableindex'
+ ---@async
: call(function (source, pushResult)
searchField(source, pushResult)
end)
+ : case 'function'
+ : case 'doc.type.function'
+ ---@async
+ : call(function (source, pushResult)
+ searchFunction(source, pushResult)
+ end)
---@param source parser.object
---@param pushResult fun(src: parser.object)
@@ -202,6 +252,7 @@ local function searchByNode(source, pushResult)
end
end
+---@async
function vm.getRefs(source)
local results = {}
local mark = {}
diff --git a/test/crossfile/allreferences.lua b/test/crossfile/allreferences.lua
deleted file mode 100644
index 3507e094..00000000
--- a/test/crossfile/allreferences.lua
+++ /dev/null
@@ -1,216 +0,0 @@
-local config = require 'config'
-
-TEST {
- {
- path = 'a.lua',
- content = [[
- local <!f!> = require 'lib'
- ]],
- },
- {
- path = 'lib.lua',
- content = [[
- return <!<?function?> ()
- end!>
- ]],
- },
-}
-
-TEST {
- {
- path = 'a.lua',
- content = [[
- local m = {}
- function m.<~func~>()
- end
- return m
- ]],
- },
- {
- path = 'b.lua',
- content = [[
- local t = require 'a'
- t.<!func!>()
- ]],
- },
-}
-
-TEST {
- {
- path = 'a.lua',
- content = [[
- return <~function () end~>
- ]],
- },
- {
- path = 'b.lua',
- content = [[
- local t = require 'a'
- ]],
- },
- {
- path = 'b.lua',
- content = [[
- local t = require 'a'
- ]],
- },
- {
- path = 'b.lua',
- content = [[
- local t = require 'a'
- ]],
- },
- {
- path = 'b.lua',
- content = [[
- local <!t!> = require 'a'
- ]],
- },
-}
-
-TEST {
- {
- path = 'a.lua',
- content = [[
- local function <~f~>()
- end
-
- return {
- <!f!> = <!f!>,
- }
- ]]
- },
- {
- path = 'b.lua',
- content = [[
- local t = require 'a'
- local <!f!> = t.<!f!>
-
- <!f!>()
-
- return {
- <!f!> = <!f!>,
- }
- ]]
- }
-}
-
-TEST {
- {
- path = 'a.lua',
- content = [[
- local function <!f!>()
- end
-
- return {
- <!f!> = <!f!>,
- }
- ]]
- },
- {
- path = 'c.lua',
- content = [[
- local t = require 'a'
- local <!f!> = t.<!f!>
-
- <!f!>()
-
- return {
- <!f!> = <!f!>,
- }
- ]]
- },
- {
- path = 'b.lua',
- content = [[
- local t = require 'a'
- local <!f!> = t.<!f!>
-
- <~f~>()
-
- return {
- <!f!> = <!f!>,
- }
- ]]
- }
-}
-
-TEST {
- {
- path = 'a.lua',
- content = [[
- local function <~f~>()
- end
-
- return {
- <!f!> = <!f!>,
- }
- ]]
- },
- {
- path = 'b1.lua',
- content = [[
- local t = require 'a'
- t.<!f!>()
- ]]
- },
- {
- path = 'b2.lua',
- content = [[
- local t = require 'a'
- t.<!f!>()
- ]]
- },
- {
- path = 'b3.lua',
- content = [[
- local t = require 'a'
- t.<!f!>()
- ]]
- },
- {
- path = 'b4.lua',
- content = [[
- local t = require 'a'
- t.<!f!>()
- ]]
- },
- {
- path = 'b5.lua',
- content = [[
- local t = require 'a'
- t.<!f!>()
- ]]
- },
- {
- path = 'b6.lua',
- content = [[
- local t = require 'a'
- t.<!f!>()
- ]]
- },
- {
- path = 'b7.lua',
- content = [[
- local t = require 'a'
- t.<!f!>()
- ]]
- },
-}
-
-TEST {
- {
- path = 'a.lua',
- content = [[
- local <~t~> = require 'b'
- return <!t!>
- ]]
- },
- {
- path = 'b.lua',
- content = [[
- local <!t!> = require 'a'
- return <!t!>
- ]]
- },
-}
diff --git a/test/crossfile/init.lua b/test/crossfile/init.lua
index 1ed2a943..aec9a044 100644
--- a/test/crossfile/init.lua
+++ b/test/crossfile/init.lua
@@ -1,6 +1,5 @@
require 'crossfile.definition'
require 'crossfile.references'
-require 'crossfile.allreferences'
require 'crossfile.hover'
require 'crossfile.completion'
require 'crossfile.diagnostic'
diff --git a/test/crossfile/references.lua b/test/crossfile/references.lua
index 18e9c5f9..9e55c77a 100644
--- a/test/crossfile/references.lua
+++ b/test/crossfile/references.lua
@@ -68,7 +68,7 @@ function TEST(datas)
}
end
end
- if catched['?'] or catched['~'] then
+ if #catched['?'] > 0 or #catched['~'] > 0 then
sourceList = catched['?'] + catched['~']
sourceUri = uri
end
@@ -100,16 +100,15 @@ end
TEST {
{
- path = 'lib.lua',
+ path = 'a.lua',
content = [[
- return <!function ()
- end!>
+ ROOT = 1
]],
},
{
- path = 'a.lua',
+ path = 'b.lua',
content = [[
- local <~f~> = require 'lib'
+ print(<~ROOT~>)
]],
},
}
@@ -118,13 +117,13 @@ TEST {
{
path = 'a.lua',
content = [[
- <!ROOT!> = 1
+ <?ROOT?> = 1
]],
},
{
path = 'b.lua',
content = [[
- print(<~ROOT~>)
+ print(<!ROOT!>)
]],
},
}
@@ -133,15 +132,41 @@ TEST {
{
path = 'a.lua',
content = [[
- <~ROOT~> = 1
- ]],
+ ---@type A
+ local t
+
+ t.<!f!>()
+ ]]
},
{
path = 'b.lua',
content = [[
- print(<!ROOT!>)
- ]],
+ ---@class A
+ local mt
+
+ function mt.<?f?>()
+ end
+ ]]
+ }
+}
+
+TEST {
+ {
+ path = 'a.lua',
+ content = [[
+ local t = {}
+ t.<?x?> = 1
+ return t
+ ]]
},
+ {
+ path = 'b.lua',
+ content = [[
+ local t = require 'a'
+
+ print(t.<!x!>)
+ ]]
+ }
}
TEST {
@@ -149,14 +174,13 @@ TEST {
path = 'a.lua',
content = [[
local f = require 'lib'
- local <~o~> = f()
+ <!f!>()
]],
},
{
path = 'lib.lua',
content = [[
- return function ()
- return <!{}!>
+ return <?function?> ()
end
]],
},
@@ -166,20 +190,78 @@ TEST {
{
path = 'a.lua',
content = [[
- ---@type A
- local t
+ local m = {}
+ function m.<?func?>()
+ end
+ return m
+ ]],
+ },
+ {
+ path = 'b.lua',
+ content = [[
+ local t = require 'a'
+ t.<!func!>()
+ ]],
+ },
+}
- t.<!f!>()
- ]]
+TEST {
+ {
+ path = 'a.lua',
+ content = [[
+ return <?function?> () end
+ ]],
},
{
path = 'b.lua',
content = [[
- ---@class A
- local mt
+ local t = require 'a'
+ ]],
+ },
+ {
+ path = 'b.lua',
+ content = [[
+ local t = require 'a'
+ ]],
+ },
+ {
+ path = 'b.lua',
+ content = [[
+ local t = require 'a'
+ ]],
+ },
+ {
+ path = 'b.lua',
+ content = [[
+ local t = require 'a'
+ <!t!>()
+ ]],
+ },
+}
- function mt.<~f~>()
+TEST {
+ {
+ path = 'a.lua',
+ content = [[
+ local function <?f?>()
end
+
+ return {
+ f = <!f!>,
+ }
+ ]]
+ },
+ {
+ path = 'b.lua',
+ content = [[
+ local t = require 'a'
+ local f = t.f
+
+ f()
+
+ return {
+ f = f,
+ }
]]
}
}
@@ -188,17 +270,105 @@ TEST {
{
path = 'a.lua',
content = [[
- local t = {}
- t.<~x~> = 1
- return t
+ local <?function?> f()
+ end
+
+ return {
+ f = f,
+ }
]]
},
{
path = 'b.lua',
content = [[
local t = require 'a'
+ local f = t.f
- print(t.<!x!>)
+ <!f!>()
+
+ return {
+ f = f,
+ }
]]
}
}
+
+TEST {
+ {
+ path = 'a.lua',
+ content = [[
+ local <?function?> f()
+ end
+
+ return {
+ f = f,
+ }
+ ]]
+ },
+ {
+ path = 'b1.lua',
+ content = [[
+ local t = require 'a'
+ t.<!f!>()
+ ]]
+ },
+ {
+ path = 'b2.lua',
+ content = [[
+ local t = require 'a'
+ t.<!f!>()
+ ]]
+ },
+ {
+ path = 'b3.lua',
+ content = [[
+ local t = require 'a'
+ t.<!f!>()
+ ]]
+ },
+ {
+ path = 'b4.lua',
+ content = [[
+ local t = require 'a'
+ t.<!f!>()
+ ]]
+ },
+ {
+ path = 'b5.lua',
+ content = [[
+ local t = require 'a'
+ t.<!f!>()
+ ]]
+ },
+ {
+ path = 'b6.lua',
+ content = [[
+ local t = require 'a'
+ t.<!f!>()
+ ]]
+ },
+ {
+ path = 'b7.lua',
+ content = [[
+ local t = require 'a'
+ t.<!f!>()
+ ]]
+ },
+}
+
+TEST {
+ {
+ path = 'a.lua',
+ content = [[
+ local <?t?> = require 'b'
+ return <!t!>
+ ]]
+ },
+ {
+ path = 'b.lua',
+ content = [[
+ local t = require 'a'
+ return t
+ ]]
+ },
+}