Modified autoreload to use new dirwatch infrastructure, and added in nagview to verify that fs changes don't stomp on our changes, unless you want them to.

This commit is contained in:
Adam Harrison 2022-04-21 22:33:00 -04:00 committed by jgmdev
parent f89b370369
commit d56f4e1ee5
1 changed files with 40 additions and 11 deletions

View File

@ -1,7 +1,10 @@
-- mod-version:2 -- lite-xl 2.0 -- mod-version:2 -- lite-xl 2.0
local core = require "core" local core = require "core"
local config = require "core.config" local config = require "core.config"
local style = require "core.style"
local Doc = require "core.doc" local Doc = require "core.doc"
local common = require "core.common"
local dirwatch = require "core.dirwatch"
local times = setmetatable({}, { __mode = "k" }) local times = setmetatable({}, { __mode = "k" })
@ -25,18 +28,44 @@ local function reload_doc(doc)
core.log_quiet("Auto-reloaded doc \"%s\"", doc.filename) core.log_quiet("Auto-reloaded doc \"%s\"", doc.filename)
end end
local on_modify = core.on_dirmonitor_modify local function check_prompt_reload()
if core.active_view.doc and core.active_view.doc.deferred_reload then
core.on_dirmonitor_modify = function(dir, filepath) local doc = core.active_view.doc
local abs_filename = dir.name .. PATHSEP .. filepath core.nag_view:show("File Changed", doc.filename .. " has changed. Reload this file?", {
for _, doc in ipairs(core.docs) do { font = style.font, text = "Yes", default_yes = true },
local info = system.get_file_info(doc.filename or "") { font = style.font, text = "No" , default_no = true }
if doc.abs_filename == abs_filename and info and times[doc] ~= info.modified then }, function(item)
reload_doc(doc) if item.text == "Yes" then reload_doc(doc) end
break doc.deferred_reload = false
end end)
end end
on_modify(dir, filepath) end
local on_check = dirwatch.check
function dirwatch:check(change_callback, ...)
on_check(self, function(dir)
for _, doc in ipairs(core.docs) do
if dir == common.dirname(doc.abs_filename) then
local info = system.get_file_info(doc.filename or "")
if info and times[doc] ~= info.modified then
if not doc:is_dirty() then
reload_doc(doc)
else
doc.deferred_reload = true
end
break
end
end
end
change_callback(dir)
end, ...)
check_prompt_reload()
end
local set_active_view = core.set_active_view
function core.set_active_view(view)
set_active_view(view)
check_prompt_reload()
end end
-- patch `Doc.save|load` to store modified time -- patch `Doc.save|load` to store modified time