Add first commands to load/save projects

This commit is contained in:
Francesco Abbate 2021-05-18 12:58:54 +02:00
parent dd802f9e17
commit 9e29b90318
3 changed files with 54 additions and 17 deletions

View File

@ -193,6 +193,16 @@ function common.serialize(val)
end end
function common.path_join(...)
local n = select('#', ...)
local accu = select(1, ...)
for i = 2, n do
accu = accu .. PATHSEP .. select(i, ...)
end
return accu
end
function common.basename(path) function common.basename(path)
-- a path should never end by / or \ except if it is '/' (unix root) or -- a path should never end by / or \ except if it is '/' (unix root) or
-- 'X:\' (windows drive) -- 'X:\' (windows drive)

View File

@ -304,7 +304,7 @@ local function create_user_directory()
error("cannot create directory: \"" .. dirname_create .. "\"") error("cannot create directory: \"" .. dirname_create .. "\"")
end end
end end
for _, modname in ipairs {'plugins', 'colors', 'fonts'} do for _, modname in ipairs {'plugins', 'projects', 'colors', 'fonts'} do
local subdirname = dirname_create .. '/' .. modname local subdirname = dirname_create .. '/' .. modname
if not system.mkdir(subdirname) then if not system.mkdir(subdirname) then
error("cannot create directory: \"" .. subdirname .. "\"") error("cannot create directory: \"" .. subdirname .. "\"")

View File

@ -1,15 +1,10 @@
-- mod-version:1 -- lite-xl 1.16 -- mod-version:1 -- lite-xl 1.16
local core = require "core" local core = require "core"
local command = require "core.command"
local common = require "core.common" local common = require "core.common"
local DocView = require "core.docview" local DocView = require "core.docview"
local function load_workspace_file()
local load_f = loadfile(USERDIR .. PATHSEP .. "workspace.lua")
return load_f and load_f()
end
local function has_no_locked_children(node) local function has_no_locked_children(node)
if node.locked then return false end if node.locked then return false end
if node.type == "leaf" then return true end if node.type == "leaf" then return true end
@ -115,15 +110,13 @@ local function load_node(node, t)
end end
local function save_workspace() local function save_workspace(filename)
local root = get_unlocked_root(core.root_view.root_node) local root = get_unlocked_root(core.root_view.root_node)
local workspace_filename = USERDIR .. PATHSEP .. "workspace.lua" local fp = io.open(filename, "w")
local fp = io.open(workspace_filename, "w")
if fp then if fp then
local node_text = common.serialize(save_node(root)) local node_text = common.serialize(save_node(root))
local topdir_entries = {} local topdir_entries = {}
for i = 1, #core.project_entries do for _, entry in ipairs(core.project_entries) do
local entry = core.project_entries[i]
if entry.item.topdir then if entry.item.topdir then
table.insert(topdir_entries, {path = entry.name, type = entry.item.type}) table.insert(topdir_entries, {path = entry.name, type = entry.item.type})
end end
@ -135,8 +128,9 @@ local function save_workspace()
end end
local function load_workspace() local function load_workspace(filename)
local workspace = load_workspace_file() local load = loadfile(filename)
local workspace = load and load()
if workspace then if workspace then
local root = get_unlocked_root(core.root_view.root_node) local root = get_unlocked_root(core.root_view.root_node)
local active_view = load_node(root, workspace.documents) local active_view = load_node(root, workspace.documents)
@ -160,21 +154,54 @@ local run = core.run
function core.run(...) function core.run(...)
if #core.docs == 0 then if #core.docs == 0 then
core.try(load_workspace) core.try(load_workspace, USERDIR .. PATHSEP .. "workspace.lua")
local on_quit_project = core.on_quit_project local on_quit_project = core.on_quit_project
function core.on_quit_project() function core.on_quit_project()
core.try(save_workspace) local filename = USERDIR .. PATHSEP .. "workspace.lua"
core.try(save_workspace, filename)
on_quit_project() on_quit_project()
end end
local on_enter_project = core.on_enter_project local on_enter_project = core.on_enter_project
function core.on_enter_project(new_dir) function core.on_enter_project(new_dir)
on_enter_project(new_dir) on_enter_project(new_dir)
core.try(load_workspace) core.try(load_workspace, USERDIR .. PATHSEP .. "workspace.lua")
end end
end end
core.run = run core.run = run
return core.run(...) return core.run(...)
end end
command.add(nil, {
["project:save-as"] = function()
local entry = core.project_entries[1]
if entry then
core.command_view:set_text(entry.item.filename)
end
core.command_view:enter("Save Project As", function(text)
-- FIXME: add sanity check of project name.
core.project_name = text
local filename = common.path_join(USERDIR, "projects", text .. ".lua")
save_workspace(filename)
core.log("Saved project %s.", core.project_name)
end)
end,
["project:save"] = function()
local filename = common.path_join(USERDIR, "projects", core.project_name .. ".lua")
save_workspace(filename)
core.log("Saved project %s.", core.project_name)
end,
["project:load"] = function()
core.command_view:enter("Load Project", function(text)
-- FIXME: add sanity check of project name.
core.project_name = text
local filename = common.path_join(USERDIR, "projects", text .. ".lua")
load_workspace(filename)
core.log("Loaded project %s.", core.project_name)
core.reschedule_project_scan()
end)
end,
})