WIP: try to introduce project workspace save

This commit is contained in:
Francesco Abbate 2021-06-25 15:37:04 +02:00
parent 27c9a3181f
commit 7d0de3a4b9
2 changed files with 62 additions and 6 deletions

View File

@ -82,6 +82,13 @@ function core.new_project_from_directory(dir_path_abs)
end end
function core.project_workspace_name()
for _, dir in ipairs(core.project_entries) do
return dir.item.filename
end
end
local function strip_leading_path(filename) local function strip_leading_path(filename)
return filename:sub(2) return filename:sub(2)
end end
@ -304,7 +311,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', 'projects', 'colors', 'fonts'} do for _, modname in ipairs {'plugins', 'projects', 'ws', '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 .. "\"")
@ -480,7 +487,7 @@ function core.init()
core.log_items = {} core.log_items = {}
core.docs = {} core.docs = {}
core.project_entries = {} core.project_entries = {}
core.project_name = "" -- core.project_name = ""
local init_files = {} local init_files = {}
local delayed_errors = {} local delayed_errors = {}
@ -642,8 +649,13 @@ end
function core.on_quit_project() function core.on_quit_project()
local filename = USERDIR .. PATHSEP .. "workspace.lua" -- local filename = USERDIR .. PATHSEP .. "workspace.lua"
core.try(project.save_workspace, filename) -- core.try(project.save_workspace, filename)
if core.project_name then
core.try(project.save, filename)
else
core.try(project.save_unnamed)
end
end end

View File

@ -123,8 +123,8 @@ function project.save_workspace(filename)
end end
local project_entries_text = common.serialize(topdir_entries) local project_entries_text = common.serialize(topdir_entries)
fp:write(string.format( fp:write(string.format(
"return { project_name = %q, working_dir = %q, documents = %s, project_entries = %s }\n", "return { project_name = %s, working_dir = %q, documents = %s, project_entries = %s }\n",
core.project_name, core.working_dir, node_text, project_entries_text)) core.project_name and string.format("%q", core.project_name) or "nil", core.working_dir, node_text, project_entries_text))
fp:close() fp:close()
end end
end end
@ -146,6 +146,50 @@ function project.save(name)
core.log("Saved project %s.", core.project_name) core.log("Saved project %s.", core.project_name)
end end
local function workspace_files_for(basename)
local workspace_dir = USERDIR .. PATHSEP .. "ws"
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
error("cannot create workspace directory: \"" .. err .. "\"")
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
coroutine.yield(workspace_dir .. PATHSEP .. file, id)
end
end
end
end)
end
local function get_workspace_filename(basename)
local id_list = {}
for filename, id in workspace_files_for(basename) do
id_list[id] = true
end
local id = 1
while id_list[id] do
id = id + 1
end
return common.path_join(USERDIR, "ws", basename .. "-" .. tostring(id))
end
function project.save_unnamed()
local name = core.project_workspace_name()
-- empty projects shoud return nil and we don't wave them
if name then
local filename = get_workspace_filename(name)
save_workspace(filename)
end
end
function project.load_workspace(filename) function project.load_workspace(filename)
local load = loadfile(filename) local load = loadfile(filename)