feat: unified reload field for live-preview (SSE + long-running watch) (#19)
* feat(reload): add SSE live-reload server module
Problem: HTML output from pandoc has no live-reload; the browser must
be refreshed manually after each compile.
Solution: add lua/preview/reload.lua — a minimal SSE-only TCP server.
start() binds 127.0.0.1:5554 and keeps EventSource connections alive;
broadcast() pushes a reload event to all clients; inject() appends an
EventSource script before </body> (or at EOF) on every compile so
pandoc overwrites do not lose the tag.
* refactor(presets): add reload field, remove synctex field
Problem: the synctex field only handled PDF forward search and left
HTML live-preview and typst watch mode unsupported.
Solution: add reload = function(ctx) returning { 'typst', 'watch',
ctx.file } to typst (long-running watch mode), reload = true to
markdown and github (SSE push after each pandoc compile), and remove
synctex = true from latex (the -synctex=1 arg in latex.args remains
for .synctex.gz generation).
* refactor(init): replace synctex field and validation with reload
Problem: ProviderConfig still declared synctex and validated it, but
the field is being dropped in favour of the general-purpose reload.
Solution: replace the synctex annotation and vim.validate call with the
reload field, accepting boolean | string[] | function.
* feat(compiler): support long-running watch processes and SSE reload
Problem: compile() only supports one-shot invocations, requiring a
BufWritePost autocmd for watch mode and leaving HTML without live-
reload.
Solution: resolve_reload_cmd() maps provider.reload (function or table)
to a command; when present, compile() spawns it as a long-running
process instead of building a one-shot cmd from provider.cmd + args.
toggle() detects long-running providers and toggles the process
directly instead of registering a BufWritePost autocmd. When
reload = true and output is .html, the SSE server is invoked after
each successful compile. status() reports is_reload processes as
watching, not compiling. stop_all() also stops the SSE server.
* fix(compiler): format is_longrunning and annotate is_reload field
Problem: stylua required is_longrunning to be on one line; lua-ls
warned about undefined field is_reload on preview.Process.
Solution: inline the boolean expression and add is_reload? to the
preview.Process annotation.
* refactor: rename compile/toggle commands to build/watch
Problem: `compile` and `toggle` are accurate but unintuitive — `compile`
sounds academic and `toggle` says nothing about what it toggles.
Solution: rename the public API and `:Preview` subcommands to `build`
(one-shot) and `watch` (live preview). Internal compiler functions are
unchanged. No aliases for old names — clean break.
This commit is contained in:
parent
bce3cec0e6
commit
62961c8541
8 changed files with 340 additions and 20 deletions
|
|
@ -39,6 +39,18 @@ local function eval_string(val, ctx)
|
|||
return val
|
||||
end
|
||||
|
||||
---@param provider preview.ProviderConfig
|
||||
---@param ctx preview.Context
|
||||
---@return string[]?
|
||||
local function resolve_reload_cmd(provider, ctx)
|
||||
if type(provider.reload) == 'function' then
|
||||
return provider.reload(ctx)
|
||||
elseif type(provider.reload) == 'table' then
|
||||
return vim.list_extend({}, provider.reload)
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
---@param bufnr integer
|
||||
---@param name string
|
||||
---@param provider preview.ProviderConfig
|
||||
|
|
@ -60,11 +72,6 @@ function M.compile(bufnr, name, provider, ctx)
|
|||
|
||||
local resolved_ctx = vim.tbl_extend('force', ctx, { output = output_file })
|
||||
|
||||
local cmd = vim.list_extend({}, provider.cmd)
|
||||
if provider.args then
|
||||
vim.list_extend(cmd, eval_list(provider.args, resolved_ctx))
|
||||
end
|
||||
|
||||
local cwd = ctx.root
|
||||
if provider.cwd then
|
||||
cwd = eval_string(provider.cwd, resolved_ctx)
|
||||
|
|
@ -74,6 +81,103 @@ function M.compile(bufnr, name, provider, ctx)
|
|||
last_output[bufnr] = output_file
|
||||
end
|
||||
|
||||
local reload_cmd = resolve_reload_cmd(provider, resolved_ctx)
|
||||
|
||||
if reload_cmd then
|
||||
log.dbg(
|
||||
'starting long-running process for buffer %d with provider "%s": %s',
|
||||
bufnr,
|
||||
name,
|
||||
table.concat(reload_cmd, ' ')
|
||||
)
|
||||
|
||||
local obj = vim.system(
|
||||
reload_cmd,
|
||||
{
|
||||
cwd = cwd,
|
||||
env = provider.env,
|
||||
},
|
||||
vim.schedule_wrap(function(result)
|
||||
active[bufnr] = nil
|
||||
if not vim.api.nvim_buf_is_valid(bufnr) then
|
||||
return
|
||||
end
|
||||
|
||||
if result.code ~= 0 then
|
||||
log.dbg('long-running process failed for buffer %d (exit code %d)', bufnr, result.code)
|
||||
local errors_mode = provider.errors
|
||||
if errors_mode == nil then
|
||||
errors_mode = 'diagnostic'
|
||||
end
|
||||
if provider.error_parser and errors_mode then
|
||||
local output = (result.stdout or '') .. (result.stderr or '')
|
||||
if errors_mode == 'diagnostic' then
|
||||
diagnostic.set(bufnr, name, provider.error_parser, output, ctx)
|
||||
elseif errors_mode == 'quickfix' then
|
||||
local ok, diagnostics = pcall(provider.error_parser, output, ctx)
|
||||
if ok and diagnostics and #diagnostics > 0 then
|
||||
local items = {}
|
||||
for _, d in ipairs(diagnostics) do
|
||||
table.insert(items, {
|
||||
bufnr = bufnr,
|
||||
lnum = d.lnum + 1,
|
||||
col = d.col + 1,
|
||||
text = d.message,
|
||||
type = d.severity == vim.diagnostic.severity.WARN and 'W' or 'E',
|
||||
})
|
||||
end
|
||||
vim.fn.setqflist(items, 'r')
|
||||
vim.cmd('copen')
|
||||
end
|
||||
end
|
||||
end
|
||||
vim.api.nvim_exec_autocmds('User', {
|
||||
pattern = 'PreviewCompileFailed',
|
||||
data = {
|
||||
bufnr = bufnr,
|
||||
provider = name,
|
||||
code = result.code,
|
||||
stderr = result.stderr or '',
|
||||
},
|
||||
})
|
||||
end
|
||||
end)
|
||||
)
|
||||
|
||||
if provider.open and not opened[bufnr] and 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 }
|
||||
|
||||
vim.api.nvim_create_autocmd('BufWipeout', {
|
||||
buffer = bufnr,
|
||||
once = true,
|
||||
callback = function()
|
||||
M.stop(bufnr)
|
||||
last_output[bufnr] = nil
|
||||
end,
|
||||
})
|
||||
|
||||
vim.api.nvim_exec_autocmds('User', {
|
||||
pattern = 'PreviewCompileStarted',
|
||||
data = { bufnr = bufnr, provider = name },
|
||||
})
|
||||
return
|
||||
end
|
||||
|
||||
local cmd = vim.list_extend({}, provider.cmd)
|
||||
if provider.args then
|
||||
vim.list_extend(cmd, eval_list(provider.args, resolved_ctx))
|
||||
end
|
||||
|
||||
log.dbg('compiling buffer %d with provider "%s": %s', bufnr, name, table.concat(cmd, ' '))
|
||||
|
||||
local obj = vim.system(
|
||||
|
|
@ -104,6 +208,12 @@ function M.compile(bufnr, name, provider, ctx)
|
|||
pattern = 'PreviewCompileSuccess',
|
||||
data = { bufnr = bufnr, provider = name, output = output_file },
|
||||
})
|
||||
if provider.reload == true and output_file:match('%.html$') then
|
||||
local r = require('preview.reload')
|
||||
r.start()
|
||||
r.inject(output_file)
|
||||
r.broadcast()
|
||||
end
|
||||
if provider.open and not opened[bufnr] and output_file ~= '' then
|
||||
if provider.open == true then
|
||||
vim.ui.open(output_file)
|
||||
|
|
@ -198,6 +308,7 @@ function M.stop_all()
|
|||
for bufnr, _ in pairs(watching) do
|
||||
M.unwatch(bufnr)
|
||||
end
|
||||
require('preview.reload').stop()
|
||||
end
|
||||
|
||||
---@param bufnr integer
|
||||
|
|
@ -205,6 +316,19 @@ end
|
|||
---@param provider preview.ProviderConfig
|
||||
---@param ctx_builder fun(bufnr: integer): preview.Context
|
||||
function M.toggle(bufnr, name, provider, ctx_builder)
|
||||
local is_longrunning = type(provider.reload) == 'table' or type(provider.reload) == 'function'
|
||||
|
||||
if is_longrunning then
|
||||
if active[bufnr] then
|
||||
M.stop(bufnr)
|
||||
vim.notify('[preview.nvim]: watching stopped', vim.log.levels.INFO)
|
||||
else
|
||||
M.compile(bufnr, name, provider, ctx_builder(bufnr))
|
||||
vim.notify('[preview.nvim]: watching with "' .. name .. '"', vim.log.levels.INFO)
|
||||
end
|
||||
return
|
||||
end
|
||||
|
||||
if watching[bufnr] then
|
||||
M.unwatch(bufnr)
|
||||
vim.notify('[preview.nvim]: watching stopped', vim.log.levels.INFO)
|
||||
|
|
@ -313,8 +437,8 @@ function M.status(bufnr)
|
|||
local proc = active[bufnr]
|
||||
if proc then
|
||||
return {
|
||||
compiling = true,
|
||||
watching = watching[bufnr] ~= nil,
|
||||
compiling = not proc.is_reload,
|
||||
watching = watching[bufnr] ~= nil or proc.is_reload == true,
|
||||
provider = proc.provider,
|
||||
output_file = proc.output_file,
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue