Do not show empty documents when restoring session

When a filename cannot be read when restoring a session do
not create a document. Previous behavior was to create an empty
"unsaved" document.
This commit is contained in:
Francesco Abbate 2021-06-04 14:05:54 +02:00
parent b046afccf9
commit 29e25a7605
1 changed files with 32 additions and 16 deletions

View File

@ -29,7 +29,7 @@ local function workspace_files_for(project_dir)
end end
local function load_workspace_file(project_dir) local function consume_workspace_file(project_dir)
for filename, id in workspace_files_for(project_dir) do for filename, id in workspace_files_for(project_dir) do
local load_f = loadfile(filename) local load_f = loadfile(filename)
local workspace = load_f and load_f() local workspace = load_f and load_f()
@ -99,16 +99,26 @@ end
local function load_view(t) local function load_view(t)
if t.type == "doc" then if t.type == "doc" then
local dv
if not t.filename then
-- document not associated to a file
dv = DocView(core.open_doc())
if t.text then dv.doc:insert(1, 1, t.text) end
else
-- we have a filename, try to read the file
local ok, doc = pcall(core.open_doc, t.filename) local ok, doc = pcall(core.open_doc, t.filename)
if not ok then if ok then
return DocView(core.open_doc()) dv = DocView(doc)
end end
local dv = DocView(doc) end
if t.text then doc:insert(1, 1, t.text) end -- doc view "dv" can be nil here if the filename associated to the document
doc:set_selection(table.unpack(t.selection)) -- cannot be read.
dv.last_line, dv.last_col = doc:get_selection() if dv and dv.doc then
dv.doc:set_selection(table.unpack(t.selection))
dv.last_line, dv.last_col = dv.doc:get_selection()
dv.scroll.x, dv.scroll.to.x = t.scroll.x, t.scroll.x 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 dv.scroll.y, dv.scroll.to.y = t.scroll.y, t.scroll.y
end
return dv return dv
end end
return require(t.module)() return require(t.module)()
@ -141,13 +151,19 @@ end
local function load_node(node, t) local function load_node(node, t)
if t.type == "leaf" then if t.type == "leaf" then
local res local res
for _, v in ipairs(t.views) do local active_view
for i, v in ipairs(t.views) do
local view = load_view(v) local view = load_view(v)
if view then
if v.active then res = view end if v.active then res = view end
node:add_view(view) node:add_view(view)
if t.active_view == i then
active_view = view
end end
if t.active_view then end
node:set_active_view(node.views[t.active_view]) end
if active_view then
node:set_active_view(active_view)
end end
return res return res
else else
@ -184,7 +200,7 @@ end
local function load_workspace() local function load_workspace()
local workspace = load_workspace_file(core.project_dir) local workspace = consume_workspace_file(core.project_dir)
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)