Deprecate core.add_save_hook to override Doc:save

In order to stay simple and closer to the lite's design principles we
deprecate the core.add_save_hook function and the related mechanism.
Instead we now directly override the Doc:save() method.

The method is already overrided from core.init to add the automatic
reloading of style when user's module is saved.

The cleanup is related to the discussion in issue #229.
This commit is contained in:
Francesco Abbate 2021-05-31 09:41:37 +02:00
parent 6d044224c1
commit 4e93eabbac
1 changed files with 22 additions and 11 deletions

View File

@ -411,6 +411,20 @@ local function whitespace_replacements()
end
local function reload_on_user_module_save()
-- auto-realod style when user's module is saved by overriding Doc:Save()
local doc_save = Doc.save
local user_filename = system.absolute_path(USERDIR .. PATHSEP .. "init.lua")
function Doc:save(filename)
doc_save(self)
if self.abs_filename == user_filename then
core.reload_module("core.style")
core.load_user_directory()
end
end
end
function core.init()
command = require "core.command"
keymap = require "core.keymap"
@ -553,6 +567,8 @@ function core.init()
if item.text == "Exit" then os.exit(1) end
end)
end
reload_on_user_module_save()
end
@ -612,13 +628,19 @@ do
end
-- DEPRECATED function
core.doc_save_hooks = {}
function core.add_save_hook(fn)
core.error("The function core.add_save_hook is deprecated." ..
" Modules should now directly override the Doc:save function.")
core.doc_save_hooks[#core.doc_save_hooks + 1] = fn
end
-- DEPRECATED function
function core.on_doc_save(filename)
-- for backward compatibility in modules. Hooks are deprecated, the function Doc:save
-- should be directly overidded.
for _, hook in ipairs(core.doc_save_hooks) do
hook(filename)
end
@ -1065,15 +1087,4 @@ function core.on_error(err)
end
core.add_save_hook(function(filename)
local doc = core.active_view.doc
local user_filename = system.absolute_path(USERDIR .. PATHSEP .. "init.lua")
if doc and doc:is(Doc) and doc.abs_filename == user_filename then
core.reload_module("core.style")
core.load_user_directory()
end
end)
return core