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