Compare commits

..

No commits in common. "e03f51ca040882171a6bed5302eaa84ebe4936a5" and "df0765a27f59ab4d8256c02e91708eb4bf828cf6" have entirely different histories.

2 changed files with 29 additions and 93 deletions

View file

@ -93,12 +93,7 @@ Provider fields:~
successful compilation in toggle/watch successful compilation in toggle/watch
mode. `true` uses |vim.ui.open()|. A mode. `true` uses |vim.ui.open()|. A
string[] is run as a command with the string[] is run as a command with the
output path appended. When a string[] output path appended.
is used the viewer process is tracked
and sent SIGTERM when the buffer is
deleted. `true` and single-instance
apps (e.g. Chrome) do not support
auto-close.
`reload` boolean|string[]|function `reload` boolean|string[]|function
Reload the output after recompilation. Reload the output after recompilation.

View file

@ -15,49 +15,10 @@ local opened = {}
---@type table<integer, string> ---@type table<integer, string>
local last_output = {} local last_output = {}
---@type table<integer, table>
local viewer_procs = {}
---@type table<integer, uv.uv_fs_event_t>
local open_watchers = {}
local debounce_timers = {} local debounce_timers = {}
local DEBOUNCE_MS = 500 local DEBOUNCE_MS = 500
---@param bufnr integer
local function stop_open_watcher(bufnr)
local w = open_watchers[bufnr]
if w then
w:stop()
w:close()
open_watchers[bufnr] = nil
end
end
---@param bufnr integer
local function close_viewer(bufnr)
local obj = viewer_procs[bufnr]
if obj then
local kill = obj.kill
kill(obj, 'sigterm')
viewer_procs[bufnr] = nil
end
end
---@param bufnr integer
---@param output_file string
---@param open_config boolean|string[]
local function do_open(bufnr, output_file, open_config)
if open_config == true then
vim.ui.open(output_file)
elseif type(open_config) == 'table' then
local open_cmd = vim.list_extend({}, open_config)
table.insert(open_cmd, output_file)
viewer_procs[bufnr] = vim.system(open_cmd)
end
end
---@param val string[]|fun(ctx: preview.Context): string[] ---@param val string[]|fun(ctx: preview.Context): string[]
---@param ctx preview.Context ---@param ctx preview.Context
---@return string[] ---@return string[]
@ -199,40 +160,21 @@ function M.compile(bufnr, name, provider, ctx, opts)
end) end)
) )
if provider.open and not opts.oneshot and not opened[bufnr] and output_file ~= '' then if
local pre_stat = vim.uv.fs_stat(output_file) provider.open
local pre_mtime = pre_stat and pre_stat.mtime.sec or 0 and not opts.oneshot
local out_dir = vim.fn.fnamemodify(output_file, ':h') and not opened[bufnr]
local out_name = vim.fn.fnamemodify(output_file, ':t') and output_file ~= ''
stop_open_watcher(bufnr) and vim.uv.fs_stat(output_file)
local watcher = vim.uv.new_fs_event() then
if watcher then if provider.open == true then
open_watchers[bufnr] = watcher vim.ui.open(output_file)
watcher:start( elseif type(provider.open) == 'table' then
out_dir, local open_cmd = vim.list_extend({}, provider.open)
{}, table.insert(open_cmd, output_file)
vim.schedule_wrap(function(err, filename, _events) vim.system(open_cmd)
if err or vim.fn.fnamemodify(filename or '', ':t') ~= out_name then
return
end end
if opened[bufnr] then
stop_open_watcher(bufnr)
return
end
if not vim.api.nvim_buf_is_valid(bufnr) then
stop_open_watcher(bufnr)
return
end
local new_stat = vim.uv.fs_stat(output_file)
if not (new_stat and new_stat.mtime.sec > pre_mtime) then
return
end
stop_open_watcher(bufnr)
do_open(bufnr, output_file, provider.open)
opened[bufnr] = true opened[bufnr] = true
end)
)
end
end end
active[bufnr] = { obj = obj, provider = name, output_file = output_file, is_reload = true } active[bufnr] = { obj = obj, provider = name, output_file = output_file, is_reload = true }
@ -242,8 +184,6 @@ function M.compile(bufnr, name, provider, ctx, opts)
once = true, once = true,
callback = function() callback = function()
M.stop(bufnr) M.stop(bufnr)
stop_open_watcher(bufnr)
close_viewer(bufnr)
last_output[bufnr] = nil last_output[bufnr] = nil
end, end,
}) })
@ -306,7 +246,13 @@ function M.compile(bufnr, name, provider, ctx, opts)
and output_file ~= '' and output_file ~= ''
and vim.uv.fs_stat(output_file) and vim.uv.fs_stat(output_file)
then then
do_open(bufnr, output_file, provider.open) if provider.open == true then
vim.ui.open(output_file)
elseif type(provider.open) == 'table' then
local open_cmd = vim.list_extend({}, provider.open)
table.insert(open_cmd, output_file)
vim.system(open_cmd)
end
opened[bufnr] = true opened[bufnr] = true
end end
else else
@ -353,7 +299,6 @@ function M.compile(bufnr, name, provider, ctx, opts)
once = true, once = true,
callback = function() callback = function()
M.stop(bufnr) M.stop(bufnr)
close_viewer(bufnr)
last_output[bufnr] = nil last_output[bufnr] = nil
end, end,
}) })
@ -394,12 +339,6 @@ function M.stop_all()
for bufnr, _ in pairs(watching) do for bufnr, _ in pairs(watching) do
M.unwatch(bufnr) M.unwatch(bufnr)
end end
for bufnr, _ in pairs(open_watchers) do
stop_open_watcher(bufnr)
end
for bufnr, _ in pairs(viewer_procs) do
close_viewer(bufnr)
end
require('preview.reload').stop() require('preview.reload').stop()
end end
@ -455,8 +394,6 @@ function M.toggle(bufnr, name, provider, ctx_builder)
once = true, once = true,
callback = function() callback = function()
M.unwatch(bufnr) M.unwatch(bufnr)
stop_open_watcher(bufnr)
close_viewer(bufnr)
opened[bufnr] = nil opened[bufnr] = nil
end, end,
}) })
@ -534,7 +471,13 @@ function M.open(bufnr, open_config)
log.dbg('output file no longer exists for buffer %d: %s', bufnr, output) log.dbg('output file no longer exists for buffer %d: %s', bufnr, output)
return false return false
end end
do_open(bufnr, output, open_config) if type(open_config) == 'table' then
local open_cmd = vim.list_extend({}, open_config)
table.insert(open_cmd, output)
vim.system(open_cmd)
else
vim.ui.open(output)
end
return true return true
end end
@ -559,8 +502,6 @@ M._test = {
opened = opened, opened = opened,
last_output = last_output, last_output = last_output,
debounce_timers = debounce_timers, debounce_timers = debounce_timers,
viewer_procs = viewer_procs,
open_watchers = open_watchers,
} }
return M return M