summaryrefslogtreecommitdiff
path: root/script-beta/test/crossfile
diff options
context:
space:
mode:
Diffstat (limited to 'script-beta/test/crossfile')
-rw-r--r--script-beta/test/crossfile/completion.lua621
-rw-r--r--script-beta/test/crossfile/definition.lua448
-rw-r--r--script-beta/test/crossfile/hover.lua347
-rw-r--r--script-beta/test/crossfile/init.lua4
-rw-r--r--script-beta/test/crossfile/references.lua249
5 files changed, 1669 insertions, 0 deletions
diff --git a/script-beta/test/crossfile/completion.lua b/script-beta/test/crossfile/completion.lua
new file mode 100644
index 00000000..cb9f2f65
--- /dev/null
+++ b/script-beta/test/crossfile/completion.lua
@@ -0,0 +1,621 @@
+local service = require 'service'
+local workspace = require 'workspace'
+local fs = require 'bee.filesystem'
+local core = require 'core'
+local uric = require 'uri'
+
+rawset(_G, 'TEST', true)
+
+local CompletionItemKind = {
+ Text = 1,
+ Method = 2,
+ Function = 3,
+ Constructor = 4,
+ Field = 5,
+ Variable = 6,
+ Class = 7,
+ Interface = 8,
+ Module = 9,
+ Property = 10,
+ Unit = 11,
+ Value = 12,
+ Enum = 13,
+ Keyword = 14,
+ Snippet = 15,
+ Color = 16,
+ File = 17,
+ Reference = 18,
+ Folder = 19,
+ EnumMember = 20,
+ Constant = 21,
+ Struct = 22,
+ Event = 23,
+ Operator = 24,
+ TypeParameter = 25,
+}
+
+local EXISTS = {}
+
+local function eq(a, b)
+ if a == EXISTS and b ~= nil then
+ return true
+ end
+ local tp1, tp2 = type(a), type(b)
+ if tp1 ~= tp2 then
+ return false
+ end
+ if tp1 == 'table' then
+ local mark = {}
+ for k in pairs(a) do
+ if not eq(a[k], b[k]) then
+ return false
+ end
+ mark[k] = true
+ end
+ for k in pairs(b) do
+ if not mark[k] then
+ return false
+ end
+ end
+ return true
+ end
+ return a == b
+end
+
+function TEST(data)
+ local lsp = service()
+ local ws = workspace(lsp, 'test')
+ lsp.workspace = ws
+ ws.root = ROOT
+
+ local mainUri
+ local mainBuf
+ local pos
+ for _, info in ipairs(data) do
+ local uri = uric.encode(fs.path(info.path))
+ local script = info.content
+ if info.main then
+ pos = script:find('$', 1, true) - 1
+ script = script:gsub('%$', '')
+ mainUri = uri
+ mainBuf = script
+ end
+ lsp:saveText(uri, 1, script)
+ ws:addFile(uric.decode(uri))
+
+ while lsp._needCompile[1] do
+ lsp:compileVM(lsp._needCompile[1])
+ end
+ end
+
+ local vm = lsp:loadVM(mainUri)
+ assert(vm)
+ local result = core.completion(vm, mainBuf, pos)
+ local expect = data.completion
+ if expect then
+ assert(result)
+ assert(eq(expect, result))
+ else
+ assert(result == nil)
+ end
+end
+
+if require'bee.platform'.OS == 'Windows' then
+
+TEST {
+ {
+ path = 'abc.lua',
+ content = '',
+ },
+ {
+ path = 'abc/aaa.lua',
+ content = '',
+ },
+ {
+ path = 'xxx/abcde.lua',
+ content = '',
+ },
+ {
+ path = 'test.lua',
+ content = 'require "a$"',
+ main = true,
+ },
+ completion = {
+ {
+ label = 'abc',
+ filterText = 'abc',
+ kind = CompletionItemKind.Reference,
+ documentation = 'abc.lua',
+ textEdit = EXISTS,
+ additionalTextEdits = EXISTS,
+ },
+ {
+ label = 'abc.aaa',
+ filterText = 'abc.aaa',
+ kind = CompletionItemKind.Reference,
+ documentation = 'abc/aaa.lua',
+ textEdit = EXISTS,
+ additionalTextEdits = EXISTS,
+ },
+ {
+ label = 'abcde',
+ filterText = 'abcde',
+ kind = CompletionItemKind.Reference,
+ documentation = 'xxx/abcde.lua',
+ textEdit = EXISTS,
+ additionalTextEdits = EXISTS,
+ },
+ }
+}
+
+TEST {
+ {
+ path = 'abc.lua',
+ content = '',
+ },
+ {
+ path = 'test.lua',
+ content = 'require "A$"',
+ main = true,
+ },
+ completion = {
+ {
+ label = 'abc',
+ filterText = 'abc',
+ kind = CompletionItemKind.Reference,
+ documentation = 'abc.lua',
+ textEdit = EXISTS,
+ additionalTextEdits = EXISTS,
+ },
+ }
+}
+
+TEST {
+ {
+ path = 'abc.lua',
+ content = '',
+ },
+ {
+ path = 'ABCD.lua',
+ content = '',
+ },
+ {
+ path = 'test.lua',
+ content = 'require "a$"',
+ main = true,
+ },
+ completion = {
+ {
+ label = 'ABCD',
+ filterText = 'ABCD',
+ kind = CompletionItemKind.Reference,
+ documentation = 'ABCD.lua',
+ textEdit = EXISTS,
+ additionalTextEdits = EXISTS,
+ },
+ {
+ label = 'abc',
+ filterText = 'abc',
+ kind = CompletionItemKind.Reference,
+ documentation = 'abc.lua',
+ textEdit = EXISTS,
+ additionalTextEdits = EXISTS,
+ },
+ }
+}
+
+else
+
+TEST {
+ {
+ path = 'abc.lua',
+ content = '',
+ },
+ {
+ path = 'ABCD.lua',
+ content = '',
+ },
+ {
+ path = 'test.lua',
+ content = 'require "a$"',
+ main = true,
+ },
+ completion = {
+ {
+ label = 'abc',
+ filterText = 'abc',
+ kind = CompletionItemKind.Reference,
+ documentation = 'abc.lua',
+ textEdit = EXISTS,
+ additionalTextEdits = EXISTS,
+ },
+ }
+}
+
+end
+
+TEST {
+ {
+ path = 'abc.lua',
+ content = '',
+ },
+ {
+ path = 'abc/init.lua',
+ content = '',
+ },
+ {
+ path = 'test.lua',
+ content = 'require "abc$"',
+ main = true,
+ },
+ completion = {
+ {
+ label = 'abc',
+ filterText = 'abc',
+ kind = CompletionItemKind.Reference,
+ documentation = 'abc.lua',
+ textEdit = EXISTS,
+ additionalTextEdits = EXISTS,
+ },
+ {
+ label = 'abc.init',
+ filterText = 'abc.init',
+ kind = CompletionItemKind.Reference,
+ documentation = 'abc/init.lua',
+ textEdit = EXISTS,
+ additionalTextEdits = EXISTS,
+ },
+ }
+}
+
+TEST {
+ {
+ path = 'abc/init.lua',
+ content = '',
+ },
+ {
+ path = 'abc/bbc.lua',
+ content = '',
+ },
+ {
+ path = 'test.lua',
+ content = 'require "abc$"',
+ main = true,
+ },
+ completion = {
+ {
+ label = 'abc',
+ filterText = 'abc',
+ kind = CompletionItemKind.Reference,
+ documentation = 'abc/init.lua',
+ textEdit = EXISTS,
+ additionalTextEdits = EXISTS,
+ },
+ {
+ label = 'abc.bbc',
+ filterText = 'abc.bbc',
+ kind = CompletionItemKind.Reference,
+ documentation = 'abc/bbc.lua',
+ textEdit = EXISTS,
+ additionalTextEdits = EXISTS,
+ },
+ {
+ label = 'abc.init',
+ filterText = 'abc.init',
+ kind = CompletionItemKind.Reference,
+ documentation = 'abc/init.lua',
+ textEdit = EXISTS,
+ additionalTextEdits = EXISTS,
+ },
+ }
+}
+
+TEST {
+ {
+ path = 'abc.lua',
+ content = '',
+ },
+ {
+ path = 'abc/init.lua',
+ content = '',
+ },
+ {
+ path = 'test.lua',
+ content = 'require "abc.$"',
+ main = true,
+ },
+ completion = {
+ {
+ label = 'abc.init',
+ filterText = 'abc.init',
+ kind = CompletionItemKind.Reference,
+ documentation = 'abc/init.lua',
+ textEdit = EXISTS,
+ additionalTextEdits = EXISTS,
+ },
+ }
+}
+
+TEST {
+ {
+ path = 'abc.lua',
+ content = '',
+ },
+ {
+ path = 'abc/init.lua',
+ content = '',
+ },
+ {
+ path = 'test.lua',
+ content = 'require "abc.i$"',
+ main = true,
+ },
+ completion = {
+ {
+ label = 'abc.init',
+ filterText = 'abc.init',
+ kind = CompletionItemKind.Reference,
+ documentation = 'abc/init.lua',
+ textEdit = EXISTS,
+ additionalTextEdits = EXISTS,
+ },
+ }
+}
+
+TEST {
+ {
+ path = 'core/core.lua',
+ content = '',
+ },
+ {
+ path = 'core/xxx.lua',
+ content = '',
+ },
+ {
+ path = 'test.lua',
+ content = 'require "core.co$"',
+ main = true,
+ },
+ completion = {
+ {
+ label = 'core.core',
+ filterText = 'core.core',
+ kind = CompletionItemKind.Reference,
+ documentation = 'core/core.lua',
+ textEdit = EXISTS,
+ additionalTextEdits = EXISTS,
+ },
+ }
+}
+
+TEST {
+ {
+ path = 'x000.lua',
+ content = '',
+ },
+ {
+ path = 'abc/x111.lua',
+ content = '',
+ },
+ {
+ path = 'abc/test.lua',
+ content = 'require "x$"',
+ main = true,
+ },
+ completion = {
+ {
+ label = 'x000',
+ filterText = 'x000',
+ kind = CompletionItemKind.Reference,
+ documentation = 'x000.lua',
+ textEdit = EXISTS,
+ additionalTextEdits = EXISTS,
+ },
+ {
+ label = 'x111',
+ filterText = 'x111',
+ kind = CompletionItemKind.Reference,
+ documentation = 'abc/x111.lua',
+ textEdit = EXISTS,
+ additionalTextEdits = EXISTS,
+ },
+ }
+}
+
+TEST {
+ {
+ path = 'a.lua',
+ content = [[
+ return {
+ a = 1,
+ b = 2,
+ c = 3,
+ }
+ ]]
+ },
+ {
+ path = 'b.lua',
+ content = [[
+ local t = require 'a'
+ t.$
+ ]],
+ main = true,
+ },
+ completion = {
+ {
+ label = 'a',
+ kind = CompletionItemKind.Enum,
+ detail = '(number) = 1',
+ },
+ {
+ label = 'b',
+ kind = CompletionItemKind.Enum,
+ detail = '(number) = 2',
+ },
+ {
+ label = 'c',
+ kind = CompletionItemKind.Enum,
+ detail = '(number) = 3',
+ },
+ }
+}
+
+TEST {
+ {
+ path = 'a.lua',
+ content = [[
+ zabc = 1
+ ]]
+ },
+ {
+ path = 'a.lua',
+ content = [[
+ zabcd = print
+ ]]
+ },
+ {
+ path = 'a.lua',
+ content = [[
+ zabcdef = 1
+ ]]
+ },
+ {
+ path = 'b.lua',
+ content = [[
+ zab$
+ ]],
+ main = true,
+ },
+ completion = {
+ {
+ label = 'zabcdef',
+ kind = CompletionItemKind.Enum,
+ detail = '(number) = 1',
+ },
+ }
+}
+
+TEST {
+ {
+ path = 'init.lua',
+ content = [[
+ setmetatable(_G, {__index = {}})
+ ]]
+ },
+ {
+ path = 'a.lua',
+ content = [[
+ print(zabc)
+ ]]
+ },
+ {
+ path = 'a.lua',
+ content = [[
+ zabcdef = 1
+ ]]
+ },
+ {
+ path = 'b.lua',
+ content = [[
+ zab$
+ ]],
+ main = true,
+ },
+ completion = {
+ {
+ label = 'zabcdef',
+ kind = CompletionItemKind.Enum,
+ detail = '(number) = 1',
+ },
+ }
+}
+
+TEST {
+ {
+ path = 'a.lua',
+ content = [[
+ local japi = require 'jass.japi'
+ japi.xxxaaaaxxxx
+ ]]
+ },
+ {
+ path = 'a.lua',
+ content = [[
+ local japi = require 'jass.japi'
+ japi.xxxaaaax$
+ ]],
+ main = true,
+ },
+}
+
+TEST {
+ {
+ path = 'xxx.lua',
+ content = ''
+ },
+ {
+ path = 'xxxx.lua',
+ content = [[
+ require 'xx$'
+ ]],
+ main = true,
+ },
+ completion = {
+ {
+ label = 'xxx',
+ filterText = 'xxx',
+ kind = CompletionItemKind.Reference,
+ documentation = 'xxx.lua',
+ textEdit = EXISTS,
+ additionalTextEdits = EXISTS,
+ },
+ }
+}
+
+TEST {
+ {
+ path = [[xx'xx.lua]],
+ content = ''
+ },
+ {
+ path = 'main.lua',
+ content = [[
+ require 'xx$'
+ ]],
+ main = true,
+ },
+ completion = {
+ {
+ label = [[xx'xx]],
+ filterText = [[xx'xx]],
+ kind = CompletionItemKind.Reference,
+ documentation = [[xx'xx.lua]],
+ textEdit = EXISTS,
+ additionalTextEdits = EXISTS,
+ },
+ }
+}
+
+TEST {
+ {
+ path = [[xx]=]xx.lua]],
+ content = ''
+ },
+ {
+ path = 'main.lua',
+ content = [[
+ require [=[xx$]=]'
+ ]],
+ main = true,
+ },
+ completion = {
+ {
+ label = [[xx]=]xx]],
+ filterText = [[xx]=]xx]],
+ kind = CompletionItemKind.Reference,
+ documentation = [[xx]=]xx.lua]],
+ textEdit = EXISTS,
+ additionalTextEdits = EXISTS,
+ },
+ }
+}
diff --git a/script-beta/test/crossfile/definition.lua b/script-beta/test/crossfile/definition.lua
new file mode 100644
index 00000000..7cf12c74
--- /dev/null
+++ b/script-beta/test/crossfile/definition.lua
@@ -0,0 +1,448 @@
+local files = require 'files'
+local furi = require 'file-uri'
+local core = require 'core.definition'
+
+rawset(_G, 'TEST', true)
+
+local function catch_target(script, sep)
+ local list = {}
+ local cur = 1
+ local cut = 0
+ while true do
+ local start, finish = script:find(('<%%%s.-%%%s>'):format(sep, sep), cur)
+ if not start then
+ break
+ end
+ list[#list+1] = { start - cut, finish - 4 - cut }
+ cur = finish + 1
+ cut = cut + 4
+ end
+ local new_script = script:gsub(('<%%%s(.-)%%%s>'):format(sep, sep), '%1')
+ return new_script, list
+end
+
+local function founded(targets, results)
+ if #targets ~= #results then
+ return false
+ end
+ for _, target in ipairs(targets) do
+ for _, result in ipairs(results) do
+ if target[1] == result[1]
+ and target[2] == result[2]
+ and target[3] == result[3]
+ then
+ goto NEXT
+ end
+ end
+ do return false end
+ ::NEXT::
+ end
+ return true
+end
+
+function TEST(datas)
+ files.removeAll()
+
+ local targetList = {}
+ local sourceList
+ local sourceUri
+ for i, data in ipairs(datas) do
+ local uri = furi.encode(data.path)
+ local new, list = catch_target(data.content, '!')
+ if new ~= data.content or data.target then
+ if data.target then
+ targetList[#targetList+1] = {
+ data.target[1],
+ data.target[2],
+ uri,
+ }
+ else
+ for _, position in ipairs(list) do
+ targetList[#targetList+1] = {
+ position[1],
+ position[2],
+ uri,
+ }
+ end
+ end
+ data.content = new
+ end
+ new, list = catch_target(data.content, '?')
+ if new ~= data.content then
+ sourceList = list
+ sourceUri = uri
+ data.content = new
+ end
+ files.setText(uri, data.content)
+ end
+
+ local sourcePos = (sourceList[1][1] + sourceList[1][2]) // 2
+ local positions = core(sourceUri, sourcePos)
+ if positions then
+ local result = {}
+ for i, position in ipairs(positions) do
+ result[i] = {
+ position.target.start,
+ position.target.finish,
+ position.uri,
+ }
+ end
+ assert(founded(targetList, result))
+ else
+ assert(#targetList == 0)
+ end
+end
+
+TEST {
+ {
+ path = 'a.lua',
+ content = '',
+ target = {0, 0},
+ },
+ {
+ path = 'b.lua',
+ content = 'require <?"a"?>',
+ },
+}
+
+TEST {
+ {
+ path = 'aaa/bbb.lua',
+ content = '',
+ target = {0, 0},
+ },
+ {
+ path = 'b.lua',
+ content = 'require "aaa.<?bbb?>"',
+ },
+}
+
+TEST {
+ {
+ path = 'a.lua',
+ content = 'local <!t!> = 1; return <!t!>',
+ },
+ {
+ path = 'b.lua',
+ content = 'local <?t?> = require "a"',
+ target = {7, 7},
+ },
+}
+
+if require 'bee.platform'.OS == 'Windows' then
+TEST {
+ {
+ path = 'a.lua',
+ content = '',
+ target = {0, 0},
+ },
+ {
+ path = 'b.lua',
+ content = 'require <?"A"?>',
+ },
+}
+end
+
+TEST {
+ {
+ path = 'a.lua',
+ content = 'local <!t!> = 1; return <!t!>',
+ },
+ {
+ path = 'b.lua',
+ content = 'local <?t?> = require "a"',
+ target = {7, 7},
+ },
+}
+
+TEST {
+ {
+ path = 'a.lua',
+ content = [[
+ local t = {
+ <!x!> = 1,
+ }
+ return t
+ ]],
+ },
+ {
+ path = 'b.lua',
+ content = [[
+ local t = require "a"
+ t.<?x?>()
+ ]],
+ },
+}
+
+TEST {
+ {
+ path = 'a.lua',
+ content = [[
+ return {
+ <!x!> = 1,
+ }
+ ]],
+ },
+ {
+ path = 'b.lua',
+ content = [[
+ local t = require "a"
+ t.<?x?>()
+ ]],
+ },
+}
+
+TEST {
+ {
+ path = 'a.lua',
+ content = [[
+ return <!function ()
+ end!>
+ ]],
+ },
+ {
+ path = 'b.lua',
+ content = [[
+ local <!f!> = require "a"
+ <?f?>()
+ ]],
+ },
+}
+
+TEST {
+ {
+ path = 'a.lua',
+ content = [[
+ return <!a():b():c()!>
+ ]],
+ },
+ {
+ path = 'b.lua',
+ content = [[
+ local <?t?> = require 'a'
+ ]],
+ target = {19, 19},
+ },
+}
+
+TEST {
+ {
+ path = 'a.lua',
+ content = [[
+ <!global!> = 1
+ ]],
+ },
+ {
+ path = 'b.lua',
+ content = [[
+ print(<?global?>)
+ ]],
+ }
+}
+
+TEST {
+ {
+ path = 'b.lua',
+ content = [[
+ print(<?global?>)
+ ]],
+ },
+ {
+ path = 'a.lua',
+ content = [[
+ <!global!> = 1
+ ]],
+ },
+}
+
+TEST {
+ {
+ path = 'a.lua',
+ content = [[
+ x = {}
+ x.<!global!> = 1
+ ]],
+ },
+ {
+ path = 'b.lua',
+ content = [[
+ print(x.<?global?>)
+ ]],
+ },
+}
+
+TEST {
+ {
+ path = 'a.lua',
+ content = [[
+ x.<!global!> = 1
+ ]],
+ },
+ {
+ path = 'b.lua',
+ content = [[
+ print(x.<?global?>)
+ ]],
+ },
+ {
+ path = 'c.lua',
+ content = [[
+ x = {}
+ ]]
+ }
+}
+
+TEST {
+ {
+ path = 'a.lua',
+ content = [[
+ return function (<!arg!>)
+ print(<?arg?>)
+ end
+ ]],
+ },
+ {
+ path = 'b.lua',
+ content = [[
+ local f = require 'a'
+ local v = 1
+ f(v)
+ ]],
+ },
+}
+
+TEST {
+ {
+ path = 'a.lua',
+ content = [[
+ return <!{
+ a = 1,
+ }!>
+ ]],
+ },
+ {
+ path = 'b.lua',
+ content = [[
+ local <!t!> = require 'a'
+ <?t?>
+ ]],
+ }
+}
+
+TEST {
+ {
+ path = 'a.lua',
+ content = [[
+ return <!function () end!>
+ ]]
+ },
+ {
+ path = 'b.lua',
+ content = [[
+ local f = require 'a'
+ ]]
+ },
+ {
+ path = 'c.lua',
+ content = [[
+ local <!f!> = require 'a'
+ <?f?>
+ ]]
+ }
+}
+
+TEST {
+ {
+ path = 'a.lua',
+ content = [[
+ local function <!f!>()
+ end
+ return <!f!>
+ ]]
+ },
+ {
+ path = 'b.lua',
+ content = [[
+ local f = require 'a'
+ ]]
+ },
+ {
+ path = 'c.lua',
+ content = [[
+ local <!f!> = require 'a'
+ <?f?>
+ ]]
+ }
+}
+
+TEST {
+ {
+ path = 'a/xxx.lua',
+ content = [[
+ return <!function () end!>
+ ]]
+ },
+ {
+ path = 'b/xxx.lua',
+ content = [[
+ local <!f!> = require 'xxx'
+ <?f?>
+ return function () end
+ ]]
+ }
+}
+
+TEST {
+ {
+ path = 'a.lua',
+ content = [[
+ local x
+ return {
+ <!x!> = x,
+ }
+ ]],
+ },
+ {
+ path = 'b.lua',
+ content = [[
+ local t = require 'a'
+ print(t.<?x?>)
+ ]],
+ },
+}
+
+--TEST {
+-- {
+-- path = 'a.lua',
+-- content = [[
+-- ---@class Class
+-- local <!obj!>
+-- ]]
+-- },
+-- {
+-- path = 'b.lua',
+-- content = [[
+-- ---@type Class
+-- local <!obj!>
+-- <?obj?>
+-- ]]
+-- },
+--}
+
+--TEST {
+-- {
+-- path = 'a.lua',
+-- content = [[
+-- ---@type Class
+-- local <!obj!>
+-- <?obj?>
+-- ]]
+-- },
+-- {
+-- path = 'b.lua',
+-- content = [[
+-- ---@class Class
+-- local <!obj!>
+-- ]]
+-- },
+--}
diff --git a/script-beta/test/crossfile/hover.lua b/script-beta/test/crossfile/hover.lua
new file mode 100644
index 00000000..1d4328e1
--- /dev/null
+++ b/script-beta/test/crossfile/hover.lua
@@ -0,0 +1,347 @@
+local service = require 'service'
+local workspace = require 'workspace'
+local fs = require 'bee.filesystem'
+local core = require 'core'
+local uric = require 'uri'
+
+rawset(_G, 'TEST', true)
+
+local EXISTS = {}
+
+local function eq(a, b)
+ if a == EXISTS and b ~= nil then
+ return true
+ end
+ if b == EXISTS and a ~= nil then
+ return true
+ end
+ local tp1, tp2 = type(a), type(b)
+ if tp1 ~= tp2 then
+ return false
+ end
+ if tp1 == 'table' then
+ local mark = {}
+ for k in pairs(a) do
+ if not eq(a[k], b[k]) then
+ return false
+ end
+ mark[k] = true
+ end
+ for k in pairs(b) do
+ if not mark[k] then
+ return false
+ end
+ end
+ return true
+ end
+ return a == b
+end
+
+local function catch_target(script, sep)
+ local list = {}
+ local cur = 1
+ local cut = 0
+ while true do
+ local start, finish = script:find(('<%%%s.-%%%s>'):format(sep, sep), cur)
+ if not start then
+ break
+ end
+ list[#list+1] = { start - cut, finish - 4 - cut }
+ cur = finish + 1
+ cut = cut + 4
+ end
+ local new_script = script:gsub(('<%%%s(.-)%%%s>'):format(sep, sep), '%1')
+ return new_script, list
+end
+
+function TEST(data)
+ local lsp = service()
+ local ws = workspace(lsp, 'test')
+ lsp.workspace = ws
+ ws.root = ROOT
+
+ local targetScript = data[1].content
+ local targetUri = uric.encode(fs.path(data[1].path))
+
+ local sourceScript, sourceList = catch_target(data[2].content, '?')
+ local sourceUri = uric.encode(fs.path(data[2].path))
+
+ lsp:saveText(targetUri, 1, targetScript)
+ ws:addFile(uric.decode(targetUri))
+ lsp:compileVM(targetUri)
+ lsp:saveText(sourceUri, 1, sourceScript)
+ ws:addFile(uric.decode(sourceUri))
+ lsp:compileVM(sourceUri)
+
+ local sourceVM = lsp:loadVM(sourceUri)
+ assert(sourceVM)
+ local sourcePos = (sourceList[1][1] + sourceList[1][2]) // 2
+ local source = core.findSource(sourceVM, sourcePos)
+ local hover = core.hover(source, lsp)
+ assert(hover)
+ if data.hover.description then
+ local uriROOT = uric.encode(ROOT):gsub('%%', '%%%%')
+ data.hover.description = data.hover.description:gsub('%$ROOT%$', uriROOT)
+ end
+ if hover.label then
+ hover.label = hover.label:gsub('\r\n', '\n')
+ end
+ assert(eq(hover, data.hover))
+end
+
+TEST {
+ {
+ path = 'a.lua',
+ content = '',
+ },
+ {
+ path = 'b.lua',
+ content = 'require <?"a"?>',
+ },
+ hover = {
+ description = [[[a.lua]($ROOT$/a.lua)]],
+ }
+}
+
+TEST {
+ {
+ path = 'a.lua',
+ content = [[
+ local function f(a, b)
+ end
+ return f
+ ]],
+ },
+ {
+ path = 'b.lua',
+ content = [[
+ local x = require 'a'
+ <?x?>()
+ ]]
+ },
+ hover = {
+ label = 'function f(a: any, b: any)',
+ name = 'f',
+ args = EXISTS,
+ }
+}
+
+TEST {
+ {
+ path = 'a.lua',
+ content = [[
+ return function (a, b)
+ end
+ ]],
+ },
+ {
+ path = 'b.lua',
+ content = [[
+ local f = require 'a'
+ <?f?>()
+ ]]
+ },
+ hover = {
+ label = 'function (a: any, b: any)',
+ name = '',
+ args = EXISTS,
+ }
+}
+
+TEST {
+ {
+ path = 'a.lua',
+ content = [[
+ local mt = {}
+ mt.__index = mt
+
+ function mt:add(a, b)
+ end
+
+ return function ()
+ return setmetatable({}, mt)
+ end
+ ]],
+ },
+ {
+ path = 'b.lua',
+ content = [[
+ local m = require 'a'
+ local obj = m()
+ obj:<?add?>()
+ ]]
+ },
+ hover = {
+ label = 'function mt:add(a: any, b: any)',
+ name = 'mt:add',
+ args = EXISTS,
+ },
+}
+
+TEST {
+ {
+ path = 'a.lua',
+ content = [[
+ t = {
+ [{}] = 1,
+ }
+ ]],
+ },
+ {
+ path = 'b.lua',
+ content = [[
+ <?t?>[{}] = 2
+ ]]
+ },
+ hover = {
+ label = [[
+global t: {
+ [*table]: number = 1,
+ [*table]: number = 2,
+}]],
+ name = 't',
+ },
+}
+
+TEST {
+ {
+ path = 'a.lua',
+ content = [[
+ t = {
+ [{}] = 1,
+ }
+ ]],
+ },
+ {
+ path = 'a.lua',
+ content = [[
+ <?t?>[{}] = 2
+ ]]
+ },
+ hover = {
+ label = [[
+global t: {
+ [*table]: number = 2,
+}]],
+ name = 't',
+ },
+}
+
+TEST {
+ {
+ path = 'a.lua',
+ content = '',
+ },
+ {
+ path = 'b.lua',
+ content = [[
+ --- abc
+ ---@param x number
+ function <?f?>(x) end
+ ]],
+ },
+ hover = {
+ label = [[function f(x: number)]],
+ name = 'f',
+ description = 'abc',
+ args = EXISTS,
+ }
+}
+
+TEST {
+ {
+ path = 'a.lua',
+ content = '',
+ },
+ {
+ path = 'b.lua',
+ content = [[
+ --- abc
+ <?x?> = 1
+ ]],
+ },
+ hover = {
+ label = [[global x: number = 1]],
+ name = 'x',
+ description = 'abc',
+ }
+}
+
+TEST {
+ {
+ path = 'a.lua',
+ content = '',
+ },
+ {
+ path = 'b.lua',
+ content = [[
+ ---@param x string
+ ---| "'选项1'" # 注释1
+ ---| > "'选项2'" # 注释2
+ function <?f?>(x) end
+ ]]
+ },
+ hover = {
+ label = 'function f(x: string)',
+ name = 'f',
+ args = EXISTS,
+ rawEnum = EXISTS,
+ enum = [[
+
+x: string
+ | '选项1' -- 注释1
+ |>'选项2' -- 注释2]]
+ }
+}
+
+TEST {
+ {
+ path = 'a.lua',
+ content = '',
+ },
+ {
+ path = 'b.lua',
+ content = [[
+ ---@alias option
+ ---| "'选项1'" # 注释1
+ ---| > "'选项2'" # 注释2
+ ---@param x option
+ function <?f?>(x) end
+ ]]
+ },
+ hover = {
+ label = 'function f(x: option)',
+ name = 'f',
+ args = EXISTS,
+ rawEnum = EXISTS,
+ enum = [[
+
+x: option
+ | '选项1' -- 注释1
+ |>'选项2' -- 注释2]]
+ }
+}
+
+TEST {
+ {
+ path = 'a.lua',
+ content = '',
+ },
+ {
+ path = 'b.lua',
+ content = [[
+ ---@param x string {comment = 'aaaa'}
+ ---@param y string {comment = 'bbbb'}
+ local function <?f?>(x, y) end
+ ]]
+ },
+ hover = {
+ label = 'function f(x: string, y: string)',
+ name = 'f',
+ args = EXISTS,
+ description = [[
++ `x`*(string)*: aaaa
+
++ `y`*(string)*: bbbb]]
+ }
+}
diff --git a/script-beta/test/crossfile/init.lua b/script-beta/test/crossfile/init.lua
new file mode 100644
index 00000000..c67b3b8f
--- /dev/null
+++ b/script-beta/test/crossfile/init.lua
@@ -0,0 +1,4 @@
+require 'crossfile.references'
+require 'crossfile.definition'
+--require 'crossfile.hover'
+--require 'crossfile.completion'
diff --git a/script-beta/test/crossfile/references.lua b/script-beta/test/crossfile/references.lua
new file mode 100644
index 00000000..a1747dac
--- /dev/null
+++ b/script-beta/test/crossfile/references.lua
@@ -0,0 +1,249 @@
+local files = require 'files'
+local furi = require 'file-uri'
+local core = require 'core.reference'
+
+rawset(_G, 'TEST', true)
+
+local EXISTS = {}
+
+local function eq(a, b)
+ if a == EXISTS and b ~= nil then
+ return true
+ end
+ local tp1, tp2 = type(a), type(b)
+ if tp1 ~= tp2 then
+ return false
+ end
+ if tp1 == 'table' then
+ local mark = {}
+ for k in pairs(a) do
+ if not eq(a[k], b[k]) then
+ return false
+ end
+ mark[k] = true
+ end
+ for k in pairs(b) do
+ if not mark[k] then
+ return false
+ end
+ end
+ return true
+ end
+ return a == b
+end
+
+local function catch_target(script, sep)
+ local list = {}
+ local cur = 1
+ local cut = 0
+ while true do
+ local start, finish = script:find(('<%%%s.-%%%s>'):format(sep, sep), cur)
+ if not start then
+ break
+ end
+ list[#list+1] = { start - cut, finish - 4 - cut }
+ cur = finish + 1
+ cut = cut + 4
+ end
+ local new_script = script:gsub(('<%%%s(.-)%%%s>'):format(sep, sep), '%1')
+ return new_script, list
+end
+
+local function founded(targets, results)
+ if #targets ~= #results then
+ return false
+ end
+ for _, target in ipairs(targets) do
+ for _, result in ipairs(results) do
+ if target[1] == result[1]
+ and target[2] == result[2]
+ and target[3] == result[3]
+ then
+ goto NEXT
+ end
+ end
+ do return false end
+ ::NEXT::
+ end
+ return true
+end
+
+function TEST(datas)
+ files.removeAll()
+
+ local targetList = {}
+ local sourceList
+ local sourceUri
+ for i, data in ipairs(datas) do
+ local uri = furi.encode(data.path)
+ local new, list = catch_target(data.content, '!')
+ if new ~= data.content or data.target then
+ if data.target then
+ targetList[#targetList+1] = {
+ data.target[1],
+ data.target[2],
+ uri,
+ }
+ else
+ for _, position in ipairs(list) do
+ targetList[#targetList+1] = {
+ position[1],
+ position[2],
+ uri,
+ }
+ end
+ end
+ data.content = new
+ end
+ new, list = catch_target(data.content, '~')
+ if new ~= data.content then
+ sourceList = list
+ sourceUri = uri
+ data.content = new
+ end
+ new, list = catch_target(data.content, '?')
+ if new ~= data.content then
+ sourceList = list
+ sourceUri = uri
+ data.content = new
+ for _, position in ipairs(list) do
+ targetList[#targetList+1] = {
+ position[1],
+ position[2],
+ uri,
+ }
+ end
+ end
+ files.setText(uri, data.content)
+ end
+
+ local sourcePos = (sourceList[1][1] + sourceList[1][2]) // 2
+ local positions = core(sourceUri, sourcePos)
+ if positions then
+ local result = {}
+ for i, position in ipairs(positions) do
+ result[i] = {
+ position.target.start,
+ position.target.finish,
+ position.uri,
+ }
+ end
+ assert(founded(targetList, result))
+ else
+ assert(#targetList == 0)
+ end
+end
+
+TEST {
+ {
+ path = 'lib.lua',
+ content = [[
+ return <!function ()
+ end!>
+ ]],
+ },
+ {
+ path = 'a.lua',
+ content = [[
+ local <?f?> = require 'lib'
+ ]],
+ },
+}
+
+TEST {
+ {
+ path = 'a.lua',
+ content = [[
+ local <!f!> = require 'lib'
+ ]],
+ },
+ {
+ path = 'lib.lua',
+ content = [[
+ return <~function~> ()
+ end
+ ]],
+ target = {20, 46},
+ },
+}
+
+TEST {
+ {
+ path = 'a.lua',
+ content = [[
+ <!ROOT!> = 1
+ ]],
+ },
+ {
+ path = 'b.lua',
+ content = [[
+ print(<?ROOT?>)
+ ]],
+ },
+}
+
+TEST {
+ {
+ path = 'a.lua',
+ content = [[
+ <?ROOT?> = 1
+ ]],
+ },
+ {
+ path = 'b.lua',
+ content = [[
+ print(<!ROOT!>)
+ ]],
+ },
+}
+
+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 f = require 'lib'
+ local <?o?> = f()
+ ]],
+ },
+ {
+ path = 'lib.lua',
+ content = [[
+ return function ()
+ return <!{}!>
+ end
+ ]],
+ },
+}