2022-05-31 22:34:14 +02:00
|
|
|
-- mod-version:3
|
2020-12-29 16:44:31 +01:00
|
|
|
local core = require "core"
|
2020-12-30 13:32:25 +01:00
|
|
|
local common = require "core.common"
|
2020-12-29 16:44:31 +01:00
|
|
|
local DocView = require "core.docview"
|
2021-06-04 14:42:08 +02:00
|
|
|
local LogView = require "core.logview"
|
2020-12-29 16:44:31 +01:00
|
|
|
|
2021-01-03 23:36:28 +01:00
|
|
|
|
|
|
|
local function workspace_files_for(project_dir)
|
2021-01-10 12:56:15 +01:00
|
|
|
local basename = common.basename(project_dir)
|
2024-02-17 18:47:15 +01:00
|
|
|
local workspace_dir = common.basepath(USERDIR) .. "ws"
|
2021-01-03 23:36:28 +01:00
|
|
|
local info_wsdir = system.get_file_info(workspace_dir)
|
|
|
|
if not info_wsdir then
|
|
|
|
local ok, err = system.mkdir(workspace_dir)
|
|
|
|
if not ok then
|
2021-06-08 12:06:54 +02:00
|
|
|
error("cannot create workspace directory: \"" .. err .. "\"")
|
2021-01-03 23:36:28 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
return coroutine.wrap(function()
|
|
|
|
local files = system.list_dir(workspace_dir) or {}
|
|
|
|
local n = #basename
|
|
|
|
for _, file in ipairs(files) do
|
|
|
|
if file:sub(1, n) == basename then
|
|
|
|
local id = tonumber(file:sub(n + 1):match("^-(%d+)$"))
|
|
|
|
if id then
|
2024-02-17 18:47:15 +01:00
|
|
|
coroutine.yield(common.basepath(workspace_dir) .. file, id)
|
2021-01-03 23:36:28 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-06-04 14:05:54 +02:00
|
|
|
local function consume_workspace_file(project_dir)
|
2021-01-03 23:36:28 +01:00
|
|
|
for filename, id in workspace_files_for(project_dir) do
|
|
|
|
local load_f = loadfile(filename)
|
|
|
|
local workspace = load_f and load_f()
|
|
|
|
if workspace and workspace.path == project_dir then
|
|
|
|
os.remove(filename)
|
|
|
|
return workspace
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
local function get_workspace_filename(project_dir)
|
|
|
|
local id_list = {}
|
|
|
|
for filename, id in workspace_files_for(project_dir) do
|
|
|
|
id_list[id] = true
|
|
|
|
end
|
|
|
|
local id = 1
|
|
|
|
while id_list[id] do
|
|
|
|
id = id + 1
|
|
|
|
end
|
2021-01-10 12:56:15 +01:00
|
|
|
local basename = common.basename(project_dir)
|
2024-02-17 18:47:15 +01:00
|
|
|
return common.basepath(USERDIR) .. "ws" .. PATHSEP .. basename .. "-" .. tostring(id)
|
2021-01-03 23:36:28 +01:00
|
|
|
end
|
2020-12-29 16:44:31 +01:00
|
|
|
|
|
|
|
|
|
|
|
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-10-18 06:46:58 +02:00
|
|
|
crlf = view.doc.crlf,
|
|
|
|
text = view.doc.new_file and view.doc:get_text(1, 1, math.huge, math.huge)
|
2020-12-29 16:44:31 +01:00
|
|
|
}
|
|
|
|
end
|
2021-06-04 14:42:08 +02:00
|
|
|
if mt == LogView then return end
|
2020-12-29 16:44:31 +01:00
|
|
|
for name, mod in pairs(package.loaded) do
|
|
|
|
if mod == mt then
|
|
|
|
return {
|
|
|
|
type = "view",
|
|
|
|
active = (core.active_view == view),
|
2022-10-10 04:55:07 +02:00
|
|
|
module = name,
|
|
|
|
scroll = { x = view.scroll.to.x, y = view.scroll.to.y, to = { x = view.scroll.to.x, y = view.scroll.to.y } },
|
2020-12-29 16:44:31 +01:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
local function load_view(t)
|
|
|
|
if t.type == "doc" then
|
2021-06-04 14:05:54 +02:00
|
|
|
local dv
|
|
|
|
if not t.filename then
|
|
|
|
-- document not associated to a file
|
|
|
|
dv = DocView(core.open_doc())
|
|
|
|
else
|
|
|
|
-- we have a filename, try to read the file
|
|
|
|
local ok, doc = pcall(core.open_doc, t.filename)
|
|
|
|
if ok then
|
|
|
|
dv = DocView(doc)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if dv and dv.doc then
|
2023-10-18 06:46:58 +02:00
|
|
|
if dv.doc.new_file and t.text then
|
|
|
|
dv.doc:insert(1, 1, t.text)
|
|
|
|
dv.doc.crlf = t.crlf
|
|
|
|
end
|
2021-06-04 14:05:54 +02:00
|
|
|
dv.doc:set_selection(table.unpack(t.selection))
|
2022-04-03 22:24:39 +02:00
|
|
|
dv.last_line1, dv.last_col1, dv.last_line2, dv.last_col2 = dv.doc:get_selection()
|
2021-06-04 14:05:54 +02:00
|
|
|
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
|
2020-12-29 16:44:31 +01:00
|
|
|
end
|
|
|
|
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
|
2021-06-04 14:05:54 +02:00
|
|
|
local active_view
|
|
|
|
for i, v in ipairs(t.views) do
|
2020-12-29 16:44:31 +01:00
|
|
|
local view = load_view(v)
|
2021-06-04 14:05:54 +02:00
|
|
|
if view then
|
|
|
|
if v.active then res = view end
|
|
|
|
node:add_view(view)
|
|
|
|
if t.active_view == i then
|
|
|
|
active_view = view
|
|
|
|
end
|
2022-10-10 04:55:07 +02:00
|
|
|
if not view:is(DocView) then
|
2024-02-17 18:47:15 +01:00
|
|
|
view.scroll = v.scroll
|
2022-10-10 04:55:07 +02:00
|
|
|
end
|
2021-06-04 14:05:54 +02:00
|
|
|
end
|
2020-12-29 16:44:31 +01:00
|
|
|
end
|
2021-06-04 14:05:54 +02:00
|
|
|
if active_view then
|
|
|
|
node:set_active_view(active_view)
|
2020-12-29 16:44:31 +01: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
|
|
|
|
|
|
|
|
|
2020-12-29 18:04:13 +01:00
|
|
|
local function save_directories()
|
|
|
|
local project_dir = core.project_dir
|
|
|
|
local dir_list = {}
|
2021-01-03 12:02:07 +01:00
|
|
|
for i = 2, #core.project_directories do
|
2021-03-06 23:36:05 +01:00
|
|
|
dir_list[#dir_list + 1] = common.relative_path(project_dir, core.project_directories[i].name)
|
2020-12-29 18:04:13 +01:00
|
|
|
end
|
|
|
|
return dir_list
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2020-12-29 16:44:31 +01:00
|
|
|
local function save_workspace()
|
|
|
|
local root = get_unlocked_root(core.root_view.root_node)
|
2021-01-03 23:36:28 +01:00
|
|
|
local workspace_filename = get_workspace_filename(core.project_dir)
|
2020-12-29 16:44:31 +01:00
|
|
|
local fp = io.open(workspace_filename, "w")
|
|
|
|
if fp then
|
2020-12-30 13:32:25 +01:00
|
|
|
local node_text = common.serialize(save_node(root))
|
|
|
|
local dir_text = common.serialize(save_directories())
|
2021-01-03 23:36:28 +01:00
|
|
|
fp:write(string.format("return { path = %q, documents = %s, directories = %s }\n", core.project_dir, node_text, dir_text))
|
2020-12-29 16:44:31 +01:00
|
|
|
fp:close()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
local function load_workspace()
|
2021-06-04 14:05:54 +02:00
|
|
|
local workspace = consume_workspace_file(core.project_dir)
|
2021-01-03 23:36:28 +01:00
|
|
|
if workspace then
|
2020-12-29 16:44:31 +01:00
|
|
|
local root = get_unlocked_root(core.root_view.root_node)
|
2021-01-03 23:36:28 +01:00
|
|
|
local active_view = load_node(root, workspace.documents)
|
2020-12-29 16:44:31 +01:00
|
|
|
if active_view then
|
|
|
|
core.set_active_view(active_view)
|
|
|
|
end
|
2021-01-03 23:36:28 +01:00
|
|
|
for i, dir_name in ipairs(workspace.directories) do
|
2020-12-30 14:53:50 +01:00
|
|
|
core.add_project_directory(system.absolute_path(dir_name))
|
2020-12-29 18:04:13 +01:00
|
|
|
end
|
2020-12-29 16:44:31 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
local run = core.run
|
|
|
|
|
|
|
|
function core.run(...)
|
|
|
|
if #core.docs == 0 then
|
|
|
|
core.try(load_workspace)
|
|
|
|
|
2020-12-30 14:31:36 +01:00
|
|
|
local on_quit_project = core.on_quit_project
|
|
|
|
function core.on_quit_project()
|
2021-01-03 12:02:07 +01:00
|
|
|
core.try(save_workspace)
|
2020-12-30 14:31:36 +01:00
|
|
|
on_quit_project()
|
|
|
|
end
|
|
|
|
|
|
|
|
local on_enter_project = core.on_enter_project
|
|
|
|
function core.on_enter_project(new_dir)
|
|
|
|
on_enter_project(new_dir)
|
|
|
|
core.try(load_workspace)
|
2020-12-29 16:44:31 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
core.run = run
|
|
|
|
return core.run(...)
|
|
|
|
end
|