diff options
Diffstat (limited to 'test')
28 files changed, 763 insertions, 917 deletions
diff --git a/test/basic/noder.lua b/test/basic/noder.lua index bc572a4c..87b34e12 100644 --- a/test/basic/noder.lua +++ b/test/basic/noder.lua @@ -2,6 +2,7 @@ local noder = require 'core.noder' local files = require 'files' local util = require 'utility' local guide = require 'parser.guide' +local catch = require 'catch' local function getSource(pos) local ast = files.getState('') @@ -30,12 +31,9 @@ local CARE = {} local function TEST(script) return function (expect) files.removeAll() - local start = script:find('<?', 1, true) - local finish = script:find('?>', 1, true) - local pos = (start + finish) // 2 + 1 - local newScript = script:gsub('<[!?]', ' '):gsub('[!?]>', ' ') + local newScript, catched = catch(script, '?') files.setText('', newScript) - local source = getSource(pos) + local source = getSource(catched['?'][1][1]) assert(source) local result = { id = noder.getID(source), @@ -53,21 +51,21 @@ CARE['id'] = true TEST [[ local <?x?> ]] { - id = 'l:9', + id = 'l:6', } TEST [[ local x print(<?x?>) ]] { - id = 'l:7', + id = 'l:6', } TEST [[ local x <?x?> = 1 ]] { - id = 'l:7', + id = 'l:6', } TEST [[ @@ -86,14 +84,14 @@ TEST [[ local x print(x.y.<?z?>) ]] { - id = 'l:7|.y|.z', + id = 'l:6|.y|.z', } TEST [[ local x function x:<?f?>() end ]] { - id = 'l:7|.f', + id = 'l:6|.f', } TEST [[ @@ -113,7 +111,7 @@ TEST [[ <?x?> = 1, } ]] { - id = 't:1|.x', + id = 't:0|.x', } TEST [[ @@ -134,12 +132,12 @@ TEST [[ ::<?label?>:: goto label ]] { - id = 'l:5', + id = 'l:2', } TEST [[ ::label:: goto <?label?> ]] { - id = 'l:3', + id = 'l:2', } diff --git a/test/catch.lua b/test/catch.lua new file mode 100644 index 00000000..849be09d --- /dev/null +++ b/test/catch.lua @@ -0,0 +1,84 @@ +local m = require 'lpeglabel' + +local mt = {} + +local function catchedTable() + return setmetatable({}, mt) +end + +function mt.__add(a, b) + if not a or not b then + return a or b + end + local t = catchedTable() + for _, v in ipairs(a) do + t[#t+1] = v + end + for _, v in ipairs(b) do + t[#t+1] = v + end + return t +end + +local function parseTokens(script, seps) + local parser = m.P { + m.Ct(m.V 'Token'^0), + Token = m.Cp() * (m.V 'Mark' + m.V 'Nl' + m.V 'Text'), + Mark = m.Cc 'ML' * m.P '<' * m.C(m.S(seps)) + + m.Cc 'MR' * m.C(m.S(seps)) * m.P '>', + Nl = m.Cc 'NL' * m.C(m.P '\r\n' + m.S '\r\n'), + Text = m.Cc 'TX' * m.C((1 - m.V 'Nl' - m.V 'Mark')^1), + } + local results = parser:match(script) + return results +end + +---@param script string +---@param seps string +return function (script, seps) + local tokens = parseTokens(script, seps) + local newBuf = {} + local result = {} + local marks = {} + + local lineOffset = 1 + local line = 0 + local skipOffset = 0 + for i = 1, #tokens, 3 do + local offset = tokens[i + 0] + local mode = tokens[i + 1] + local text = tokens[i + 2] + if mode == 'TX' then + newBuf[#newBuf+1] = text + end + if mode == 'NL' then + newBuf[#newBuf+1] = text + line = line + 1 + lineOffset = offset + #text - skipOffset + end + if mode == 'ML' then + marks[#marks+1] = { + char = text, + position = line * 10000 + offset - skipOffset - lineOffset, + } + skipOffset = skipOffset + 1 + #text + end + if mode == 'MR' then + for j = #marks, 1, -1 do + local mark = marks[j] + if mark.char == text then + local position = line * 10000 + offset - skipOffset - lineOffset + if not result[text] then + result[text] = catchedTable() + end + result[text][#result[text]+1] = { mark.position, position } + table.remove(marks, j) + break + end + end + skipOffset = skipOffset + 1 + #text + end + end + + return table.concat(newBuf), result +end diff --git a/test/code_action/init.lua b/test/code_action/init.lua index 11ce6ec7..70d8c9ec 100644 --- a/test/code_action/init.lua +++ b/test/code_action/init.lua @@ -1,6 +1,7 @@ local core = require 'core.code-action' local files = require 'files' local lang = require 'language' +local catch = require 'catch' rawset(_G, 'TEST', true) @@ -38,11 +39,10 @@ end function TEST(script) return function (expect) files.removeAll() - local start = script:find('<?', 1, true) - local finish = script:find('?>', 1, true) - local new_script = script:gsub('<[!?]', ' '):gsub('[!?]>', ' ') - files.setText('', new_script) - local results = core('', start, finish) + + local newScript, catched = catch(script, '?') + files.setText('', newScript) + local results = core('', catched['?'][1][1], catched['?'][1][2]) assert(results) assert(eq(expect, results)) end diff --git a/test/command/auto-require.lua b/test/command/auto-require.lua index 94bb5069..a52662fb 100644 --- a/test/command/auto-require.lua +++ b/test/command/auto-require.lua @@ -3,7 +3,7 @@ local files = require 'files' local autoRequire = require 'core.command.autoRequire' local client = require 'client' -local findInsertOffset = util.getUpvalue(autoRequire, 'findInsertOffset') +local findInsertRow = util.getUpvalue(autoRequire, 'findInsertRow') local applyAutoRequire = util.getUpvalue(autoRequire, 'applyAutoRequire') local originEditText = client.editText @@ -19,18 +19,25 @@ function TEST(text) files.removeAll() files.setText('', text) EditResult = nil - local offset, fmt = findInsertOffset('') - applyAutoRequire('', offset, name, name, fmt) + local row, fmt = findInsertRow('') + applyAutoRequire('', row, name, name, fmt) assert(util.equal(EditResult, expect)) end end end --- TODO change to position TEST '' 'test' { start = 0, - finish = -1, - text = '\nlocal test = require "test"\n' + finish = 0, + text = 'local test = require "test"\n' +} + +TEST [[ +local aaaaaa = require 'aaa' +]] 'test' { + start = 10000, + finish = 10000, + text = 'local test = require \'test\'\n' } client.editText = originEditText diff --git a/test/completion/common.lua b/test/completion/common.lua index 2efa63ad..33cbdf4d 100644 --- a/test/completion/common.lua +++ b/test/completion/common.lua @@ -8,7 +8,7 @@ config.set('Lua.completion.showWord', 'Enable') TEST [[ local zabcde -za$ +za<??> ]] { { @@ -20,7 +20,7 @@ za$ TEST [[ local zabcdefg local zabcde -zabcde$ +zabcde<??> ]] { { @@ -35,7 +35,7 @@ zabcde$ TEST [[ local zabcdefg -za$ +za<??> local zabcde ]] { @@ -51,7 +51,7 @@ local zabcde TEST [[ local zabcde -zace$ +zace<??> ]] { { @@ -63,7 +63,7 @@ zace$ TEST [[ ZABC = x local zabc -zac$ +zac<??> ]] { { @@ -77,7 +77,7 @@ zac$ } TEST [[ -ass$ +ass<??> ]] { { @@ -92,7 +92,7 @@ ass$ TEST [[ local assert = 1 -ass$ +ass<??> ]] { { @@ -103,7 +103,7 @@ ass$ TEST [[ local assert = 1 -_G.ass$ +_G.ass<??> ]] { { @@ -119,7 +119,7 @@ _G.ass$ TEST [[ local function ffff(a, b) end -ff$ +ff<??> ]] { { @@ -134,7 +134,7 @@ ff$ TEST [[ local zabc = 1 -z$ +z<??> ]] { { @@ -145,7 +145,7 @@ z$ TEST [[ local zabc = 1.0 -z$ +z<??> ]] { { @@ -158,7 +158,7 @@ TEST [[ local t = { abc = 1, } -t.ab$ +t.ab<??> ]] { { @@ -172,7 +172,7 @@ local t = { abc = 1, } local n = t.abc -t.ab$ +t.ab<??> ]] { { @@ -187,7 +187,7 @@ mt.ggg = 1 function mt:get(a, b) return 1 end -mt:g$ +mt:g<??> ]] { { @@ -205,7 +205,7 @@ mt:g$ } TEST [[ -loc$ +loc<??> ]] { { @@ -220,7 +220,7 @@ loc$ IgnoreFunction = true TEST [[ -do$ +do<??> ]] { { @@ -234,7 +234,7 @@ do$ } TEST [[ -while true d$ +while true d<??> ]] { { @@ -248,18 +248,18 @@ while true d$ } TEST [[ -results$ +results<??> ]] (nil) TEST [[ -result$ +result<??> local results ]] (EXISTS) TEST [[ -local a$ +local a<??> local function f(fff) fff = ast @@ -280,7 +280,7 @@ end TEST [[ t.a = {} t.b = {} -t.$ +t.<??> ]] { { @@ -296,7 +296,7 @@ t.$ TEST [[ t.a = {} t.b = {} -t. $ +t. <??> ]] { { @@ -314,7 +314,7 @@ TEST [[ t.a = {} function t:b() end -t:$ +t:<??> ]] { { @@ -331,7 +331,7 @@ TEST [[ local t = { a = {}, } -t.$ +t.<??> xxx() ]] { @@ -342,14 +342,14 @@ xxx() } TEST [[ -(''):$ +(''):<??> ]] (EXISTS) TEST [[ local zzz -return 'aa' .. zz$ +return 'aa' .. zz<??> ]] { { @@ -358,9 +358,9 @@ return 'aa' .. zz$ }, } -TEST 'local s = "a:$"' (nil) +TEST 'local s = "a:<??>"' (nil) -TEST 'debug.$' +TEST 'debug.<??>' (EXISTS) IgnoreFunction = true @@ -371,7 +371,7 @@ local xxxx = { } local t = { - x$ + x<??> } ]] { @@ -392,7 +392,7 @@ local t = { TEST [[ print(ff2) local faa -local f$ +local f<??> print(fff) ]] { @@ -419,7 +419,7 @@ print(fff) } TEST [[ -local function f(ff$) +local function f(ff<??>) print(fff) end ]] @@ -431,19 +431,19 @@ end } TEST [[ -collectgarbage($) +collectgarbage(<??>) ]] (EXISTS) TEST [[ -collectgarbage('$') +collectgarbage('<??>') ]] { { label = "'collect'", kind = define.CompletionItemKind.EnumMember, textEdit = { - start = 16, + start = 15, finish = 17, newText = "'collect'", }, @@ -452,7 +452,7 @@ collectgarbage('$') label = "'stop'", kind = define.CompletionItemKind.EnumMember, textEdit = { - start = 16, + start = 15, finish = 17, newText = "'stop'", }, @@ -461,7 +461,7 @@ collectgarbage('$') label = "'restart'", kind = define.CompletionItemKind.EnumMember, textEdit = { - start = 16, + start = 15, finish = 17, newText = "'restart'", }, @@ -470,7 +470,7 @@ collectgarbage('$') label = "'count'", kind = define.CompletionItemKind.EnumMember, textEdit = { - start = 16, + start = 15, finish = 17, newText = "'count'", }, @@ -479,7 +479,7 @@ collectgarbage('$') label = "'step'", kind = define.CompletionItemKind.EnumMember, textEdit = { - start = 16, + start = 15, finish = 17, newText = "'step'", }, @@ -488,7 +488,7 @@ collectgarbage('$') label = "'isrunning'", kind = define.CompletionItemKind.EnumMember, textEdit = { - start = 16, + start = 15, finish = 17, newText = "'isrunning'", }, @@ -497,7 +497,7 @@ collectgarbage('$') label = "'incremental'", kind = define.CompletionItemKind.EnumMember, textEdit = { - start = 16, + start = 15, finish = 17, newText = "'incremental'", }, @@ -506,7 +506,7 @@ collectgarbage('$') label = "'generational'", kind = define.CompletionItemKind.EnumMember, textEdit = { - start = 16, + start = 15, finish = 17, newText = "'generational'", }, @@ -514,7 +514,7 @@ collectgarbage('$') } TEST [[ -io.read($) +io.read(<??>) ]] { { @@ -536,25 +536,25 @@ io.read($) } TEST [[ -io.open('', $) +io.open('', <??>) ]] (EXISTS) TEST [[ -local function f(a, $) +local function f(a, <??>) end ]] (nil) TEST [[ -self.results.list[#$] +self.results.list[#<??>] ]] { { label = '#self.results.list+1', kind = define.CompletionItemKind.Snippet, textEdit = { - start = 19, + start = 18, finish = 20, newText = '#self.results.list+1] = ', }, @@ -562,7 +562,7 @@ self.results.list[#$] } TEST [[ -self.results.list[#$] +self.results.list[#<??>] local n = 1 ]] { @@ -570,7 +570,7 @@ local n = 1 label = '#self.results.list+1', kind = define.CompletionItemKind.Snippet, textEdit = { - start = 19, + start = 18, finish = 20, newText = '#self.results.list+1] = ', }, @@ -578,14 +578,14 @@ local n = 1 } TEST [[ -self.results.list[#$] = 1 +self.results.list[#<??>] = 1 ]] { { label = '#self.results.list+1', kind = define.CompletionItemKind.Snippet, textEdit = { - start = 19, + start = 18, finish = 20, newText = '#self.results.list+1]', }, @@ -593,14 +593,14 @@ self.results.list[#$] = 1 } TEST [[ -self.results.list[#self.re$] +self.results.list[#self.re<??>] ]] { { label = '#self.results.list+1', kind = define.CompletionItemKind.Snippet, textEdit = { - start = 19, + start = 18, finish = 27, newText = '#self.results.list+1] = ', }, @@ -612,14 +612,14 @@ self.results.list[#self.re$] } TEST [[ -fff[#ff$] +fff[#ff<??>] ]] { { label = '#fff+1', kind = define.CompletionItemKind.Snippet, textEdit = { - start = 5, + start = 4, finish = 8, newText = '#fff+1] = ', }, @@ -631,14 +631,14 @@ fff[#ff$] } TEST [[ -local _ = fff.kkk[#$] +local _ = fff.kkk[#<??>] ]] { { label = '#fff.kkk', kind = define.CompletionItemKind.Snippet, textEdit = { - start = 19, + start = 18, finish = 20, newText = '#fff.kkk]', }, @@ -646,14 +646,14 @@ local _ = fff.kkk[#$] } TEST [[ -fff.kkk[#$].yy +fff.kkk[#<??>].yy ]] { { label = '#fff.kkk', kind = define.CompletionItemKind.Snippet, textEdit = { - start = 9, + start = 8, finish = 10, newText = '#fff.kkk]', }, @@ -665,7 +665,7 @@ local t = { a = 1, } -t . $ +t . <??> ]] (EXISTS) @@ -674,7 +674,7 @@ local t = { a = 1, } -t . $ b +t . <??> b ]] (EXISTS) @@ -683,7 +683,7 @@ local t = { a = 1, } -t $ +t <??> ]] (nil) @@ -692,13 +692,13 @@ local t = { a = 1, } -t $. +t <??>. ]] (nil) TEST [[ local xxxx -xxxx$ +xxxx<??> ]] { { @@ -710,7 +710,7 @@ xxxx$ TEST [[ local xxxx local XXXX -xxxx$ +xxxx<??> ]] { { @@ -727,7 +727,7 @@ TEST [[ local t = { xxxxx = 1, } -xx$ +xx<??> ]] { { @@ -738,7 +738,7 @@ xx$ TEST [[ local index -tbl[inde$] +tbl[inde<??>] ]] { { @@ -753,7 +753,7 @@ return function () a = {}, b = {}, } - t.$ + t.<??> end ]] { @@ -769,7 +769,7 @@ end TEST [[ local ast = 1 -local t = 'as$' +local t = 'as<??>' local ask = 1 ]] (EXISTS) @@ -777,7 +777,7 @@ local ask = 1 TEST [[ local add -function f(ad$) +function f(ad<??>) local _ = add end ]] @@ -789,25 +789,25 @@ end } TEST [[ -function table.i$ +function table.i<??> ]] (EXISTS) TEST [[ do - xx.$ + xx.<??> end ]] (nil) TEST [[ -print(io.$) +print(io.<??>) ]] (EXISTS) require 'config'.set('Lua.runtime.version', 'Lua 5.4') --TEST [[ ---local $ +--local <??> --]] --{ -- { @@ -821,7 +821,7 @@ require 'config'.set('Lua.runtime.version', 'Lua 5.4') --} -- --TEST [[ ---local <toc$ +--local <toc<??> --]] --{ -- { @@ -835,7 +835,7 @@ local mt = {} mt.__index = mt local t = setmetatable({}, mt) -t.$ +t.<??> ]] { { @@ -848,7 +848,7 @@ TEST [[ local elseaaa ELSE = 1 if a then -else$ +else<??> ]] { { @@ -877,7 +877,7 @@ Cared['insertText'] = true IgnoreFunction = false TEST [[ local xpcal -xpcal$ +xpcal<??> ]] { { @@ -900,7 +900,7 @@ TEST [[ function mt:f(a, b, c) end -mt:f$ +mt:f<??> ]] { { @@ -916,7 +916,7 @@ mt:f$ } TEST [[ -function$ +function<??> ]] { { @@ -934,7 +934,7 @@ end", } TEST [[ -local t = function$ +local t = function<??> ]] { { @@ -956,7 +956,7 @@ IgnoreFunction = true TEST [[ local function f() if a then - else$ + else<??> end ]] { @@ -979,21 +979,21 @@ local t = { ['a.b.c'] = {} } -t.$ +t.<??> ]] { { label = 'a.b.c', kind = define.CompletionItemKind.Field, textEdit = { - start = 37, - finish = 36, + start = 40002, + finish = 40002, newText = '["a.b.c"]', }, additionalTextEdits = { { - start = 36, - finish = 36, + start = 40001, + finish = 40002, newText = '', }, }, @@ -1005,21 +1005,21 @@ local t = { ['a.b.c'] = {} } -t. $ +t. <??> ]] { { label = 'a.b.c', kind = define.CompletionItemKind.Field, textEdit = { - start = 40, - finish = 39, + start = 40005, + finish = 40005, newText = '["a.b.c"]', }, additionalTextEdits = { { - start = 36, - finish = 36, + start = 40001, + finish = 40002, newText = '', }, }, @@ -1031,15 +1031,15 @@ local t = { ['a.b.c'] = {} } -t['$'] +t['<??>'] ]] { { label = 'a.b.c', kind = define.CompletionItemKind.Field, textEdit = { - start = 38, - finish = 37, + start = 40003, + finish = 40003, newText = 'a.b.c', } } @@ -1048,33 +1048,33 @@ t['$'] TEST [[ _ENV['z.b.c'] = {} -z$ +z<??> ]] { { label = 'z.b.c', kind = define.CompletionItemKind.Field, textEdit = { - start = 21, - finish = 21, + start = 20000, + finish = 20001, newText = '_ENV["z.b.c"]', }, }, } TEST [[ -io.close(1, $) +io.close(1, <??>) ]] (nil) TEST [[ -io$ +io<??> ]] (EXISTS) IgnoreFunction = false TEST [[ -loadstring$ +loadstring<??> ]] { { @@ -1090,7 +1090,7 @@ loadstring$ } --TEST [[ ---bit32$ +--bit32<??> --]] --{ -- { @@ -1103,7 +1103,7 @@ loadstring$ TEST [[ function loadstring() end -loadstring$ +loadstring<??> ]] { { @@ -1127,7 +1127,7 @@ loadstring$ } TEST [[ -debug.setcsta$ +debug.setcsta<??> ]] { { @@ -1143,12 +1143,12 @@ debug.setcsta$ } TEST [[ ----@$ +---@<??> ]] (EXISTS) TEST [[ ----@cl$ +---@cl<??> ]] { { @@ -1159,7 +1159,7 @@ TEST [[ TEST [[ ---@class ZABC ----@class ZBBC : Z$ +---@class ZBBC : Z<??> ]] { { @@ -1170,14 +1170,14 @@ TEST [[ TEST [[ ---@class ZABC ----@class ZBBC : $ +---@class ZBBC : <??> ]] (EXISTS) TEST [[ ---@class zabc local abcd ----@type za$ +---@type za<??> ]] { { @@ -1189,14 +1189,14 @@ local abcd TEST [[ ---@class abc local abcd ----@type $ +---@type <??> ]] (EXISTS) TEST [[ ---@class zabc local abcd ----@type zxxx|z$ +---@type zxxx|z<??> ]] { { @@ -1207,7 +1207,7 @@ local abcd TEST [[ ---@alias zabc zabb ----@type za$ +---@type za<??> ]] { { @@ -1218,7 +1218,7 @@ TEST [[ TEST [[ ---@class ZClass ----@param x ZC$ +---@param x ZC<??> ]] { { @@ -1229,7 +1229,7 @@ TEST [[ Cared['insertText'] = true TEST [[ ----@param $ +---@param <??> function f(a, b, c) end ]] @@ -1257,7 +1257,7 @@ a ${1:any} } TEST [[ ----@param $ +---@param <??> function f(a, b, c) end function f2(a) end @@ -1286,7 +1286,7 @@ a ${1:any} } TEST [[ ----@param aa$ +---@param aa<??> function f(aaa, bbb, ccc) end ]] @@ -1299,7 +1299,7 @@ end TEST [[ local function f() - ---@param $ + ---@param <??> function f(a, b, c) end end @@ -1328,7 +1328,7 @@ a ${1:any} } TEST [[ ----@param $ +---@param <??> function mt:f(a, b, c, ...) end ]] @@ -1360,7 +1360,7 @@ a ${1:any} } TEST [[ ----@param aaa $ +---@param aaa <??> function f(aaa, bbb, ccc) end ]] @@ -1369,7 +1369,7 @@ end TEST [[ ---@param xyz Class ---@param xxx Class -function f(x$) +function f(x<??>) ]] { { @@ -1389,7 +1389,7 @@ function f(x$) TEST [[ ---@param xyz Class ---@param xxx Class -function f($ +function f(<??> ]] { { @@ -1409,7 +1409,7 @@ function f($ TEST [[ ---@param xyz Class ---@param xxx Class -function f($) +function f(<??>) ]] { { @@ -1428,7 +1428,7 @@ function f($) TEST [[ local function f() - ---@t$ + ---@t<??> end ]] { @@ -1443,7 +1443,7 @@ TEST [[ ---@field name string ---@field id integer local mt = {} -mt.$ +mt.<??> ]] { { @@ -1461,7 +1461,7 @@ TEST [[ function f(y, x) end -f(1, $) +f(1, <??>) ]] { { @@ -1483,7 +1483,7 @@ TEST [[ function f(y, x) end -f(1,$) +f(1,<??>) ]] { { @@ -1505,7 +1505,7 @@ TEST [[ function f(x) end -f($) +f(<??>) ]] { { @@ -1528,7 +1528,7 @@ TEST [[ function f(x) end -f($) +f(<??>) ]] { { @@ -1550,7 +1550,7 @@ TEST [[ function f(x) end -f('$') +f('<??>') ]] { { @@ -1586,7 +1586,7 @@ TEST [[ local function f(x) end -f($) +f(<??>) ]] { { @@ -1619,7 +1619,7 @@ end ---comment 3 ---| '3' -f($) +f(<??>) ]] { { @@ -1638,7 +1638,7 @@ function f(x) end f(function () - $ + <??> end) ]] (nil) @@ -1652,7 +1652,7 @@ TEST [[ ---@return string local function zzzzz(list, sep, i, j) end -zzz$ +zzz<??> ]] { { @@ -1672,7 +1672,7 @@ Cared['description'] = true TEST [[ --- abc zzz = 1 -zz$ +zz<??> ]] { { @@ -1695,7 +1695,7 @@ TEST [[ ---| "'选项2'" # 注释2 function f(x) end -f($) +f(<??>) ]] { { @@ -1711,7 +1711,7 @@ f($) } TEST [[ -utf8.charpatter$ +utf8.charpatter<??> ]] { { @@ -1726,7 +1726,7 @@ TEST [[ ---@type "'a'"|"'b'"|"'c'" local x -print(x == $) +print(x == <??>) ]] { { @@ -1747,7 +1747,7 @@ TEST [[ ---@type "'a'"|"'b'"|"'c'" local x -x = $ +x = <??> ]] { { @@ -1768,7 +1768,7 @@ TEST [[ ---@type "'a'"|"'b'"|"'c'" local x -print(x == '$') +print(x == '<??>') ]] { { @@ -1792,7 +1792,7 @@ TEST [[ ---@type "'a'"|"'b'"|"'c'" local x -x = '$' +x = '<??>' ]] { { @@ -1815,24 +1815,24 @@ x = '$' TEST [[ local t = type() -print(t == $) +print(t == <??>) ]] (EXISTS) TEST [[ -if type(arg) == '$' +if type(arg) == '<??>' ]] (EXISTS) TEST [[ -if type(arg) == $ +if type(arg) == <??> ]] (EXISTS) TEST [[ ---@type string local s -s.$ +s.<??> ]] (EXISTS) @@ -1841,7 +1841,7 @@ TEST [[ local t local vvv = assert(t) -vvv$ +vvv<??> ]] { { @@ -1857,7 +1857,7 @@ TEST [[ ---@param callback fun(x: number, y: number):string local function f(callback) end -f($) +f(<??>) ]] { { @@ -1871,7 +1871,7 @@ end", } TEST [[ ----$ +---<??> local function f(a, b, c) return a + 1, b .. '', c[1] end @@ -1894,7 +1894,7 @@ ${1:comment}\ Cared['insertText'] = nil TEST [[ ---$ +--<??> ]] { { @@ -1918,7 +1918,7 @@ TEST [[ local function f(x) end f({ - $ + <??> }) ]] { @@ -1942,7 +1942,7 @@ local function f(x) end f({ aaa = 1, - $ + <??> }) ]] { @@ -1960,7 +1960,7 @@ TEST [[ ---@param x cc local function f(x) end -f({aaa = 1,$}) +f({aaa = 1,<??>}) ]] { { @@ -1977,7 +1977,7 @@ TEST [[ ---@param x cc local function f(x) end -f({aaa $}) +f({aaa <??>}) ]] (nil) @@ -1988,7 +1988,7 @@ TEST [[ ---@param x cc local function f(x) end -f({if$}) +f({if<??>}) ]] { include = true, @@ -2008,7 +2008,7 @@ local function f(x) end f({ { - $ + <??> } }) ]] @@ -2020,7 +2020,7 @@ local function f() end local s = f() -s.$ +s.<??> ]] (EXISTS) @@ -2032,7 +2032,7 @@ TEST [[ ---@type cc local t -print(t.aa$) +print(t.aa<??>) ]] { { @@ -2050,7 +2050,7 @@ TEST [[ ---@type table<string, "'a'"|"'b'"|"'c'"> local x -x.a = $ +x.a = <??> ]] { { @@ -2071,7 +2071,7 @@ TEST [[ ---@type table<string, "'a'"|"'b'"|"'c'"> local x -x['a'] = $ +x['a'] = <??> ]] { { @@ -2091,7 +2091,7 @@ x['a'] = $ TEST [[ ---@type table<string, "'a'"|"'b'"|"'c'"> local x = { - a = $ + a = <??> } ]] { @@ -2112,7 +2112,7 @@ local x = { TEST [[ ---@type table<string, "'a'"|"'b'"|"'c'"> local x = { - ['a'] = $ + ['a'] = <??> } ]] { @@ -2138,7 +2138,7 @@ local m function m.f() end -m.f$ +m.f<??> ]]{ { label = "f()", @@ -2154,7 +2154,7 @@ m.f$ Cared['insertText'] = nil TEST [[ -if true then$ +if true then<??> ]] { { @@ -2168,7 +2168,7 @@ if true then$ } TEST [[ -if true then$ +if true then<??> end ]] { @@ -2179,7 +2179,7 @@ end } TEST [[ -if true then$ +if true then<??> else ]] { @@ -2190,7 +2190,7 @@ else } TEST [[ -if true then$ +if true then<??> elseif ]] { @@ -2202,7 +2202,7 @@ elseif TEST [[ do - if true then$ + if true then<??> end ]] { @@ -2226,7 +2226,7 @@ local function f(x, ...) end f(1, { - $ + <??> }) ]] { @@ -2250,7 +2250,7 @@ local function f(x, ...) end f(1, {}, {}, { - $ + <??> }) ]] { @@ -2271,7 +2271,7 @@ TEST [[ ---@type C local t = { - $ + <??> } ]] @@ -2293,7 +2293,7 @@ TEST [[ ---@type C local t = { - x$ + x<??> } ]] @@ -2306,19 +2306,19 @@ local t = { } TEST [[ -if $ then +if <??> then ]] (nil) TEST [[ -elseif $ then +elseif <??> then ]] (nil) TEST [[ ---@type iolib local t = { - $ + <??> ]] (EXISTS) @@ -2329,7 +2329,7 @@ TEST [[ ---@param t A function api(t) end -api({$}) +api({<??>}) ]] (EXISTS) @@ -2340,22 +2340,22 @@ TEST [[ ---@param t A function m:api(t) end -m:api({$}) +m:api({<??>}) ]] (EXISTS) TEST [[ ---@class AAA.BBB ----@type AAA.$ +---@type AAA.<??> ]] { { label = 'AAA.BBB', kind = define.CompletionItemKind.Class, textEdit = { - start = 29, - finish = 32, + start = 20009, + finish = 20013, newText = 'AAA.BBB', }, } @@ -2365,7 +2365,7 @@ Cared['insertText'] = true TEST [[ ---@overload fun(a: any, b: any) local function zzzz(a) end -zzzz$ +zzzz<??> ]] { { @@ -2406,13 +2406,13 @@ local tarray local b = tdirect -- type . here, shows "world" -- Inferred by index -local c = tarray[1].$ -- type . here, no auto completion +local c = tarray[1].<??> -- type . here, no auto completion ]] (EXISTS) TEST [[ local function f() - if type() == '$' then + if type() == '<??>' then end end ]] @@ -2425,7 +2425,7 @@ GGG = 1 GGG = function () end -GGG$ +GGG<??> ]] { { @@ -2446,7 +2446,7 @@ local t = {} t.GGG = function () end -t.GGG$ +t.GGG<??> ]] { { @@ -2463,7 +2463,7 @@ TEST [[ ---@param f fun(a: any, b: any):boolean local function f(f) end -f(fun$) +f(fun<??>) ]] { { @@ -2471,8 +2471,8 @@ f(fun$) kind = define.CompletionItemKind.Function, textEdit = { newText = 'function (${1:a}, ${2:b})\n\t$0\nend', - start = 68, - finish = 70, + start = 30002, + finish = 30005, } }, { @@ -2489,7 +2489,7 @@ TEST [[ ---@type {[1]: number} local t -t.$ +t.<??> ]] { { @@ -2497,13 +2497,13 @@ t.$ kind = define.CompletionItemKind.Field, textEdit = { newText = '[1]', - start = 35, - finish = 34, + start = 30002, + finish = 30002, }, additionalTextEdits = { { - start = 34, - finish = 34, + start = 30001, + finish = 30002, newText = '', }, }, @@ -2514,7 +2514,7 @@ TEST [[ ---@type {[1]: number} local t -t.$ +t.<??> ]] { { @@ -2522,13 +2522,13 @@ t.$ kind = define.CompletionItemKind.Field, textEdit = { newText = '[1]', - start = 35, - finish = 34, + start = 30002, + finish = 30002, }, additionalTextEdits = { { - start = 34, - finish = 34, + start = 30001, + finish = 30002, newText = '', }, }, @@ -2543,7 +2543,7 @@ TEST [[ local function f(x) end -local r = f('$') +local r = f('<??>') ]] { { @@ -2551,8 +2551,8 @@ local r = f('$') kind = define.CompletionItemKind.EnumMember, textEdit = { newText = "'aaa'", - start = 103, - finish = 104, + start = 70012, + finish = 70014, }, }, { @@ -2560,8 +2560,8 @@ local r = f('$') kind = define.CompletionItemKind.EnumMember, textEdit = { newText = "'bbb'", - start = 103, - finish = 104, + start = 70012, + finish = 70014, }, }, } @@ -2570,7 +2570,7 @@ TEST [[ ---@type fun(x: "'aaa'"|"'bbb'") local f -f('$') +f('<??>') ]] { { @@ -2578,8 +2578,8 @@ f('$') kind = define.CompletionItemKind.EnumMember, textEdit = { newText = "'aaa'", - start = 45, - finish = 46, + start = 30002, + finish = 30004, }, }, { @@ -2587,8 +2587,8 @@ f('$') kind = define.CompletionItemKind.EnumMember, textEdit = { newText = "'bbb'", - start = 45, - finish = 46, + start = 30002, + finish = 30004, }, }, } @@ -2598,7 +2598,7 @@ TEST [[ ---@field on fun() local c -c:$ +c:<??> ]] { { @@ -2612,7 +2612,7 @@ TEST [[ ---@field on fun(x: "'aaa'"|"'bbb'") local c -c:on($) +c:on(<??>) ]] (EXISTS) @@ -2621,7 +2621,7 @@ TEST [[ ---@field on fun(x: "'aaa'"|"'bbb'") local c -c:on('$') +c:on('<??>') ]] (EXISTS) @@ -2632,7 +2632,7 @@ function m.f() end m.f() -m.$ +m.<??> ]] { [1] = EXISTS, @@ -2647,7 +2647,7 @@ function class1:method1() end ---@class class2 : class1 class2 = {} -class2:$ +class2:<??> ]] { diff --git a/test/completion/continue.lua b/test/completion/continue.lua index 4159ea7a..63f970e9 100644 --- a/test/completion/continue.lua +++ b/test/completion/continue.lua @@ -9,7 +9,7 @@ ContinueTyping = true TEST [[ local zabcde -za$ +za<??> ]] { { @@ -20,7 +20,7 @@ za$ TEST [[ -- zabcde -io.z$ +io.z<??> ]] { { @@ -32,7 +32,7 @@ io.z$ TEST [[ -- provider -pro$ +pro<??> ]] { { @@ -45,7 +45,7 @@ TEST [[ ---@param n '"abcdefg"' local function f(n) end -f 'abc$' +f 'abc<??>' ]] { { @@ -59,7 +59,7 @@ TEST [[ ---@type '"abcdefg"' local t -if t == 'abc$' +if t == 'abc<??>' ]] { { diff --git a/test/completion/init.lua b/test/completion/init.lua index 24c8932f..051c288c 100644 --- a/test/completion/init.lua +++ b/test/completion/init.lua @@ -1,5 +1,6 @@ local core = require 'core.completion' local files = require 'files' +local catch = require 'catch' EXISTS = {'EXISTS'} @@ -64,17 +65,17 @@ ContinueTyping = false function TEST(script) return function (expect) files.removeAll() - local pos = script:find('$', 1, true) - 1 - local new_script = script:gsub('%$', '') + local newScript, catched = catch(script, '?') - files.setText('', new_script) + files.setText('', newScript) core.clearCache() + local inputPos = catched['?'][1][1] if ContinueTyping then - local triggerCharacter = script:sub(pos - 1, pos - 1) - core.completion('', pos - 1, triggerCharacter) + local triggerCharacter = script:sub(inputPos - 1, inputPos - 1) + core.completion('', inputPos, triggerCharacter) end - local triggerCharacter = script:sub(pos, pos) - local result = core.completion('', pos, triggerCharacter) + local triggerCharacter = script:sub(inputPos, inputPos) + local result = core.completion('', inputPos, triggerCharacter) if not expect then assert(result == nil) return diff --git a/test/crossfile/allreferences.lua b/test/crossfile/allreferences.lua index 056fa416..bddc159d 100644 --- a/test/crossfile/allreferences.lua +++ b/test/crossfile/allreferences.lua @@ -9,10 +9,9 @@ TEST { { path = 'lib.lua', content = [[ - return <~function~> () - end + return <!<?function?> () + end!> ]], - target = {22, 50}, }, } @@ -21,7 +20,7 @@ TEST { path = 'a.lua', content = [[ local m = {} - function m.<?func?>() + function m.<~func~>() end return m ]], @@ -39,7 +38,7 @@ TEST { { path = 'a.lua', content = [[ - return <?function () end?> + return <~function () end~> ]], }, { @@ -72,7 +71,7 @@ TEST { { path = 'a.lua', content = [[ - local function <?f?>() + local function <~f~>() end return { @@ -126,7 +125,7 @@ TEST { local t = require 'a' local <!f!> = t.<!f!> - <?f?>() + <~f~>() return { <!f!> = <!f!>, @@ -139,7 +138,7 @@ TEST { { path = 'a.lua', content = [[ - local function <?f?>() + local function <~f~>() end return { @@ -202,7 +201,7 @@ TEST { { path = 'a.lua', content = [[ - local <?t?> = require 'b' + local <~t~> = require 'b' return <!t!> ]] }, diff --git a/test/crossfile/completion.lua b/test/crossfile/completion.lua index d26398ca..9c5ed96e 100644 --- a/test/crossfile/completion.lua +++ b/test/crossfile/completion.lua @@ -4,6 +4,7 @@ local furi = require 'file-uri' local platform = require 'bee.platform' local util = require 'utility' local config = require 'config' +local catch = require 'catch' rawset(_G, 'TEST', true) @@ -88,8 +89,9 @@ function TEST(data) local uri = furi.encode(info.path) local script = info.content if info.main then - pos = script:find('$', 1, true) - 1 - script = script:gsub('%$', '') + local newScript, catched = catch(script, '?') + pos = catched['?'][1][1] + script = newScript mainUri = uri end files.setText(uri, script) @@ -139,7 +141,7 @@ TEST { }, { path = 'test.lua', - content = 'require "a$"', + content = 'require "a<??>"', main = true, }, completion = { @@ -178,7 +180,7 @@ TEST { }, { path = 'test.lua', - content = 'require "A$"', + content = 'require "A<??>"', main = true, }, completion = { @@ -201,7 +203,7 @@ TEST { }, { path = 'test.lua', - content = 'require "a$"', + content = 'require "a<??>"', main = true, }, completion = { @@ -229,7 +231,7 @@ TEST { }, { path = 'test.lua', - content = 'require "abc$"', + content = 'require "abc<??>"', main = true, }, completion = { @@ -257,7 +259,7 @@ TEST { }, { path = 'test.lua', - content = 'require "abc$"', + content = 'require "abc<??>"', main = true, }, completion = { @@ -290,7 +292,7 @@ TEST { }, { path = 'test.lua', - content = 'require "abc.i$"', + content = 'require "abc.i<??>"', main = true, }, completion = { @@ -315,7 +317,7 @@ TEST { }, { path = 'test.lua', - content = 'require "abc/i$"', + content = 'require "abc/i<??>"', main = true, }, completion = { @@ -339,7 +341,7 @@ TEST { }, { path = 'test.lua', - content = 'require "core.co$"', + content = 'require "core.co<??>"', main = true, }, completion = { @@ -362,7 +364,7 @@ TEST { }, { path = 'abc/test.lua', - content = 'require "x$"', + content = 'require "x<??>"', main = true, }, completion = { @@ -397,7 +399,7 @@ TEST { }, { path = 'main.lua', - content = 'require "x$"', + content = 'require "x<??>"', main = true, }, completion = { @@ -428,7 +430,7 @@ TEST { }, { path = 'main.lua', - content = 'require "x$"', + content = 'require "x<??>"', main = true, }, completion = { @@ -457,7 +459,7 @@ TEST { path = 'b.lua', content = [[ local t = require 'a' - t.$ + t.<??> ]], main = true, }, @@ -499,7 +501,7 @@ TEST { { path = 'b.lua', content = [[ - zab$ + zab<??> ]], main = true, }, @@ -533,7 +535,7 @@ TEST { { path = 'b.lua', content = [[ - zab$ + zab<??> ]], main = true, }, @@ -557,7 +559,7 @@ TEST { path = 'a.lua', content = [[ local japi = require 'jass.japi' - japi.xxxaaaax$ + japi.xxxaaaax<??> ]], main = true, }, @@ -571,7 +573,7 @@ TEST { { path = 'xxxx.lua', content = [[ - require 'xx$' + require 'xx<??>' ]], main = true, }, @@ -592,7 +594,7 @@ TEST { { path = 'main.lua', content = [[ - require 'xx$' + require 'xx<??>' ]], main = true, }, @@ -613,7 +615,7 @@ TEST { { path = 'main.lua', content = [[ - require [=[xx$]=]' + require [=[xx<??>]=]' ]], main = true, }, @@ -635,7 +637,7 @@ TEST { { path = 'main.lua', content = [[ - dofile 'ab$' + dofile 'ab<??>' ]], main = true, }, @@ -656,7 +658,7 @@ TEST { { path = 'main.lua', content = [[ - dofile 'ab$' + dofile 'ab<??>' ]], main = true, }, @@ -686,7 +688,7 @@ TEST { content = [[ local t = require 'a' local v = setmetatable({}, {__index = t}) - v.$ + v.<??> ]] }, completion = { @@ -712,7 +714,7 @@ TEST { content = [[ local z = require 'a' - z$ + z<??> ]], main = true, }, @@ -743,7 +745,7 @@ TEST { path = 'main.lua', main = true, content = [[ - myfun$ + myfun<??> ]], }, completion = { @@ -774,7 +776,7 @@ TEST { path = 'main.lua', main = true, content = [[ - myfun$ + myfun<??> ]], }, completion = { @@ -807,7 +809,7 @@ TEST { path = 'main.lua', main = true, content = [[ - A.$ + A.<??> ]], }, completion = EXISTS, @@ -819,7 +821,7 @@ TEST { path = 'main.lua', main = true, content = [[ - require'$ + require'<??> ]] }, completion = EXISTS diff --git a/test/crossfile/definition.lua b/test/crossfile/definition.lua index 66e9b269..01bf0202 100644 --- a/test/crossfile/definition.lua +++ b/test/crossfile/definition.lua @@ -3,26 +3,10 @@ local furi = require 'file-uri' local core = require 'core.definition' local config = require 'config' local platform = require 'bee.platform' +local catch = require 'catch' 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 @@ -50,32 +34,26 @@ function TEST(datas) 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 + local newScript, catched = catch(data.content, '!?~') + for _, position in ipairs(catched['!'] or {}) do + targetList[#targetList+1] = { + position[1], + position[2], + uri, + } + end + for _, position in ipairs(catched['~'] or {}) do + targetList[#targetList+1] = { + position[1], + position[2], + uri, + } end - new, list = catch_target(data.content, '?') - if new ~= data.content then - sourceList = list + if catched['?'] or catched['~'] then + sourceList = catched['?'] or catched['~'] sourceUri = uri - data.content = new end - files.setText(uri, data.content) + files.setText(uri, newScript) end local sourcePos = (sourceList[1][1] + sourceList[1][2]) // 2 @@ -98,8 +76,7 @@ end TEST { { path = 'a.lua', - content = '', - target = {0, 0}, + content = '<!!>', }, { path = 'b.lua', @@ -110,8 +87,7 @@ TEST { TEST { { path = 'aaa/bbb.lua', - content = '', - target = {0, 0}, + content = '<!!>', }, { path = 'b.lua', @@ -122,8 +98,7 @@ TEST { TEST { { path = '@bbb.lua', - content = '', - target = {0, 0}, + content = '<!!>', }, { path = 'b.lua', @@ -134,8 +109,7 @@ TEST { TEST { { path = 'aaa/bbb.lua', - content = '', - target = {0, 0}, + content = '<!!>', }, { path = 'b.lua', @@ -150,8 +124,7 @@ TEST { }, { path = 'b.lua', - content = 'local <?t?> = require "a"', - target = {7, 7}, + content = 'local <~t~> = require "a"', }, } @@ -176,8 +149,7 @@ TEST { }, { path = 'b.lua', - content = 'local <?t?> = require "a"', - target = {7, 7}, + content = 'local <~t~> = require "a"', }, } @@ -245,9 +217,8 @@ TEST { { path = 'b.lua', content = [[ - local <?t?> = require 'a' + local <~t~> = require 'a' ]], - target = {19, 19}, }, } diff --git a/test/crossfile/diagnostic.lua b/test/crossfile/diagnostic.lua index a430cb4e..adc2961f 100644 --- a/test/crossfile/diagnostic.lua +++ b/test/crossfile/diagnostic.lua @@ -3,28 +3,12 @@ local furi = require 'file-uri' local core = require 'core.diagnostics' local config = require 'config' local platform = require 'bee.platform' +local catch = require 'catch' config.get 'Lua.diagnostics.neededFileStatus'['deprecated'] = 'Any' 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 @@ -48,19 +32,18 @@ function TEST(datas) files.removeAll() local targetList = {} - local sourceUri for _, data in ipairs(datas) do local uri = furi.encode(data.path) - local new, list = catch_target(data.content, '!') - for _, position in ipairs(list) do + local newScript, catched = catch(data.content, '!') + for _, position in ipairs(catched['!'] or {}) do targetList[#targetList+1] = { position[1], position[2], uri, } end - data.content = new - files.setText(uri, new) + data.content = newScript + files.setText(uri, newScript) end local result = {} diff --git a/test/crossfile/hover.lua b/test/crossfile/hover.lua index a877d226..1c46214c 100644 --- a/test/crossfile/hover.lua +++ b/test/crossfile/hover.lua @@ -2,6 +2,7 @@ local files = require 'files' local furi = require 'file-uri' local core = require 'core.hover' local config = require 'config' +local catch = require 'catch' rawset(_G, 'TEST', true) @@ -36,36 +37,19 @@ local function eq(a, b) 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 sourceScript, sourceList = catch(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 sourcePos = (sourceList['?'][1][1] + sourceList['?'][1][2]) // 2 local hover = core.byUri(sourceUri, sourcePos) assert(hover) hover = tostring(hover):gsub('\r\n', '\n') diff --git a/test/crossfile/references.lua b/test/crossfile/references.lua index 6c28b34b..145792b5 100644 --- a/test/crossfile/references.lua +++ b/test/crossfile/references.lua @@ -1,6 +1,7 @@ local files = require 'files' local furi = require 'file-uri' local core = require 'core.reference' +local catch = require 'catch' rawset(_G, 'TEST', true) @@ -32,21 +33,6 @@ local function eq(a, b) 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 @@ -74,37 +60,9 @@ function TEST(datas) 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 + local newScript, catched = catch(data.content, '!?~') + if catched['!'] or catched['~'] then + for _, position in ipairs(catched['!'] + catched['~']) do targetList[#targetList+1] = { position[1], position[2], @@ -112,7 +70,11 @@ function TEST(datas) } end end - files.setText(uri, data.content) + if catched['?'] or catched['~'] then + sourceList = catched['?'] + catched['~'] + sourceUri = uri + end + files.setText(uri, newScript) end local sourcePos = (sourceList[1][1] + sourceList[1][2]) // 2 @@ -143,7 +105,7 @@ TEST { { path = 'a.lua', content = [[ - local <?f?> = require 'lib' + local <~f~> = require 'lib' ]], }, } @@ -158,7 +120,7 @@ TEST { { path = 'b.lua', content = [[ - print(<?ROOT?>) + print(<~ROOT~>) ]], }, } @@ -167,7 +129,7 @@ TEST { { path = 'a.lua', content = [[ - <?ROOT?> = 1 + <~ROOT~> = 1 ]], }, { @@ -183,7 +145,7 @@ TEST { path = 'a.lua', content = [[ local f = require 'lib' - local <?o?> = f() + local <~o~> = f() ]], }, { @@ -212,7 +174,7 @@ TEST { ---@class A local mt - function mt.<?f?>() + function mt.<~f~>() end ]] } diff --git a/test/definition/init.lua b/test/definition/init.lua index 85bcd5d5..2e87b0c2 100644 --- a/test/definition/init.lua +++ b/test/definition/init.lua @@ -1,23 +1,10 @@ local core = require 'core.definition' local files = require 'files' local vm = require 'vm' +local catch = require 'catch' rawset(_G, 'TEST', true) -local function catch_target(script) - local list = {} - local cur = 1 - while true do - local start, finish = script:find('<!.-!>', cur) - if not start then - break - end - list[#list+1] = { start + 2, finish - 2 } - cur = finish + 1 - end - return list -end - local function founded(targets, results) if #targets ~= #results then return false @@ -36,15 +23,11 @@ end function TEST(script) files.removeAll() - script = script:gsub('\n', '\r\n') - local target = catch_target(script) - local start = script:find('<?', 1, true) - local finish = script:find('?>', 1, true) - local pos = (start + finish) // 2 + 1 - local new_script = script:gsub('<[!?]', ' '):gsub('[!?]>', ' ') - files.setText('', new_script) + local newScript, catched = catch(script, '!?') - local results = core('', pos) + files.setText('', newScript) + + local results = core('', catched['?'][1][1]) if results then local positions = {} for i, result in ipairs(results) do @@ -52,15 +35,9 @@ function TEST(script) positions[i] = { result.target.start, result.target.finish } end end - if not founded(target, positions) then - core('', pos) - end - assert(founded(target, positions)) + assert(founded(catched['!'] or {}, positions)) else - if #target ~= 0 then - core('', pos) - end - assert(#target == 0) + assert(catched['!'] == nil) end end diff --git a/test/definition/table.lua b/test/definition/table.lua index d63cc655..61e8746d 100644 --- a/test/definition/table.lua +++ b/test/definition/table.lua @@ -54,7 +54,7 @@ t[<?"method"?>]() TEST [[ local t t[<!"longString"!>] = 1 -t[<?[==[longString]==]?>]() +t[ <?[==[longString]==]?> ]() ]] TEST [[ diff --git a/test/diagnostics/init.lua b/test/diagnostics/init.lua index 79b267c3..e3bf9516 100644 --- a/test/diagnostics/init.lua +++ b/test/diagnostics/init.lua @@ -2,36 +2,12 @@ local core = require 'core.diagnostics' local files = require 'files' local config = require 'config' local util = require 'utility' +local catch = require 'catch' config.get 'Lua.diagnostics.neededFileStatus'['deprecated'] = 'Any' rawset(_G, 'TEST', true) -local function catch_target(script, ...) - local list = {} - local function catch(buf) - local cur = 1 - local cut = 0 - while true do - local start, finish = buf:find('<!.-!>', cur) - if not start then - break - end - list[#list+1] = { start - cut, finish - 4 - cut } - cur = finish + 1 - cut = cut + 4 - end - end - catch(script) - if ... then - for _, buf in ipairs {...} do - catch(buf) - end - end - local new_script = script:gsub('<!(.-)!>', '%1') - return new_script, list -end - local function founded(targets, results) if #targets ~= #results then return false @@ -50,8 +26,8 @@ end function TEST(script, ...) files.removeAll() - local new_script, target = catch_target(script, ...) - files.setText('', new_script) + local newScript, catched = catch(script, '!') + files.setText('', newScript) files.open('') local datas = {} core('', function (results) @@ -65,11 +41,11 @@ function TEST(script, ...) end if results[1] then - if not founded(target, results) then - error(('%s\n%s'):format(util.dump(target), util.dump(results))) + if not founded(catched['!'] or {}, results) then + error(('%s\n%s'):format(util.dump(catched['!']), util.dump(results))) end else - assert(#target == 0) + assert(catched['!'] == nil) end end @@ -99,15 +75,11 @@ local <!t!> = {} <!t!>.a = 1 ]] -TEST([[ -local <!function x() +TEST [[ +local <!function <!x!>() end!> -]], -[[ -local function <!x!>() -end ]] -) + TEST [[ local <!x!> = <!function () end!> @@ -118,21 +90,13 @@ local <!x!> <!x!> = <!function () end!> ]] -TEST([[ +TEST [[ local <!function x() end!> -local <!function y() +local <!function <!y!>() x() end!> -]], -[[ -local function x() -end -local function <!y!>() - x() -end ]] -) TEST [[ local print, _G @@ -155,6 +119,11 @@ TEST [[ ]] TEST [[ + +<! !> +]] + +TEST [[ X = 1<! !> ]] diff --git a/test/document_symbol/init.lua b/test/document_symbol/init.lua index d745d53f..d3168197 100644 --- a/test/document_symbol/init.lua +++ b/test/document_symbol/init.lua @@ -63,8 +63,8 @@ A = 1 name = 'A', detail = 'global number = 1', kind = define.SymbolKind.Class, - range = {1, 5}, - selectionRange = {1, 1}, + range = {0, 5}, + selectionRange = {0, 1}, } } @@ -77,9 +77,9 @@ end name = 'f', detail = 'function ()', kind = define.SymbolKind.Function, - range = {7, 22}, - selectionRange = {16, 16}, - valueRange = {7, 22}, + range = {6, 10003}, + selectionRange = {15, 16}, + valueRange = {6, 10003}, } } @@ -92,9 +92,9 @@ end name = 'f', detail = 'function ()', kind = define.SymbolKind.Function, - range = {1, 16}, - selectionRange = {10, 10}, - valueRange = {1, 16}, + range = {0, 10003}, + selectionRange = {9, 10}, + valueRange = {0, 10003}, } } @@ -107,9 +107,9 @@ end name = '', detail = 'return function ()', kind = define.SymbolKind.Function, - range = {8, 22}, - selectionRange = {8, 8}, - valueRange = {8, 22}, + range = {7, 10003}, + selectionRange = {7, 15}, + valueRange = {7, 10003}, } } @@ -122,9 +122,9 @@ end name = 'f', detail = 'function ()', kind = define.SymbolKind.Function, - range = {1, 19}, - selectionRange = {1, 1}, - valueRange = {5, 19}, + range = {0, 10003}, + selectionRange = {0, 1}, + valueRange = {4, 10003}, } } @@ -137,9 +137,9 @@ end name = 'f', detail = 'function ()', kind = define.SymbolKind.Function, - range = {7, 25}, - selectionRange = {7, 7}, - valueRange = {11, 25}, + range = {6, 10003}, + selectionRange = {6, 7}, + valueRange = {10, 10003}, } } @@ -152,9 +152,9 @@ end name = 'mt:add', detail = 'function ()', kind = define.SymbolKind.Method, - range = {1, 21}, - selectionRange = {10, 15}, - valueRange = {1, 21}, + range = {0, 10003}, + selectionRange = {9, 15}, + valueRange = {0, 10003}, } } @@ -173,25 +173,25 @@ end name = 'A', detail = 'function ()', kind = define.SymbolKind.Function, - range = {1, 68}, - selectionRange = {10, 10}, - valueRange = {1, 68}, + range = {0, 50003}, + selectionRange = {9, 10}, + valueRange = {0, 50003}, children = { [1] = { name = 'A1', detail = 'function ()', kind = define.SymbolKind.Function, - range = {18, 38}, - selectionRange = {27, 28}, - valueRange = {18, 38}, + range = {10004, 20007}, + selectionRange = {10013, 10015}, + valueRange = {10004, 20007}, }, [2] = { name = 'A2', detail = 'function ()', kind = define.SymbolKind.Function, - range = {44, 64}, - selectionRange = {53, 54}, - valueRange = {44, 64}, + range = {30004, 40007}, + selectionRange = {30013, 30015}, + valueRange = {30004, 40007}, }, }, }, @@ -199,9 +199,9 @@ end name = 'B', detail = 'function ()', kind = define.SymbolKind.Function, - range = {70, 85}, - selectionRange = {79, 79}, - valueRange = {70, 85}, + range = {60000, 70003}, + selectionRange = {60009, 60010}, + valueRange = {60000, 70003}, }, } @@ -220,31 +220,31 @@ local z name = 'x', detail = 'local number = 1', kind = define.SymbolKind.Variable, - range = {7, 11}, - selectionRange = {7, 7}, + range = {6, 11}, + selectionRange = {6, 7}, }, [2] = { name = 'f', detail = 'function ()', kind = define.SymbolKind.Function, - range = {19, 79}, - selectionRange = {28, 28}, - valueRange = {19, 79}, + range = {10006, 50003}, + selectionRange = {10015, 10016}, + valueRange = {10006, 50003}, children = { [1] = { name = 'x', detail = 'local string = "x"', kind = define.SymbolKind.Variable, - range = {42, 48}, - selectionRange = {42, 42}, + range = {20010, 20017}, + selectionRange = {20010, 20011}, }, [2] = { name = 'y', detail = 'local {}', kind = define.SymbolKind.Variable, - range = {60, 65}, - selectionRange = {60, 60}, - valueRange = {64, 65}, + range = {30010, 30016}, + selectionRange = {30010, 30011}, + valueRange = {30014, 30016}, }, --[3] = { -- name = 'z', @@ -260,15 +260,15 @@ local z name = 'y', detail = 'local boolean = true', kind = define.SymbolKind.Variable, - range = {87, 94}, - selectionRange = {87, 87}, + range = {60006, 60014}, + selectionRange = {60006, 60007}, }, [4] = { name = 'z', detail = 'local', kind = define.SymbolKind.Variable, - range = {102, 102}, - selectionRange = {102, 102}, + range = {70006, 70007}, + selectionRange = {70006, 70007}, }, } @@ -284,30 +284,30 @@ local t = { name = 't', detail = 'local {a, b, c}', kind = define.SymbolKind.Variable, - range = {7, 46}, - selectionRange = {7, 7}, - valueRange = {11, 46}, + range = {6, 40001}, + selectionRange = {6, 7}, + valueRange = {10, 40001}, children = { [1] = { name = 'a', detail = 'field number = 1', kind = define.SymbolKind.Property, - range = {17, 21}, - selectionRange = {17, 17}, + range = {10004, 10009}, + selectionRange = {10004, 10005}, }, [2] = { name = 'b', detail = 'field number = 2', kind = define.SymbolKind.Property, - range = {28, 32}, - selectionRange = {28, 28}, + range = {20004, 20009}, + selectionRange = {20004, 20005}, }, [3] = { name = 'c', detail = 'field number = 3', kind = define.SymbolKind.Property, - range = {39, 43}, - selectionRange = {39, 39}, + range = {30004, 30009}, + selectionRange = {30004, 30005}, }, } } @@ -325,24 +325,24 @@ local t = { name = 't', detail = 'local {a}', kind = define.SymbolKind.Variable, - range = {7, 44}, - selectionRange = {7, 7}, - valueRange = {11, 44}, + range = {6, 40001}, + selectionRange = {6, 7}, + valueRange = {10, 40001}, children = { [1] = { name = 'a', detail = 'field {b}', kind = define.SymbolKind.Property, - range = {17, 42}, - selectionRange = {17, 17}, - valueRange = {21, 42}, + range = {10004, 30005}, + selectionRange = {10004, 10005}, + valueRange = {10008, 30005}, children = { [1] = { name = 'b', detail = EXISTS, kind = define.SymbolKind.Property, - range = {31, 35}, - selectionRange = {31, 31}, + range = {20008, 20013}, + selectionRange = {20008, 20009}, } } }, @@ -360,16 +360,16 @@ g = 1 name = 'g', detail = 'function ()', kind = define.SymbolKind.Function, - range = {7, 22}, - selectionRange = {16, 16}, - valueRange = {7, 22}, + range = {6, 10003}, + selectionRange = {15, 16}, + valueRange = {6, 10003}, }, [2] = { name = 'g', detail = 'setlocal number = 1', kind = define.SymbolKind.Variable, - range = {25, 29}, - selectionRange = {25, 25}, + range = {30000, 30005}, + selectionRange = {30000, 30001}, } } @@ -383,30 +383,30 @@ end name = 'f', detail = 'function (a, b, ...)', kind = define.SymbolKind.Function, - range = {1, 58}, - selectionRange = {10, 10}, - valueRange = {1, 58}, + range = {0, 30003}, + selectionRange = {9, 10}, + valueRange = {0, 30003}, children = { [1] = { name = 'a', detail = 'param', kind = define.SymbolKind.Constant, - range = {12, 12}, - selectionRange = {12, 12}, + range = {11, 12}, + selectionRange = {11, 12}, }, [2] = { name = 'b', detail = 'param', kind = define.SymbolKind.Constant, - range = {15, 15}, - selectionRange = {15, 15}, + range = {14, 15}, + selectionRange = {14, 15}, }, [3] = { name = 'x', detail = 'local', kind = define.SymbolKind.Variable, - range = {33, 39}, - selectionRange = {33, 33}, + range = {10010, 10017}, + selectionRange = {10010, 10011}, } } }, @@ -424,17 +424,17 @@ local v = t name = 't', detail = 'local {a, b}', kind = define.SymbolKind.Variable, - range = {7, 35}, - selectionRange = {7, 7}, - valueRange = {11, 35}, + range = {6, 30001}, + selectionRange = {6, 7}, + valueRange = {10, 30001}, children = EXISTS, }, [2] = { name = 'v', detail = 'local', kind = define.SymbolKind.Variable, - range = {44, 48}, - selectionRange = {44, 44}, + range = {50006, 50011}, + selectionRange = {50006, 50007}, }, } @@ -446,47 +446,47 @@ local function name = 'x', detail = 'local', kind = define.SymbolKind.Variable, - range = {7, 7}, - selectionRange = {7, 7}, + range = {6, 7}, + selectionRange = {6, 7}, }, [2] = { name = "", detail = "function ()", kind = 12, - range = {15, 22}, - selectionRange = {15, 15}, - valueRange = {15, 22}, + range = {10006, 10014}, + selectionRange = {10006, 10014}, + valueRange = {10006, 10014}, }, } TEST [[ local a, b = { - x = 1, - y = 1, - z = 1, + x1 = 1, + y1 = 1, + z1 = 1, }, { - x = 1, - y = 1, - z = 1, + x2 = 1, + y2= 1, + z2 = 1, } ]]{ [1] = { name = 'a', - detail = 'local {x, y, z}', + detail = 'local {x1, y1, z1}', kind = define.SymbolKind.Variable, - range = {7, 49}, - selectionRange = {7, 7}, - valueRange = {14, 49}, + range = {6, 40001}, + selectionRange = {6, 7}, + valueRange = {13, 40001}, children = EXISTS, }, [2] = { name = 'b', - detail = 'local {x, y, z}', + detail = 'local {x2, y2, z2}', kind = define.SymbolKind.Variable, - range = {10, 87}, - selectionRange = {10, 10}, - valueRange = {52, 87}, + range = {9, 80001}, + selectionRange = {9, 10}, + valueRange = {40003, 80001}, children = EXISTS, } } @@ -504,24 +504,24 @@ end name = 'x', detail = 'function ()', kind = define.SymbolKind.Function, - range = {7, 22}, - selectionRange = {16, 16}, - valueRange = {7, 22}, + range = {6, 10003}, + selectionRange = {15, 16}, + valueRange = {6, 10003}, }, [2] = { name = 'f', detail = 'function ()', kind = define.SymbolKind.Function, - range = {31, 58}, - selectionRange = {40, 40}, - valueRange = {31, 58}, + range = {30006, 50003}, + selectionRange = {30015, 30016}, + valueRange = {30006, 50003}, children = { [1] = { name = 'c', detail = 'local', kind = define.SymbolKind.Variable, - range = {54, 54}, - selectionRange = {54, 54}, + range = {40010, 40011}, + selectionRange = {40010, 40011}, }, }, } @@ -537,16 +537,16 @@ local t = f({ name = 't', detail = 'local', kind = define.SymbolKind.Variable, - range = {7, 26}, - selectionRange = {7, 7}, - valueRange = {11, 26}, + range = {6, 20002}, + selectionRange = {6, 7}, + valueRange = {10, 20002}, children = { [1] = { name = 'k', detail = 'field number = 1', kind = define.SymbolKind.Property, - range = {19, 23}, - selectionRange = {19, 19}, + range = {10004, 10009}, + selectionRange = {10004, 10005}, } } } @@ -563,31 +563,31 @@ end name = 't', detail = 'local {}', kind = define.SymbolKind.Variable, - range = {7, 12}, - selectionRange = {7, 7}, - valueRange = {11, 12}, + range = {6, 12}, + selectionRange = {6, 7}, + valueRange = {10, 12}, }, [2] = { name = 'f', detail = 'function (a, b)', kind = define.SymbolKind.Function, - range = {21, 40}, - selectionRange = {30, 30}, - valueRange = {21, 40}, + range = {20006, 30003}, + selectionRange = {20015, 20016}, + valueRange = {20006, 30003}, children = { [1] = { name = 'a', detail = 'param', kind = define.SymbolKind.Constant, - range = {32, 32}, - selectionRange = {32, 32}, + range = {20017, 20018}, + selectionRange = {20017, 20018}, }, [2] = { name = 'b', detail = 'param', kind = define.SymbolKind.Constant, - range = {35, 35}, - selectionRange = {35, 35}, + range = {20020, 20021}, + selectionRange = {20020, 20021}, } } } @@ -604,17 +604,17 @@ local a = f { name = 'a', detail = 'local', kind = define.SymbolKind.Variable, - range = {7, 43}, - selectionRange = {7, 7}, - valueRange = {11, 43}, + range = {6, 30001}, + selectionRange = {6, 7}, + valueRange = {10, 30001}, children = { [1] = { name = 'x', detail = 'function ()', kind = define.SymbolKind.Function, - range = {19, 41}, - selectionRange = {19, 19}, - valueRange = {23, 41}, + range = {10004, 20007}, + selectionRange = {10004, 10005}, + valueRange = {10008, 20007}, } } } @@ -630,9 +630,9 @@ end) name = '', detail = 'table.sort -> function (a, b)', kind = define.SymbolKind.Function, - range = {15, 50}, - selectionRange = {15, 15}, - valueRange = {15, 50}, + range = {14, 20003}, + selectionRange = {14, 22}, + valueRange = {14, 20003}, children = EXISTS, } } @@ -643,32 +643,32 @@ local root = { local function function_inside_function() end end - } +} ]] { [1] = { name = 'root', detail = 'local {inner_function}', kind = define.SymbolKind.Variable, - range = {7, 123}, - selectionRange = {7, 10}, - valueRange = {14, 123}, + range = {6, 50001}, + selectionRange = {6, 10}, + valueRange = {13, 50001}, children = { [1] = { name = 'inner_function', detail = 'function ()', kind = define.SymbolKind.Function, - range = {20, 117}, - selectionRange = {20, 33}, - valueRange = {37, 117}, + range = {10004, 40007}, + selectionRange = {10004, 10018}, + valueRange = {10021, 40007}, children = { [1] = { name = 'function_inside_function', detail = 'function ()', kind = define.SymbolKind.Function, - range = {63, 109}, - selectionRange = {72, 95}, - valueRange = {63, 109}, + range = {20014, 30011}, + selectionRange = {20023, 20047}, + valueRange = {20014, 30011}, }, }, }, diff --git a/test/example/guide.txt b/test/example/guide.txt index da8d5c32..cff05faf 100644 --- a/test/example/guide.txt +++ b/test/example/guide.txt @@ -586,7 +586,7 @@ end ---@param lines table ---@return integer {name = 'row'} ---@return integer {name = 'col'} -function m.positionOf(lines, offset) +function m.rowColOf(lines, offset) if offset < 1 then return 0, 0 end @@ -619,7 +619,7 @@ end ---@param row integer ---@param col integer ---@return integer {name = 'offset'} -function m.offsetOf(lines, row, col) +function m.positionOf(lines, row, col) if row < 1 then return 0 end diff --git a/test/full/example.lua b/test/full/example.lua index 5596233e..20de5528 100644 --- a/test/full/example.lua +++ b/test/full/example.lua @@ -12,26 +12,24 @@ local function testIfExit(path) config.set('Lua.workspace.preloadFileSize', 1000000000) local buf = util.loadFile(path:string()) if buf then - local vm + local state local clock = os.clock() local max = 1 local need - local parseClock = 0 local compileClock = 0 local luadocClock = 0 local noderClock = 0 local total for i = 1, max do - vm = TEST(buf) + state = TEST(buf) local luadocStart = os.clock() - luadoc(nil, vm) + luadoc(state) local luadocPassed = os.clock() - luadocStart local passed = os.clock() - clock local noderStart = os.clock() local noderPassed = os.clock() - noderStart - parseClock = parseClock + vm.parseClock - compileClock = compileClock + vm.compileClock + compileClock = compileClock + state.compileClock luadocClock = luadocClock + luadocPassed noderClock = noderClock + noderPassed if passed >= 1.0 or i == max then @@ -40,10 +38,9 @@ local function testIfExit(path) break end end - print(('基准编译测试[%s]单次耗时:%.10f(解析:%.10f, 编译:%.10f, LuaDoc: %.10f, Noder: %.10f)'):format( + print(('基准编译测试[%s]单次耗时:%.10f(解析:%.10f, LuaDoc: %.10f, Noder: %.10f)'):format( path:filename():string(), need, - parseClock / total, compileClock / total, luadocClock / total, noderClock / total @@ -52,7 +49,6 @@ local function testIfExit(path) local clock = os.clock() local max = 100 local need - local lines = parser:lines(buf) for i = 1, max do files.removeAll() files.open('') diff --git a/test/full/init.lua b/test/full/init.lua index 89d9b8b3..9584db6f 100644 --- a/test/full/init.lua +++ b/test/full/init.lua @@ -5,9 +5,10 @@ local util = require 'utility' rawset(_G, 'TEST', true) function TEST(script) - local ast = parser:compile(script, 'lua', 'Lua 5.3') - assert(ast) - return ast + local clock = os.clock() + local state = parser.compile(script, 'Lua', 'Lua 5.3') + state.compileClock = os.clock() - clock + return state end local function startCollectDiagTimes() diff --git a/test/highlight/init.lua b/test/highlight/init.lua index 2bf639fa..7faa9e08 100644 --- a/test/highlight/init.lua +++ b/test/highlight/init.lua @@ -1,22 +1,6 @@ local core = require 'core.highlight' local files = require 'files' - -local function catch_target(script) - local list = {} - local cur = 1 - while true do - local start, finish = script:find('<[!?].-[!?]>', cur) - if not start then - break - end - list[#list+1] = { - start = start + 2, - finish = finish - 2, - } - cur = finish + 1 - end - return list -end +local catch = require 'catch' local function founded(targets, results) if #targets ~= #results then @@ -35,20 +19,18 @@ local function founded(targets, results) end function TEST(script) - local target = catch_target(script) - for _, enter in ipairs(target) do - local start, finish = enter.start, enter.finish - files.removeAll() + files.removeAll() + local newScript, catched = catch(script, '!') + files.setText('', newScript) + for _, enter in ipairs(catched['!']) do + local start, finish = enter[1], enter[2] local pos = (start + finish) // 2 - local new_script = script:gsub('<[!?~]', ' '):gsub('[!?~]>', ' ') - files.setText('', new_script) - local positions = core('', pos) - if positions then - assert(founded(target, positions)) - else - assert(#target == 0) + local results = {} + for _, position in ipairs(positions) do + results[#results+1] = { position.start, position.finish } end + assert(founded(catched['!'], results)) end end diff --git a/test/hover/init.lua b/test/hover/init.lua index 3f07ea30..8058317d 100644 --- a/test/hover/init.lua +++ b/test/hover/init.lua @@ -1,7 +1,6 @@ local core = require 'core.hover' -local findSource = require 'core.find-source' -local getLabel = require 'core.hover.label' local files = require 'files' +local catch = require 'catch' rawset(_G, 'TEST', true) @@ -23,12 +22,9 @@ local accept = { function TEST(script) return function (expect) files.removeAll() - local start = script:find('<?', 1, true) - local finish = script:find('?>', 1, true) - local pos = (start + finish) // 2 + 1 - local new_script = script:gsub('<[!?]', ' '):gsub('[!?]>', ' ') - files.setText('', new_script) - local hover = core.byUri('', pos) + local newScript, catched = catch(script, '?') + files.setText('', newScript) + local hover = core.byUri('', catched['?'][1][1]) assert(hover) expect = expect:gsub('^[\r\n]*(.-)[\r\n]*$', '%1'):gsub('\r\n', '\n') local label = tostring(hover):match('```lua[\r\n]*(.-)[\r\n]*```'):gsub('\r\n', '\n') @@ -1703,3 +1699,18 @@ print(b.<?x?>) [[ field A.x: any ]] + +TEST [[ +---@class A +---@field x number +---@field y number + +---@type A<string, number> +local <?t?> +]] +[[ +local t: A<string, number> { + x: number, + y: number, +} +]] diff --git a/test/references/common.lua b/test/references/common.lua index 3669486b..4245a79d 100644 --- a/test/references/common.lua +++ b/test/references/common.lua @@ -221,30 +221,12 @@ TEST [[ ---@class A local a -a.<!xxx!> = 1 - ---@type A local b -b.xxx = 1 -- Dont search this - ----@class C: A -local c -print(c.<?xxx?>) -]] - -TEST [[ ----@class A -local <?a?> - ----@class B -local b - -b.xx = 1 -- Dont search this - ----@type B +---@type A local c ----@type A -c.<!xx!> = 1 +b.<?x?> = 1 +c.<!x!> = 1 ]] diff --git a/test/references/init.lua b/test/references/init.lua index 2fba92e5..351c80b6 100644 --- a/test/references/init.lua +++ b/test/references/init.lua @@ -1,19 +1,6 @@ local core = require 'core.reference' local files = require 'files' - -local function catch_target(script) - local list = {} - local cur = 1 - while true do - local start, finish = script:find('<[!?].-[!?]>', cur) - if not start then - break - end - list[#list+1] = { start + 2, finish - 2 } - cur = finish + 1 - end - return list -end +local catch = require 'catch' local function founded(targets, results) if #targets ~= #results then @@ -33,14 +20,12 @@ end function TEST(script) files.removeAll() - local expect = catch_target(script) - local start = script:find('<[?~]') - local finish = script:find('[?~]>') - local pos = (start + finish) // 2 + 1 - local new_script = script:gsub('<[!?~]', ' '):gsub('[!?~]>', ' ') - files.setText('', new_script) + local newScript, catched = catch(script, '!?~') + files.setText('', newScript) - local results = core('', pos) + local input = catched['?'] + catched['~'] + local expect = catched['!'] + catched['?'] + local results = core('', input[1][1]) if results then local positions = {} for i, result in ipairs(results) do diff --git a/test/rename/init.lua b/test/rename/init.lua index 4b10756e..b20c0279 100644 --- a/test/rename/init.lua +++ b/test/rename/init.lua @@ -1,16 +1,21 @@ local core = require 'core.rename' local files = require 'files' +local catch = require 'catch' +local guide = require 'parser.guide' local function replace(text, positions) + local state = files.getState('') local buf = {} table.sort(positions, function (a, b) return a.start < b.start end) local lastPos = 1 for _, info in ipairs(positions) do - buf[#buf+1] = text:sub(lastPos, info.start - 1) + local start = guide.positionToOffset(state, info.start) + local finish = guide.positionToOffset(state, info.finish) + buf[#buf+1] = text:sub(lastPos, start) buf[#buf+1] = info.text - lastPos = info.finish + 1 + lastPos = finish + 1 end buf[#buf+1] = text:sub(lastPos) return table.concat(buf) @@ -21,10 +26,12 @@ function TEST(oldName, newName) return function (expectScript) files.removeAll() files.setText('', oldScript) - local pos = oldScript:find('[^%w_]'..oldName..'[^%w_]') - assert(pos) + local state = files.getState('') + local offset = oldScript:find('[^%w_]'..oldName..'[^%w_]') + assert(offset) + local position = guide.offsetToPosition(state, offset) - local positions = core.rename('', pos+1, newName) + local positions = core.rename('', position, newName) local script = oldScript if positions then script = replace(script, positions) diff --git a/test/signature/init.lua b/test/signature/init.lua index 27051310..43bce29e 100644 --- a/test/signature/init.lua +++ b/test/signature/init.lua @@ -1,26 +1,25 @@ -local core = require 'core.signature' +local core = require 'core.signature' local files = require 'files' +local catch = require 'catch' rawset(_G, 'TEST', true) function TEST(script) return function (expect) - local pos = script:find('$', 1, true) - 1 - local new_script = script:gsub('%$', '') + local newScript, catched1 = catch(script, '?') + local newExpect, catched2 = catch(expect or '', '!') files.removeAll() - files.setText('', new_script) - local hovers = core('', pos) + files.setText('', newScript) + local hovers = core('', catched1['?'][1][1]) if hovers then assert(expect) local hover = hovers[#hovers] - local label = hover.label:gsub('^[\r\n]*(.-)[\r\n]*$', '%1'):gsub('\r\n', '\n') - expect.label = expect.label:gsub('^[\r\n]*(.-)[\r\n]*$', '%1'):gsub('\r\n', '\n') local arg = hover.params[hover.index].label - assert(expect.label == label) - assert(expect.arg[1] == arg[1]) - assert(expect.arg[2] == arg[2]) + assert(newExpect == hover.label) + assert(catched2['!'][1][1] == arg[1]) + assert(catched2['!'][1][2] == arg[2]) else assert(expect == nil) end @@ -31,93 +30,67 @@ TEST [[ local function x(a, b) end -x($ +x(<??> ]] -{ - label = "function x(a: any, b: any)", - arg = {12, 17}, -} +'function x(<!a: any!>, b: any)' TEST [[ local function x(a, b) end -x($) +x(<??>) ]] -{ - label = "function x(a: any, b: any)", - arg = {12, 17}, -} +'function x(<!a: any!>, b: any)' TEST [[ local function x(a, b) end -x(xxx$) +x(xxx<??>) ]] -{ - label = "function x(a: any, b: any)", - arg = {12, 17}, -} +'function x(<!a: any!>, b: any)' TEST [[ local function x(a, b) end -x(xxx, $) +x(xxx, <??>) ]] -{ - label = "function x(a: any, b: any)", - arg = {20, 25}, -} +'function x(a: any, <!b: any!>)' TEST [[ function mt:f(a) end -mt:f($ +mt:f(<??> ]] -{ - label = 'function mt:f(a: any)', - arg = {15, 20}, -} +'function mt:f(<!a: any!>)' TEST [[ local function x(a, b) return 1 end -x($ +x(<??> ]] -{ - label = "function x(a: any, b: any)", - arg = {12, 17}, -} +'function x(<!a: any!>, b: any)' TEST [[ local function x(a, ...) return 1 end -x(1, 2, 3, $ +x(1, 2, 3, <??> ]] -{ - label = "function x(a: any, ...)", - arg = {20, 22}, -} +'function x(a: any, <!...!>)' TEST [[ -(''):sub($ +(''):sub(<??> ]] -{ - label = [[ -function string:sub(i: integer, j?: integer) -]], - arg = {21, 30}, -} +'function string:sub(<!i: integer!>, j?: integer)' TEST [[ -(''):sub(1)$ +(''):sub(1)<??> ]] (nil) @@ -125,39 +98,29 @@ TEST [[ local function f(a, b, c) end -f(1, 'string$') +f(1, 'string<??>') ]] -{ - label = [[ -function f(a: any, b: any, c: any) -]], - arg = {20, 25}, -} +'function f(a: any, <!b: any!>, c: any)' TEST [[ -pcall(function () $ end) +pcall(function () <??> end) ]] (nil) TEST [[ -table.unpack {$} +table.unpack {<??>} ]] (nil) TEST [[ ---@type fun(x: number, y: number):boolean local zzzz -zzzz($) +zzzz(<??>) ]] -{ - label = [[ -function zzzz(x: number, y: number) -]], - arg = {15, 23}, -} +'function zzzz(<!x: number!>, y: number)' TEST [[ -('abc'):format(f($)) +('abc'):format(f(<??>)) ]] (nil) @@ -166,14 +129,9 @@ function Foo(param01, param02) end -Foo($) +Foo(<??>) ]] -{ - label = [[ -function Foo(param01: any, param02: any) -]], - arg = {14, 25}, -} +'function Foo(<!param01: any!>, param02: any)' TEST [[ function f1(a, b) @@ -182,51 +140,31 @@ end function f2(c, d) end -f2(f1(),$) +f2(f1(),<??>) ]] -{ - label = [[ -function f2(c: any, d: any) -]], - arg = {21, 26}, -} +'function f2(c: any, <!d: any!>)' TEST [[ local function f(a, b, c) end -f({},$) +f({},<??>) ]] -{ - label = [[ -function f(a: any, b: any, c: any) -]], - arg = {20, 25}, -} +'function f(a: any, <!b: any!>, c: any)' TEST [[ -for _ in pairs($) do +for _ in pairs(<??>) do end ]] -{ - label = [[ -function pairs(t: <T>) -]], - arg = {16, 21}, -} +'function pairs(<!t: <T>!>)' TEST [[ function m:f() end -m.f($) +m.f(<??>) ]] -{ - label = [[ -function m.f(self: table) -]], - arg = {14, 24}, -} +'function m.f(<!self: table!>)' TEST [[ ---@alias nnn table<number, string> @@ -234,59 +172,41 @@ TEST [[ ---@param x nnn local function f(x, y, z) end -f($) +f(<??>) ]] -{ - label = [[ -function f(x: table<number, string>, y: any, z: any) -]], - arg = {12, 35}, -} - +'function f(<!x: table<number, string>!>, y: any, z: any)' TEST [[ local function x(a, b) end -x( aaaa $, 2) +x( aaaa <??>, 2) ]] -{ - label = "function x(a: any, b: any)", - arg = {12, 17}, -} +"function x(<!a: any!>, b: any)" TEST [[ local function x(a, b) end -x($ aaaa , 2) +x(<??> aaaa , 2) ]] -{ - label = "function x(a: any, b: any)", - arg = {12, 17}, -} +'function x(<!a: any!>, b: any)' TEST [[ local function x(a, b) end -x(aaaa ,$ 2) +x(aaaa ,<??> 2) ]] -{ - label = "function x(a: any, b: any)", - arg = {20, 25}, -} +'function x(a: any, <!b: any!>)' TEST [[ local function x(a, b) end -x(aaaa , 2 $) +x(aaaa , 2 <??>) ]] -{ - label = "function x(a: any, b: any)", - arg = {20, 25}, -} +'function x(a: any, <!b: any!>)' TEST [[ local fooC @@ -298,9 +218,6 @@ function fooC(callback, par) end fooC(function (x, s) -end,$) +end,<??>) ]] -{ - label = 'function fooC(callback: fun(x: number, s: string):nil, par: number)', - arg = {56, 66}, -} +'function fooC(callback: fun(x: number, s: string):nil, <!par: number!>)' diff --git a/test/type_formatting/init.lua b/test/type_formatting/init.lua index 9178ea4f..46b8223d 100644 --- a/test/type_formatting/init.lua +++ b/test/type_formatting/init.lua @@ -1,16 +1,16 @@ local core = require 'core.type-formatting' local files = require 'files' local util = require 'utility' +local catch = require 'catch' rawset(_G, 'TEST', true) function TEST(script) return function (expect) - local pos = script:find('$', 1, true) - 1 - local new_script = script:gsub('%$', '') files.removeAll() - files.setText('', new_script) - local edits = core('', pos, expect.ch) + local newScript, catched = catch(script, '?') + files.setText('', newScript) + local edits = core('', catched['?'][1][1], expect.ch) if edits then assert(expect.edits) assert(util.equal(edits, expect.edits)) @@ -21,80 +21,104 @@ function TEST(script) end TEST [[ -if true then $ end +if true then <??> end ]] { ch = '\n', edits = { { - start = 13, + start = 12, finish = 13, text = '\n\t', }, { - start = 14, - finish = 17, + start = 13, + finish = 15, text = '', }, { - start = 18, - finish = 17, - text = '\nend', + start = 15, + finish = 15, + text = '\ne', }, } } TEST [[ -if true then $end +if true then <??>end ]] { ch = '\n', edits = { { - start = 13, + start = 12, finish = 13, text = '\n\t', }, { - start = 14, - finish = 16, + start = 13, + finish = 14, text = '', }, { - start = 17, - finish = 16, - text = '\nend', + start = 14, + finish = 14, + text = '\ne', }, } } TEST [[ -if true then$end +if true then<??>end ]] { ch = '\n', edits = { { - start = 13, + start = 12, finish = 12, text = '\n\t', }, { - start = 13, - finish = 15, + start = 12, + finish = 13, text = '', }, { + start = 13, + finish = 13, + text = '\ne', + }, + } +} + +TEST [[ + if true then<??>end +]] +{ + ch = '\n', + edits = { + { start = 16, - finish = 15, - text = '\nend', + finish = 16, + text = '\n \t', + }, + { + start = 16, + finish = 17, + text = '', + }, + { + start = 17, + finish = 17, + text = '\n e', }, } } TEST [[ if true then - $ + <??> end ]] { diff --git a/test/type_inference/init.lua b/test/type_inference/init.lua index a20a96e9..23ebf54e 100644 --- a/test/type_inference/init.lua +++ b/test/type_inference/init.lua @@ -1,8 +1,8 @@ local files = require 'files' -local vm = require 'vm' local guide = require 'parser.guide' local infer = require 'core.infer' local config = require 'config' +local catch = require 'catch' rawset(_G, 'TEST', true) @@ -24,12 +24,9 @@ end function TEST(wanted) return function (script) files.removeAll() - local start = script:find('<?', 1, true) - local finish = script:find('?>', 1, true) - local pos = (start + finish) // 2 + 1 - local newScript = script:gsub('<[!?]', ' '):gsub('[!?]>', ' ') + local newScript, catched = catch(script, '?') files.setText('', newScript) - local source = getSource(pos) + local source = getSource(catched['?'][1][1]) assert(source) local result = infer.searchAndViewInfers(source) if wanted ~= result then @@ -356,6 +353,13 @@ TEST 'table<string, number>' [[ local <?x?> ]] +TEST 'A<string, number>' [[ +---@class A + +---@type A<string, number> +local <?x?> +]] + TEST 'table' [[ self.<?t?>[#self.t+1] = {} ]] |