2022-05-31 22:34:14 +02:00
|
|
|
-- mod-version:3
|
2019-12-28 12:16:32 +01:00
|
|
|
local core = require "core"
|
|
|
|
local config = require "core.config"
|
|
|
|
local Doc = require "core.doc"
|
2022-05-15 21:21:26 +02:00
|
|
|
local Node = require "core.node"
|
|
|
|
local common = require "core.common"
|
|
|
|
local dirwatch = require "core.dirwatch"
|
2019-12-28 12:16:32 +01:00
|
|
|
|
2022-05-15 21:21:26 +02:00
|
|
|
config.plugins.autoreload = common.merge({
|
2022-05-23 21:12:37 +02:00
|
|
|
always_show_nagview = false,
|
|
|
|
config_spec = {
|
|
|
|
name = "Autoreload",
|
|
|
|
{
|
|
|
|
label = "Always Show Nagview",
|
|
|
|
description = "Alerts you if an opened file changes externally even if you haven't modified it.",
|
|
|
|
path = "always_show_nagview",
|
|
|
|
type = "toggle",
|
|
|
|
default = false
|
|
|
|
}
|
|
|
|
}
|
2022-05-15 21:21:26 +02:00
|
|
|
}, config.plugins.autoreload)
|
|
|
|
|
|
|
|
local watch = dirwatch.new()
|
2019-12-28 12:16:32 +01:00
|
|
|
local times = setmetatable({}, { __mode = "k" })
|
2022-05-15 21:21:26 +02:00
|
|
|
local visible = setmetatable({}, { __mode = "k" })
|
|
|
|
|
|
|
|
local function get_project_doc_watch(doc)
|
2024-11-30 15:44:52 +01:00
|
|
|
for _, v in ipairs(core.project_directories) do
|
2022-05-15 21:21:26 +02:00
|
|
|
if doc.abs_filename:find(v.name, 1, true) == 1 then return v.watch end
|
|
|
|
end
|
|
|
|
return watch
|
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
|
|
|
|
local function update_time(doc)
|
2022-05-15 21:21:26 +02:00
|
|
|
times[doc] = system.get_file_info(doc.filename).modified
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
local function reload_doc(doc)
|
2022-05-15 21:21:26 +02:00
|
|
|
doc:reload()
|
2019-12-28 12:16:32 +01:00
|
|
|
update_time(doc)
|
2022-05-15 21:21:26 +02:00
|
|
|
core.redraw = true
|
2020-04-25 14:26:55 +02:00
|
|
|
core.log_quiet("Auto-reloaded doc \"%s\"", doc.filename)
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
2024-11-30 15:44:52 +01:00
|
|
|
|
|
|
|
local timers = setmetatable({}, { __mode = "k" })
|
|
|
|
|
|
|
|
local function delayed_reload(doc, mtime)
|
|
|
|
if timers[doc] then
|
|
|
|
-- If mtime remains the same, there's no need to restart the timer
|
|
|
|
-- as we're waiting a full second anyways.
|
|
|
|
if not mtime or timers[doc].mtime ~= mtime then
|
|
|
|
timers[doc] = { last_trigger = system.get_time(), mtime = mtime }
|
|
|
|
end
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
timers[doc] = { last_trigger = system.get_time(), mtime = mtime }
|
|
|
|
core.add_thread(function()
|
|
|
|
local diff = system.get_time() - timers[doc].last_trigger
|
|
|
|
-- Wait a second before triggering a reload because we're using mtime
|
|
|
|
-- to determine if a file has changed, and on many systems it has a
|
|
|
|
-- resolution of 1 second.
|
|
|
|
while diff < 1 do
|
|
|
|
coroutine.yield(diff)
|
|
|
|
diff = system.get_time() - timers[doc].last_trigger
|
|
|
|
end
|
|
|
|
timers[doc] = nil
|
|
|
|
reload_doc(doc)
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2022-05-15 21:21:26 +02:00
|
|
|
local function check_prompt_reload(doc)
|
|
|
|
if doc and doc.deferred_reload then
|
|
|
|
core.nag_view:show("File Changed", doc.filename .. " has changed. Reload this file?", {
|
2023-02-06 18:38:00 +01:00
|
|
|
{ text = "Yes", default_yes = true },
|
|
|
|
{ text = "No", default_no = true }
|
2022-05-15 21:21:26 +02:00
|
|
|
}, function(item)
|
|
|
|
if item.text == "Yes" then reload_doc(doc) end
|
|
|
|
doc.deferred_reload = false
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
|
2022-05-15 21:21:26 +02:00
|
|
|
local function doc_changes_visiblity(doc, visibility)
|
|
|
|
if doc and visible[doc] ~= visibility and doc.abs_filename then
|
|
|
|
visible[doc] = visibility
|
|
|
|
if visibility then check_prompt_reload(doc) end
|
|
|
|
get_project_doc_watch(doc):watch(doc.abs_filename, visibility)
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
2021-10-08 23:15:25 +02:00
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
|
2022-05-15 21:21:26 +02:00
|
|
|
local on_check = dirwatch.check
|
|
|
|
function dirwatch:check(change_callback, ...)
|
|
|
|
on_check(self, function(dir)
|
|
|
|
for _, doc in ipairs(core.docs) do
|
2022-05-31 01:03:29 +02:00
|
|
|
if doc.abs_filename and (dir == common.dirname(doc.abs_filename) or dir == doc.abs_filename) then
|
2022-05-15 21:21:26 +02:00
|
|
|
local info = system.get_file_info(doc.filename or "")
|
2024-01-21 01:49:25 +01:00
|
|
|
if info and info.type == "file" and times[doc] ~= info.modified then
|
2022-05-15 21:21:26 +02:00
|
|
|
if not doc:is_dirty() and not config.plugins.autoreload.always_show_nagview then
|
2024-11-30 15:44:52 +01:00
|
|
|
delayed_reload(doc, info.modified)
|
2022-05-15 21:21:26 +02:00
|
|
|
else
|
|
|
|
doc.deferred_reload = true
|
|
|
|
if doc == core.active_view.doc then check_prompt_reload(doc) end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
change_callback(dir)
|
|
|
|
end, ...)
|
|
|
|
end
|
|
|
|
|
|
|
|
local core_set_active_view = core.set_active_view
|
|
|
|
function core.set_active_view(view)
|
|
|
|
core_set_active_view(view)
|
|
|
|
doc_changes_visiblity(view.doc, true)
|
|
|
|
end
|
|
|
|
|
|
|
|
local node_set_active_view = Node.set_active_view
|
|
|
|
function Node:set_active_view(view)
|
|
|
|
if self.active_view then doc_changes_visiblity(self.active_view.doc, false) end
|
|
|
|
node_set_active_view(self, view)
|
|
|
|
doc_changes_visiblity(self.active_view.doc, true)
|
|
|
|
end
|
|
|
|
|
|
|
|
core.add_thread(function()
|
|
|
|
while true do
|
|
|
|
-- because we already hook this function above; we only
|
|
|
|
-- need to check the file.
|
|
|
|
watch:check(function() end)
|
|
|
|
coroutine.yield(0.05)
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
-- patch `Doc.save|load` to store modified time
|
|
|
|
local load = Doc.load
|
|
|
|
local save = Doc.save
|
|
|
|
|
|
|
|
Doc.load = function(self, ...)
|
|
|
|
local res = load(self, ...)
|
|
|
|
update_time(self)
|
|
|
|
return res
|
|
|
|
end
|
|
|
|
|
|
|
|
Doc.save = function(self, ...)
|
|
|
|
local res = save(self, ...)
|
2022-05-15 21:21:26 +02:00
|
|
|
-- if starting with an unsaved document with a filename.
|
|
|
|
if not times[self] then get_project_doc_watch(self):watch(self.abs_filename, true) end
|
2019-12-28 12:16:32 +01:00
|
|
|
update_time(self)
|
|
|
|
return res
|
|
|
|
end
|