Fix project's module loading when changing project

Fix a bag of subtle problem about when loading the
project module.

We need to load the project's module before to scan
the project directory.
This commit is contained in:
Francesco Abbate 2021-12-30 23:57:23 +01:00
parent 68aea88510
commit 85d26adb62
1 changed files with 22 additions and 28 deletions

View File

@ -52,18 +52,14 @@ local function update_recents_project(action, dir_path_abs)
end end
function core.set_project_dir(new_dir, change_project_fn, defer_add) function core.set_project_dir(new_dir, change_project_fn)
local chdir_ok = pcall(system.chdir, new_dir) local chdir_ok = pcall(system.chdir, new_dir)
if chdir_ok then if chdir_ok then
if change_project_fn then change_project_fn() end if change_project_fn then change_project_fn() end
core.project_dir = common.normalize_volume(new_dir) core.project_dir = common.normalize_volume(new_dir)
core.project_directories = {} core.project_directories = {}
if not defer_add then
core.add_project_directory(new_dir)
end end
return true return chdir_ok
end
return false
end end
function core.close_current_project() function core.close_current_project()
@ -83,13 +79,22 @@ function core.close_current_project()
end end
end end
local function reload_customizations()
core.reload_module("core.style")
core.reload_module("core.config")
core.reload_module("core.keymap")
core.load_user_directory()
core.load_project_module()
end
function core.open_folder_project(dir_path_abs) function core.open_folder_project(dir_path_abs)
if core.set_project_dir(dir_path_abs, core.on_quit_project) then if core.set_project_dir(dir_path_abs, core.on_quit_project) then
core.root_view:close_all_docviews() core.root_view:close_all_docviews()
reload_customizations()
update_recents_project("add", dir_path_abs) update_recents_project("add", dir_path_abs)
if not core.load_project_module() then core.add_project_directory(dir_path_abs)
command.perform("core:open-log")
end
core.on_enter_project(dir_path_abs) core.on_enter_project(dir_path_abs)
end end
end end
@ -570,11 +575,9 @@ end
local function project_scan_add_file(dir, filepath) local function project_scan_add_file(dir, filepath)
local fileinfo = get_project_file_info(dir.name, PATHSEP .. filepath) local fileinfo = get_project_file_info(dir.name, PATHSEP .. filepath)
if not fileinfo_pass_filter(fileinfo) then return end if not fileinfo or not fileinfo_pass_filter(fileinfo) then return end
if fileinfo then
project_scan_add_entry(dir, fileinfo) project_scan_add_entry(dir, fileinfo)
end end
end
-- create a directory using mkdir but may need to create the parent -- create a directory using mkdir but may need to create the parent
@ -697,26 +700,16 @@ local function configure_borderless_window()
end end
local function reload_customizations()
core.reload_module("core.style")
core.reload_module("core.config")
core.reload_module("core.keymap")
core.load_user_directory()
core.load_project_module()
rescan_project_directories()
configure_borderless_window()
end
local function add_config_files_hooks() local function add_config_files_hooks()
-- auto-realod style when user's module is saved by overriding Doc:Save() -- auto-realod style when user's module is saved by overriding Doc:Save()
local doc_save = Doc.save local doc_save = Doc.save
local user_filename = system.absolute_path(USERDIR .. PATHSEP .. "init.lua") local user_filename = system.absolute_path(USERDIR .. PATHSEP .. "init.lua")
local module_filename = system.absolute_path(".lite_project.lua")
function Doc:save(filename, abs_filename) function Doc:save(filename, abs_filename)
doc_save(self, filename, abs_filename) doc_save(self, filename, abs_filename)
if self.abs_filename == user_filename or self.abs_filename == module_filename then if self.abs_filename == user_filename or self.abs_filename == core.project_module_filename then
reload_customizations() reload_customizations()
rescan_project_directories()
configure_borderless_window()
end end
end end
end end
@ -787,7 +780,7 @@ function core.init()
local project_dir_abs = system.absolute_path(project_dir) local project_dir_abs = system.absolute_path(project_dir)
-- We prevent set_project_dir below to effectively add and scan the directory becaese tha -- We prevent set_project_dir below to effectively add and scan the directory becaese tha
-- project module and its ignore files is not yet loaded. -- project module and its ignore files is not yet loaded.
local set_project_ok = project_dir_abs and core.set_project_dir(project_dir_abs, nil, true) local set_project_ok = project_dir_abs and core.set_project_dir(project_dir_abs)
if set_project_ok then if set_project_ok then
if project_dir_explicit then if project_dir_explicit then
update_recents_project("add", project_dir_abs) update_recents_project("add", project_dir_abs)
@ -797,7 +790,7 @@ function core.init()
update_recents_project("remove", project_dir) update_recents_project("remove", project_dir)
end end
project_dir_abs = system.absolute_path(".") project_dir_abs = system.absolute_path(".")
if not core.set_project_dir(project_dir_abs, nil, true) then if not core.set_project_dir(project_dir_abs) then
system.show_fatal_error("Lite XL internal error", "cannot set project directory to cwd") system.show_fatal_error("Lite XL internal error", "cannot set project directory to cwd")
os.exit(1) os.exit(1)
end end
@ -1028,6 +1021,7 @@ end
function core.load_project_module() function core.load_project_module()
local filename = ".lite_project.lua" local filename = ".lite_project.lua"
core.project_module_filename = system.absolute_path(filename)
if system.get_file_info(filename) then if system.get_file_info(filename) then
return core.try(function() return core.try(function()
local fn, err = loadfile(filename) local fn, err = loadfile(filename)