From b91a84eb5677522669ee20c46b5bb644690b4e92 Mon Sep 17 00:00:00 2001 From: Francesco Abbate Date: Wed, 30 Dec 2020 14:31:36 +0100 Subject: [PATCH] Fix project change and quit versus workspace plugin --- data/core/commands/core.lua | 2 +- data/core/init.lua | 64 ++++++++++++++++++++++--------------- data/plugins/treeview.lua | 2 +- data/plugins/workspace.lua | 12 +++++-- 4 files changed, 50 insertions(+), 30 deletions(-) diff --git a/data/core/commands/core.lua b/data/core/commands/core.lua index 43a04ba4..78e34798 100644 --- a/data/core/commands/core.lua +++ b/data/core/commands/core.lua @@ -178,7 +178,7 @@ command.add(nil, { end core.add_project_directory(system.absolute_path(text)) -- TODO: add the name of directory to prioritize - core.request_project_scan() + core.reschedule_project_scan() end, suggest_directory) end, diff --git a/data/core/init.lua b/data/core/init.lua index e2d92f1d..73300454 100644 --- a/data/core/init.lua +++ b/data/core/init.lua @@ -39,17 +39,37 @@ local function save_projects() end -function core.request_project_scan() - core.threads[core.project_scan_thread_id].wake = 0 +function core.reschedule_project_scan() + if core.project_scan_thread_id then + core.threads[core.project_scan_thread_id].wake = 0 + end +end + + +core.project_files_empty = {} + + +function core.set_project_dir(new_dir) + core.project_dir = new_dir + system.chdir(new_dir) + core.project_directories = {} + core.add_project_directory(new_dir) + -- core.project_files will be set during the project files scan + -- to point to the files of the project directory. + -- core.project_files will therefore not include any of the added + -- directories. + core.project_files = core.project_files_empty + core.reschedule_project_scan() end function core.open_folder_project(dirname) + core.on_quit_project() core.root_view:close_all_docviews() add_project_to_recents(dirname) save_projects() - core.switch_project = dirname - core.request_project_scan() + core.set_project_dir(dirname) + core.on_enter_project(dirname) end @@ -123,6 +143,7 @@ local function project_scan_thread() while true do -- get project files and replace previous table if the new table is -- different + local include_project_dir = false for i = 1, #core.project_directories do local dir = core.project_directories[i] local t, entries_count = get_files(dir.name, "") @@ -133,20 +154,19 @@ local function project_scan_thread() config.max_project_files.." files according to config.max_project_files.") end dir.files = t - if i == 1 then + if dir.name == core.project_dir then + include_project_dir = true core.project_files = t end core.redraw = true end end + if not include_project_dir then + core.project_files = core.project_files_empty + end -- wait for next scan - if core.switch_project then - system.chdir(core.switch_project) - core.switch_project = nil - else - coroutine.yield(config.project_scan_rate) - end + coroutine.yield(config.project_scan_rate) end end @@ -312,23 +332,13 @@ function core.init() end end - system.chdir(project_dir) - core.frame_start = 0 core.clip_rect_stack = {{ 0,0,0,0 }} core.log_items = {} core.docs = {} core.threads = setmetatable({}, { __mode = "k" }) - -- core.project_files will always point to the files of - -- core.project_directories[1]. We assume the first entry of - -- project_directories will always be the project's directory. - -- core.project_files will therefore not include any of the added - -- directories. - core.project_dir = system.absolute_path(".") - core.project_directories = {} - core.add_project_directory(core.project_dir) - core.project_files = core.project_directories[1].files + core.set_project_dir(system.absolute_path(project_dir)) core.redraw = true core.visited_files = {} @@ -403,14 +413,18 @@ function core.temp_filename(ext) .. string.format("%06x", temp_file_counter) .. (ext or "") end --- override to perform an operation before quit -function core.on_quit() +-- override to perform an operation before quitting or entering the +-- current project +do + local do_nothing = function() end + core.on_quit_project = do_nothing + core.on_enter_project = do_nothing end local function quit_with_function(quit_fn, force) if force then delete_temp_files() - core.on_quit() + core.on_quit_project() quit_fn() else if core.confirm_close_all() then diff --git a/data/plugins/treeview.lua b/data/plugins/treeview.lua index a43aa63f..e4bd5f2a 100644 --- a/data/plugins/treeview.lua +++ b/data/plugins/treeview.lua @@ -147,7 +147,7 @@ local function create_directory_in(item) core.error("cannot create directory %q: %s", dirname, err) end item.expanded = true - core.request_project_scan() + core.reschedule_project_scan() end) end diff --git a/data/plugins/workspace.lua b/data/plugins/workspace.lua index 7a05ae05..b51562df 100644 --- a/data/plugins/workspace.lua +++ b/data/plugins/workspace.lua @@ -183,10 +183,16 @@ function core.run(...) if #core.docs == 0 then core.try(load_workspace) - local original_on_quit = core.on_quit - function core.on_quit() + local on_quit_project = core.on_quit_project + function core.on_quit_project() save_workspace() - original_on_quit() + 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) end end