summaryrefslogtreecommitdiff
path: root/server-beta/src/utility.lua
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2019-10-24 15:12:38 +0800
committer最萌小汐 <sumneko@hotmail.com>2019-10-24 15:12:38 +0800
commit506ab477dca97419650106a8ce1271c340c1cf92 (patch)
tree11ad9684aad91929ef7336ec50adc94039e3c9d6 /server-beta/src/utility.lua
parentb9ccbea5dfbfcffedb2cbcd983fa12580f63d542 (diff)
downloadlua-language-server-506ab477dca97419650106a8ce1271c340c1cf92.zip
更新utility
Diffstat (limited to 'server-beta/src/utility.lua')
-rw-r--r--server-beta/src/utility.lua73
1 files changed, 65 insertions, 8 deletions
diff --git a/server-beta/src/utility.lua b/server-beta/src/utility.lua
index 85cb4105..d624bcd2 100644
--- a/server-beta/src/utility.lua
+++ b/server-beta/src/utility.lua
@@ -78,10 +78,6 @@ function m.dump(tbl, option)
local mark = {}
lines[#lines+1] = '{'
local function unpack(tbl, tab)
- if mark[tbl] and mark[tbl] > 0 then
- lines[#lines+1] = TAB[tab+1] .. '"<Loop>"'
- return
- end
mark[tbl] = (mark[tbl] or 0) + 1
local keys = {}
local keymap = {}
@@ -140,13 +136,17 @@ function m.dump(tbl, option)
local value = tbl[key]
local tp = type(value)
if tp == 'table' then
- lines[#lines+1] = ('%s%s{'):format(TAB[tab+1], keyWord)
- unpack(value, tab+1)
- lines[#lines+1] = ('%s},'):format(TAB[tab+1])
+ if mark[value] and mark[value] > 0 then
+ lines[#lines+1] = ('%s%s%s,'):format(TAB[tab+1], keyWord, option.loop or '"<Loop>"')
+ else
+ lines[#lines+1] = ('%s%s{'):format(TAB[tab+1], keyWord)
+ unpack(value, tab+1)
+ lines[#lines+1] = ('%s},'):format(TAB[tab+1])
+ end
elseif tp == 'string' then
lines[#lines+1] = ('%s%s%q,'):format(TAB[tab+1], keyWord, value)
elseif tp == 'number' then
- lines[#lines+1] = ('%s%s%s,'):format(TAB[tab+1], keyWord, formatNumber(value))
+ lines[#lines+1] = ('%s%s%s,'):format(TAB[tab+1], keyWord, (option.number or formatNumber)(value))
elseif tp == 'nil' then
else
lines[#lines+1] = ('%s%s%s,'):format(TAB[tab+1], keyWord, tostring(value))
@@ -333,4 +333,61 @@ function m.deepCopy(source, target)
return copy(source, target)
end
+--- 序列化
+function m.unpack(t)
+ local result = {}
+ local tid = 0
+ local cache = {}
+ local function unpack(o)
+ local id = cache[o]
+ if not id then
+ tid = tid + 1
+ id = tid
+ cache[o] = tid
+ if type(o) == 'table' then
+ local new = {}
+ result[tid] = new
+ for k, v in next, o do
+ new[unpack(k)] = unpack(v)
+ end
+ else
+ result[id] = o
+ end
+ end
+ return id
+ end
+ unpack(t)
+ return result
+end
+
+--- 反序列化
+function m.pack(t)
+ local cache = {}
+ local function pack(id)
+ local o = cache[id]
+ if o then
+ return o
+ end
+ o = t[id]
+ if type(o) == 'table' then
+ local new = {}
+ cache[id] = new
+ for k, v in next, o do
+ new[pack(k)] = pack(v)
+ end
+ return new
+ else
+ cache[id] = o
+ return o
+ end
+ end
+ return pack(1)
+end
+
+--- defer
+local deferMT = { __close = function (self) self[1]() end }
+function m.defer(callback)
+ return setmetatable({ callback }, deferMT)
+end
+
return m