diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2020-11-20 21:57:09 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2020-11-20 21:57:09 +0800 |
commit | 4ca61ec457822dd14966afa0752340ae8ce180a1 (patch) | |
tree | ae8adb1ad82c717868e551e699fd3cf3bb290089 /test/crossfile | |
parent | c63b2e404d8d2bb984afe3678a5ba2b2836380cc (diff) | |
download | lua-language-server-4ca61ec457822dd14966afa0752340ae8ce180a1.zip |
no longer beta
Diffstat (limited to 'test/crossfile')
-rw-r--r-- | test/crossfile/completion.lua | 579 | ||||
-rw-r--r-- | test/crossfile/definition.lua | 621 | ||||
-rw-r--r-- | test/crossfile/hover.lua | 431 | ||||
-rw-r--r-- | test/crossfile/init.lua | 4 | ||||
-rw-r--r-- | test/crossfile/references.lua | 413 |
5 files changed, 2048 insertions, 0 deletions
diff --git a/test/crossfile/completion.lua b/test/crossfile/completion.lua new file mode 100644 index 00000000..6ac45c29 --- /dev/null +++ b/test/crossfile/completion.lua @@ -0,0 +1,579 @@ +local files = require 'files' +local core = require 'core.completion' +local furi = require 'file-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 + +local Cared = { + ['label'] = true, + ['kind'] = true, + ['textEdit'] = true, +} + +function TEST(data) + files.removeAll() + + local mainUri + local pos + for _, info in ipairs(data) do + local uri = furi.encode(info.path) + local script = info.content + if info.main then + pos = script:find('$', 1, true) - 1 + script = script:gsub('%$', '') + mainUri = uri + end + files.setText(uri, script) + end + + local expect = data.completion + local result = core.completion(mainUri, pos) + if not expect then + assert(result == nil) + return + end + assert(result ~= nil) + for _, item in ipairs(result) do + local r = core.resolve(item.id) + for k, v in pairs(r or {}) do + item[k] = v + end + for k in pairs(item) do + if not Cared[k] then + item[k] = nil + end + end + if item['description'] then + item['description'] = item['description'] + : gsub('\r\n', '\n') + end + end + assert(result) + assert(eq(expect, result)) +end + +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 = 'aaa', + kind = CompletionItemKind.Reference, + textEdit = EXISTS, + }, + { + label = 'abc', + kind = CompletionItemKind.Reference, + textEdit = EXISTS, + }, + { + label = 'abc.aaa', + kind = CompletionItemKind.Reference, + textEdit = EXISTS, + }, + { + label = 'abcde', + kind = CompletionItemKind.Reference, + textEdit = EXISTS, + }, + { + label = 'xxx.abcde', + kind = CompletionItemKind.Reference, + textEdit = EXISTS, + }, + } +} + +TEST { + { + path = 'abc.lua', + content = '', + }, + { + path = 'test.lua', + content = 'require "A$"', + main = true, + }, + completion = { + { + label = 'abc', + kind = CompletionItemKind.Reference, + textEdit = EXISTS, + }, + } +} + +TEST { + { + path = 'abc.lua', + content = '', + }, + { + path = 'ABCD.lua', + content = '', + }, + { + path = 'test.lua', + content = 'require "a$"', + main = true, + }, + completion = { + { + label = 'ABCD', + kind = CompletionItemKind.Reference, + textEdit = EXISTS, + }, + { + label = 'abc', + kind = CompletionItemKind.Reference, + textEdit = EXISTS, + }, + } +} + +TEST { + { + path = 'abc.lua', + content = '', + }, + { + path = 'abc/init.lua', + content = '', + }, + { + path = 'test.lua', + content = 'require "abc$"', + main = true, + }, + completion = { + { + label = 'abc', + kind = CompletionItemKind.Reference, + textEdit = EXISTS, + }, + { + label = 'abc.init', + kind = CompletionItemKind.Reference, + textEdit = EXISTS, + }, + } +} + +TEST { + { + path = 'abc/init.lua', + content = '', + }, + { + path = 'abc/bbc.lua', + content = '', + }, + { + path = 'test.lua', + content = 'require "abc$"', + main = true, + }, + completion = { + { + label = 'abc', + kind = CompletionItemKind.Reference, + textEdit = EXISTS, + }, + { + label = 'abc.bbc', + kind = CompletionItemKind.Reference, + textEdit = EXISTS, + }, + { + label = 'abc.init', + kind = CompletionItemKind.Reference, + textEdit = EXISTS, + }, + } +} + +TEST { + { + path = 'abc.lua', + content = '', + }, + { + path = 'abc/init.lua', + content = '', + }, + { + path = 'test.lua', + content = 'require "abc.i$"', + main = true, + }, + completion = { + { + label = 'abc.init', + kind = CompletionItemKind.Reference, + textEdit = 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', + kind = CompletionItemKind.Reference, + textEdit = EXISTS, + }, + } +} + +TEST { + { + path = 'x000.lua', + content = '', + }, + { + path = 'abc/x111.lua', + content = '', + }, + { + path = 'abc/test.lua', + content = 'require "x$"', + main = true, + }, + completion = { + { + label = 'abc.x111', + kind = CompletionItemKind.Reference, + textEdit = EXISTS, + }, + { + label = 'x000', + kind = CompletionItemKind.Reference, + textEdit = EXISTS, + }, + { + label = 'x111', + kind = CompletionItemKind.Reference, + textEdit = 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, + }, + { + label = 'b', + kind = CompletionItemKind.Enum, + }, + { + label = 'c', + kind = CompletionItemKind.Enum, + }, + } +} + +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, + }, + } +} + +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, + }, + } +} + +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', + kind = CompletionItemKind.Reference, + textEdit = EXISTS, + }, + } +} + +TEST { + { + path = [[xx'xx.lua]], + content = '' + }, + { + path = 'main.lua', + content = [[ + require 'xx$' + ]], + main = true, + }, + completion = { + { + label = [[xx'xx]], + kind = CompletionItemKind.Reference, + textEdit = EXISTS, + }, + } +} + +TEST { + { + path = [[xx]=]xx.lua]], + content = '' + }, + { + path = 'main.lua', + content = [[ + require [=[xx$]=]' + ]], + main = true, + }, + completion = { + { + label = [[xx]=]xx]], + kind = CompletionItemKind.Reference, + textEdit = EXISTS, + }, + } +} + +TEST { + { + path = [[abc/init.lua]], + content = '' + }, + { + path = 'main.lua', + content = [[ + dofile 'ab$' + ]], + main = true, + }, + completion = { + { + label = [[abc\init.lua]], + kind = CompletionItemKind.Reference, + textEdit = EXISTS, + }, + } +} + +Cared['description'] = true +TEST { + { + path = [[a.lua]], + content = [[ + local m = {} + + return m + ]] + }, + { + path = 'main.lua', + content = [[ + local z = require 'a' + + z$ + ]], + main = true, + }, + completion = { + { + label = 'z', + kind = CompletionItemKind.Variable, + description = [[ +```lua +local z: {} +```]] + }, + } +} diff --git a/test/crossfile/definition.lua b/test/crossfile/definition.lua new file mode 100644 index 00000000..5bc99a19 --- /dev/null +++ b/test/crossfile/definition.lua @@ -0,0 +1,621 @@ +local files = require 'files' +local furi = require 'file-uri' +local core = require 'core.definition' +local config = require 'config' + +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 = 'aaa/bbb.lua', + content = '', + target = {0, 0}, + }, + { + path = 'b.lua', + content = 'require "<?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 = [[ + 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 m = {} + function m.<!func!>() + end + return m + ]] + }, + { + path = 'b.lua', + content = [[ + local x = require 'a' + print(x.<?func?>) + ]] + } +} + +TEST { + { + path = 'a.lua', + content = [[ + local m = {} + function m.<!func!>() + end + return m + ]] + }, + { + path = 'c.lua', + content = [[ + local x = require 'a' + print(x.func) + ]] + }, + { + path = 'b.lua', + content = [[ + local x = require 'a' + print(x.<?func?>) + ]] + } +} + +TEST { + { + path = 'a.lua', + content = [[ + return <!function () + end!> + ]] + }, + { + path = 'middle.lua', + content = [[ + return { + <!func!> = require 'a' + } + ]] + }, + { + path = 'b.lua', + content = [[ + local x = require 'middle' + print(x.<?func?>) + ]] + } +} + +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?>() + ]] + }, +} + +config.config.intelliSense.searchDepth = 0 + +TEST { + { + path = 'a.lua', + content = [[ + local t = GlobalTable + + t.settings = { + <!test!> = 1 + } + ]], + }, + { + path = 'b.lua', + content = [[ + local b = GlobalTable.settings + + print(b.<?test?>) + ]] + }, +} + +config.config.intelliSense.searchDepth = 5 + +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/test/crossfile/hover.lua b/test/crossfile/hover.lua new file mode 100644 index 00000000..9fc21461 --- /dev/null +++ b/test/crossfile/hover.lua @@ -0,0 +1,431 @@ +local files = require 'files' +local furi = require 'file-uri' +local core = require 'core.hover' +local config = require 'config' + +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(expect) + files.removeAll() + + local targetScript = expect[1].content + local targetUri = furi.encode(expect[1].path) + + local sourceScript, sourceList = catch_target(expect[2].content, '?') + local sourceUri = furi.encode(expect[2].path) + + files.setText(targetUri, targetScript) + files.setText(sourceUri, sourceScript) + + local sourcePos = (sourceList[1][1] + sourceList[1][2]) // 2 + local hover = core.byUri(sourceUri, sourcePos) + assert(hover) + if hover.label then + hover.label = hover.label:gsub('\r\n', '\n') + end + assert(eq(hover.label, expect.hover.label)) + assert(eq(hover.description, expect.hover.description)) +end + +TEST { + { + path = 'a.lua', + content = '', + }, + { + path = 'b.lua', + content = 'require <?"a"?>', + }, + hover = { + label = '1 个字节', + description = [[* [a.lua](file:///a.lua) (假设搜索路径包含 `?.lua`)]], + } +} + +TEST { + { + path = 'Folder/a.lua', + content = '', + }, + { + path = 'b.lua', + content = 'require <?"a"?>', + }, + hover = { + label = '1 个字节', + description = [[* [Folder\a.lua](file:///Folder/a.lua) (假设搜索路径包含 `Folder\?.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]: integer = 1|2, +}]], + name = 't', + }, +} + +TEST { + { + path = 'a.lua', + content = [[ + t = { + [{}] = 1, + } + ]], + }, + { + path = 'a.lua', + content = [[ + <?t?>[{}] = 2 + ]] + }, + hover = { + label = [[ +global t: { + [table]: integer = 2, +}]], + name = 't', + }, +} + +TEST { + { + path = 'a.lua', + content = [[ + return { + a = 1, + b = 2, + } + ]], + }, + { + path = 'b.lua', + content = [[ + local <?t?> = require 'a' + ]] + }, + hover = { + label = [[ +local t: { + a: integer = 1, + b: integer = 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: integer = 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|'选项1'|'选项2')", + name = 'f', + description = [[ +```lua +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: '选项1'|'选项2')", + name = 'f', + description = [[ +```lua +x: option + | '选项1' -- 注释1 + -> '选项2' -- 注释2 +```]] + } +} + +TEST { + { + path = 'a.lua', + content = '', + }, + { + path = 'b.lua', + content = [[ + ---@alias option + ---| "'选项1'" # 注释1 + ---| > "'选项2'" # 注释2 + ---@return option x + function <?f?>() end + ]] + }, + hover = { + label = [[ +function f() + -> x: '选项1'|'选项2']], + name = 'f', + description = [[ +```lua +x: option + | '选项1' -- 注释1 + -> '选项2' -- 注释2 +```]] + } +} + +TEST { + { + path = 'a.lua', + content = '', + }, + { + path = 'b.lua', + content = [[ + ---@alias option + ---| "'选项1'" # 注释1 + ---| > "'选项2'" # 注释2 + ---@return option + function <?f?>() end + ]] + }, + hover = { + label = [[ +function f() + -> '选项1'|'选项2']], + name = 'f', + description = [[ +```lua +(return 1): option + | '选项1' -- 注释1 + -> '选项2' -- 注释2 +```]] + } +} + +do return end +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/test/crossfile/init.lua b/test/crossfile/init.lua new file mode 100644 index 00000000..ca70ec44 --- /dev/null +++ b/test/crossfile/init.lua @@ -0,0 +1,4 @@ +require 'crossfile.definition' +require 'crossfile.references' +require 'crossfile.hover' +require 'crossfile.completion' diff --git a/test/crossfile/references.lua b/test/crossfile/references.lua new file mode 100644 index 00000000..66abd4e3 --- /dev/null +++ b/test/crossfile/references.lua @@ -0,0 +1,413 @@ +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 + while true do + local start, finish = script:find(('<%%%s.-%%%s>'):format(sep, sep), cur) + if not start then + break + end + list[#list+1] = { start + 2, finish - 2 } + cur = finish + 1 + 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 = {22, 50}, + }, +} + +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 = [[ + 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 f = require 'lib' + local <?o?> = f() + ]], + }, + { + path = 'lib.lua', + content = [[ + return function () + return <!{}!> + end + ]], + }, +} + +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!> + ]] + }, +} |