summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--script/core/guide.lua5
-rw-r--r--script/core/linker.lua31
-rw-r--r--test/basic/init.lua221
-rw-r--r--test/basic/linker.lua40
-rw-r--r--test/basic/textmerger.lua219
5 files changed, 297 insertions, 219 deletions
diff --git a/script/core/guide.lua b/script/core/guide.lua
index b80a3628..64192297 100644
--- a/script/core/guide.lua
+++ b/script/core/guide.lua
@@ -290,8 +290,13 @@ end
---@param obj parser.guide.object
---@return parser.guide.object
function m.getRoot(obj)
+ local source = obj
+ if source._root then
+ return source._root
+ end
for _ = 1, 1000 do
if obj.type == 'main' then
+ source._root = obj
return obj
end
local parent = obj.parent
diff --git a/script/core/linker.lua b/script/core/linker.lua
new file mode 100644
index 00000000..6c475e70
--- /dev/null
+++ b/script/core/linker.lua
@@ -0,0 +1,31 @@
+local guide = require 'parser.guide'
+
+local function getKey(source)
+ if source.type == 'local' then
+ return ('l%d%s'):format(source.start, source[1])
+ end
+end
+
+---创建source的链接信息
+local function createLink(source)
+ local idList = {}
+ if getKey then
+ idList[#idList+1] = getKey(source)
+ end
+ local id = table.concat(idList, '|')
+ return {
+ id = id,
+ }
+end
+
+local m = {}
+
+---获取source的链接信息
+function m.getLink(source)
+ if not source._link then
+ source._link = createLink(source)
+ end
+ return source._link
+end
+
+return m
diff --git a/test/basic/init.lua b/test/basic/init.lua
index a3a11f62..d3a84152 100644
--- a/test/basic/init.lua
+++ b/test/basic/init.lua
@@ -1,219 +1,2 @@
-local files = require 'files'
-local tm = require 'text-merger'
-
-local function TEST(source)
- return function (expect)
- return function (changes)
- files.removeAll()
- files.setText('', source)
- local text = tm('', changes)
- assert(text == expect)
- end
- end
-end
-
-TEST [[
-
-
-function Test(self)
-
-end
-]][[
-
-
-function Test(self)
-
-end
-
-asser]]{
- [1] = {
- range = {
- ["end"] = {
- character = 0,
- line = 5,
- },
- start = {
- character = 0,
- line = 5,
- },
- },
- rangeLength = 0,
- text = "\
-",
- },
- [2] = {
- range = {
- ["end"] = {
- character = 0,
- line = 6,
- },
- start = {
- character = 0,
- line = 6,
- },
- },
- rangeLength = 0,
- text = "a",
- },
- [3] = {
- range = {
- ["end"] = {
- character = 1,
- line = 6,
- },
- start = {
- character = 1,
- line = 6,
- },
- },
- rangeLength = 0,
- text = "s",
- },
- [4] = {
- range = {
- ["end"] = {
- character = 2,
- line = 6,
- },
- start = {
- character = 2,
- line = 6,
- },
- },
- rangeLength = 0,
- text = "s",
- },
- [5] = {
- range = {
- ["end"] = {
- character = 3,
- line = 6,
- },
- start = {
- character = 3,
- line = 6,
- },
- },
- rangeLength = 0,
- text = "e",
- },
- [6] = {
- range = {
- ["end"] = {
- character = 4,
- line = 6,
- },
- start = {
- character = 4,
- line = 6,
- },
- },
- rangeLength = 0,
- text = "r",
- },
-}
-
-TEST [[
-local mt = {}
-
-function mt['xxx']()
-
-
-
-end
-]] [[
-local mt = {}
-
-function mt['xxx']()
-
-end
-]] {
- [1] = {
- range = {
- ["end"] = {
- character = 4,
- line = 5,
- },
- start = {
- character = 4,
- line = 3,
- },
- },
- rangeLength = 8,
- text = "",
- },
-}
-
-TEST [[
-local mt = {}
-
-function mt['xxx']()
-
-end
-]] [[
-local mt = {}
-
-function mt['xxx']()
- p
-end
-]] {
- [1] = {
- range = {
- ["end"] = {
- character = 4,
- line = 3,
- },
- start = {
- character = 4,
- line = 3,
- },
- },
- rangeLength = 0,
- text = "p",
- },
-}
-
-TEST [[
-print(12345)
-]] [[
-print(123
-45)
-]] {
- [1] = {
- range = {
- ["end"] = {
- character = 9,
- line = 0,
- },
- start = {
- character = 9,
- line = 0,
- },
- },
- rangeLength = 0,
- text = "\
-",
- },
-}
-
-TEST [[
-print(123
-45)
-]] [[
-print(12345)
-]] {
- [1] = {
- range = {
- ["end"] = {
- character = 0,
- line = 1,
- },
- start = {
- character = 9,
- line = 0,
- },
- },
- rangeLength = 2,
- text = "",
- },
-}
+require 'basic.textmerger'
+require 'basic.linker'
diff --git a/test/basic/linker.lua b/test/basic/linker.lua
new file mode 100644
index 00000000..e663579e
--- /dev/null
+++ b/test/basic/linker.lua
@@ -0,0 +1,40 @@
+local linker = require 'core.linker'
+local files = require 'files'
+local util = require 'utility'
+local guide = require 'core.guide'
+
+local function getSource(pos)
+ local ast = files.getAst('')
+ return guide.eachSourceContain(ast.ast, pos, function (source)
+ if source.type == 'local'
+ or source.type == 'getlocal'
+ or source.type == 'setlocal'
+ or source.type == 'setglobal'
+ or source.type == 'getglobal'
+ or source.type == 'field'
+ or source.type == 'method' then
+ return source
+ end
+ end)
+end
+
+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('[!?]>', ' ')
+ files.setText('', newScript)
+ local source = getSource(pos)
+ assert(source)
+ local result = linker.getLink(source)
+ assert(util.equal(result, expect))
+ end
+end
+
+TEST [[
+local <?x?>
+]] {
+ id = 'l9x',
+}
diff --git a/test/basic/textmerger.lua b/test/basic/textmerger.lua
new file mode 100644
index 00000000..a3a11f62
--- /dev/null
+++ b/test/basic/textmerger.lua
@@ -0,0 +1,219 @@
+local files = require 'files'
+local tm = require 'text-merger'
+
+local function TEST(source)
+ return function (expect)
+ return function (changes)
+ files.removeAll()
+ files.setText('', source)
+ local text = tm('', changes)
+ assert(text == expect)
+ end
+ end
+end
+
+TEST [[
+
+
+function Test(self)
+
+end
+]][[
+
+
+function Test(self)
+
+end
+
+asser]]{
+ [1] = {
+ range = {
+ ["end"] = {
+ character = 0,
+ line = 5,
+ },
+ start = {
+ character = 0,
+ line = 5,
+ },
+ },
+ rangeLength = 0,
+ text = "\
+",
+ },
+ [2] = {
+ range = {
+ ["end"] = {
+ character = 0,
+ line = 6,
+ },
+ start = {
+ character = 0,
+ line = 6,
+ },
+ },
+ rangeLength = 0,
+ text = "a",
+ },
+ [3] = {
+ range = {
+ ["end"] = {
+ character = 1,
+ line = 6,
+ },
+ start = {
+ character = 1,
+ line = 6,
+ },
+ },
+ rangeLength = 0,
+ text = "s",
+ },
+ [4] = {
+ range = {
+ ["end"] = {
+ character = 2,
+ line = 6,
+ },
+ start = {
+ character = 2,
+ line = 6,
+ },
+ },
+ rangeLength = 0,
+ text = "s",
+ },
+ [5] = {
+ range = {
+ ["end"] = {
+ character = 3,
+ line = 6,
+ },
+ start = {
+ character = 3,
+ line = 6,
+ },
+ },
+ rangeLength = 0,
+ text = "e",
+ },
+ [6] = {
+ range = {
+ ["end"] = {
+ character = 4,
+ line = 6,
+ },
+ start = {
+ character = 4,
+ line = 6,
+ },
+ },
+ rangeLength = 0,
+ text = "r",
+ },
+}
+
+TEST [[
+local mt = {}
+
+function mt['xxx']()
+
+
+
+end
+]] [[
+local mt = {}
+
+function mt['xxx']()
+
+end
+]] {
+ [1] = {
+ range = {
+ ["end"] = {
+ character = 4,
+ line = 5,
+ },
+ start = {
+ character = 4,
+ line = 3,
+ },
+ },
+ rangeLength = 8,
+ text = "",
+ },
+}
+
+TEST [[
+local mt = {}
+
+function mt['xxx']()
+
+end
+]] [[
+local mt = {}
+
+function mt['xxx']()
+ p
+end
+]] {
+ [1] = {
+ range = {
+ ["end"] = {
+ character = 4,
+ line = 3,
+ },
+ start = {
+ character = 4,
+ line = 3,
+ },
+ },
+ rangeLength = 0,
+ text = "p",
+ },
+}
+
+TEST [[
+print(12345)
+]] [[
+print(123
+45)
+]] {
+ [1] = {
+ range = {
+ ["end"] = {
+ character = 9,
+ line = 0,
+ },
+ start = {
+ character = 9,
+ line = 0,
+ },
+ },
+ rangeLength = 0,
+ text = "\
+",
+ },
+}
+
+TEST [[
+print(123
+45)
+]] [[
+print(12345)
+]] {
+ [1] = {
+ range = {
+ ["end"] = {
+ character = 0,
+ line = 1,
+ },
+ start = {
+ character = 9,
+ line = 0,
+ },
+ },
+ rangeLength = 2,
+ text = "",
+ },
+}