diff --git a/doc/preview.nvim.txt b/doc/preview.nvim.txt index e2747d6..6cba09a 100644 --- a/doc/preview.nvim.txt +++ b/doc/preview.nvim.txt @@ -93,12 +93,7 @@ Provider fields:~ successful compilation in toggle/watch mode. `true` uses |vim.ui.open()|. A string[] is run as a command with the - output path appended. When a string[] - 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. + output path appended. `reload` boolean|string[]|function Reload the output after recompilation. diff --git a/lua/preview/compiler.lua b/lua/preview/compiler.lua index c5a81c2..38a048e 100644 --- a/lua/preview/compiler.lua +++ b/lua/preview/compiler.lua @@ -15,49 +15,10 @@ local opened = {} ---@type table local last_output = {} ----@type table -local viewer_procs = {} - ----@type table -local open_watchers = {} - local debounce_timers = {} 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 ctx preview.Context ---@return string[] @@ -199,40 +160,21 @@ function M.compile(bufnr, name, provider, ctx, opts) end) ) - if provider.open and not opts.oneshot and not opened[bufnr] and output_file ~= '' then - local pre_stat = vim.uv.fs_stat(output_file) - local pre_mtime = pre_stat and pre_stat.mtime.sec or 0 - local out_dir = vim.fn.fnamemodify(output_file, ':h') - local out_name = vim.fn.fnamemodify(output_file, ':t') - stop_open_watcher(bufnr) - local watcher = vim.uv.new_fs_event() - if watcher then - open_watchers[bufnr] = watcher - watcher:start( - out_dir, - {}, - vim.schedule_wrap(function(err, filename, _events) - if err or vim.fn.fnamemodify(filename or '', ':t') ~= out_name then - return - 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 - end) - ) + if + provider.open + and not opts.oneshot + and not opened[bufnr] + and output_file ~= '' + and vim.uv.fs_stat(output_file) + then + 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 end 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, callback = function() M.stop(bufnr) - stop_open_watcher(bufnr) - close_viewer(bufnr) last_output[bufnr] = nil end, }) @@ -306,7 +246,13 @@ function M.compile(bufnr, name, provider, ctx, opts) and output_file ~= '' and vim.uv.fs_stat(output_file) 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 end else @@ -353,7 +299,6 @@ function M.compile(bufnr, name, provider, ctx, opts) once = true, callback = function() M.stop(bufnr) - close_viewer(bufnr) last_output[bufnr] = nil end, }) @@ -394,12 +339,6 @@ function M.stop_all() for bufnr, _ in pairs(watching) do M.unwatch(bufnr) 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() end @@ -455,8 +394,6 @@ function M.toggle(bufnr, name, provider, ctx_builder) once = true, callback = function() M.unwatch(bufnr) - stop_open_watcher(bufnr) - close_viewer(bufnr) opened[bufnr] = nil end, }) @@ -534,7 +471,13 @@ function M.open(bufnr, open_config) log.dbg('output file no longer exists for buffer %d: %s', bufnr, output) return false 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 end @@ -559,8 +502,6 @@ M._test = { opened = opened, last_output = last_output, debounce_timers = debounce_timers, - viewer_procs = viewer_procs, - open_watchers = open_watchers, } return M