v4k-git-backup/engine/art/editor/lite/data/plugins/workspace.lua

180 lines
4.0 KiB
Lua
Raw Normal View History

2023-11-05 15:30:11 +00:00
local core = require "core"
local DocView = require "core.docview"
2023-11-06 08:55:10 +00:00
local workspace_filename = ".lite_workspace.lua"
2023-11-05 15:30:11 +00:00
2023-11-06 08:55:10 +00:00
local function serialize(val)
if type(val) == "string" then
return string.format("%q", val)
elseif type(val) == "table" then
local t = {}
for k, v in pairs(val) do
table.insert(t, "[" .. serialize(k) .. "]=" .. serialize(v))
2023-11-05 15:30:11 +00:00
end
2023-11-06 08:55:10 +00:00
return "{" .. table.concat(t, ",") .. "}"
2023-11-05 15:30:11 +00:00
end
2023-11-06 08:55:10 +00:00
return tostring(val)
2023-11-05 15:30:11 +00:00
end
local function has_no_locked_children(node)
if node.locked then return false end
if node.type == "leaf" then return true end
return has_no_locked_children(node.a) and has_no_locked_children(node.b)
end
local function get_unlocked_root(node)
if node.type == "leaf" then
return not node.locked and node
end
if has_no_locked_children(node) then
return node
end
return get_unlocked_root(node.a) or get_unlocked_root(node.b)
end
local function save_view(view)
local mt = getmetatable(view)
if mt == DocView then
return {
type = "doc",
active = (core.active_view == view),
filename = view.doc.filename,
selection = { view.doc:get_selection() },
scroll = { x = view.scroll.to.x, y = view.scroll.to.y },
2023-11-06 08:55:10 +00:00
text = not view.doc.filename and view.doc:get_text(1, 1, math.huge, math.huge)
2023-11-05 15:30:11 +00:00
}
end
for name, mod in pairs(package.loaded) do
if mod == mt then
return {
type = "view",
active = (core.active_view == view),
2023-11-06 08:55:10 +00:00
module = name
2023-11-05 15:30:11 +00:00
}
end
end
end
local function load_view(t)
if t.type == "doc" then
2023-11-06 08:55:10 +00:00
local ok, doc = pcall(core.open_doc, t.filename)
if not ok then
return DocView(core.open_doc())
2023-11-05 15:30:11 +00:00
end
2023-11-06 08:55:10 +00:00
local dv = DocView(doc)
if t.text then doc:insert(1, 1, t.text) end
doc:set_selection(table.unpack(t.selection))
dv.last_line, dv.last_col = doc:get_selection()
dv.scroll.x, dv.scroll.to.x = t.scroll.x, t.scroll.x
dv.scroll.y, dv.scroll.to.y = t.scroll.y, t.scroll.y
2023-11-05 15:30:11 +00:00
return dv
end
return require(t.module)()
end
local function save_node(node)
local res = {}
res.type = node.type
if node.type == "leaf" then
res.views = {}
for _, view in ipairs(node.views) do
local t = save_view(view)
if t then
table.insert(res.views, t)
if node.active_view == view then
res.active_view = #res.views
end
end
end
else
res.divider = node.divider
res.a = save_node(node.a)
res.b = save_node(node.b)
end
return res
end
local function load_node(node, t)
if t.type == "leaf" then
local res
2023-11-06 08:55:10 +00:00
for _, v in ipairs(t.views) do
2023-11-05 15:30:11 +00:00
local view = load_view(v)
2023-11-06 08:55:10 +00:00
if v.active then res = view end
node:add_view(view)
2023-11-05 15:30:11 +00:00
end
2023-11-06 08:55:10 +00:00
if t.active_view then
node:set_active_view(node.views[t.active_view])
2023-11-05 15:30:11 +00:00
end
return res
else
node:split(t.type == "hsplit" and "right" or "down")
node.divider = t.divider
local res1 = load_node(node.a, t.a)
local res2 = load_node(node.b, t.b)
return res1 or res2
end
end
local function save_workspace()
local root = get_unlocked_root(core.root_view.root_node)
local fp = io.open(workspace_filename, "w")
if fp then
2023-11-06 08:55:10 +00:00
fp:write("return ", serialize(save_node(root)), "\n")
2023-11-05 15:30:11 +00:00
fp:close()
end
end
local function load_workspace()
2023-11-06 08:55:10 +00:00
local ok, t = pcall(dofile, workspace_filename)
os.remove(workspace_filename)
if ok then
2023-11-05 15:30:11 +00:00
local root = get_unlocked_root(core.root_view.root_node)
2023-11-06 08:55:10 +00:00
local active_view = load_node(root, t)
2023-11-05 15:30:11 +00:00
if active_view then
core.set_active_view(active_view)
end
end
end
local run = core.run
function core.run(...)
if #core.docs == 0 then
core.try(load_workspace)
2023-11-06 08:55:10 +00:00
local exit = os.exit
function os.exit(...)
save_workspace()
exit(...)
2023-11-05 15:30:11 +00:00
end
end
core.run = run
return core.run(...)
end
2023-11-06 08:55:10 +00:00
local run1 = core.run1
function core.run1(...)
if #core.docs == 0 then
core.try(load_workspace)
local exit = os.exit
function os.exit(...)
save_workspace()
exit(...)
end
end
core.run1 = run1
return core.run1(...)
end