diff --git a/lua/preview/commands.lua b/lua/preview/commands.lua index c91f4de..2c52c5a 100644 --- a/lua/preview/commands.lua +++ b/lua/preview/commands.lua @@ -1,8 +1,8 @@ local M = {} local handlers = { - build = function() - require('preview').build() + compile = function() + require('preview').compile() end, stop = function() require('preview').stop() @@ -10,8 +10,8 @@ local handlers = { clean = function() require('preview').clean() end, - watch = function() - require('preview').watch() + toggle = function() + require('preview').toggle() end, open = function() require('preview').open() @@ -33,7 +33,7 @@ local handlers = { ---@param args string local function dispatch(args) - local subcmd = args ~= '' and args or 'build' + local subcmd = args ~= '' and args or 'compile' local handler = handlers[subcmd] if handler then handler() @@ -58,7 +58,7 @@ function M.setup() complete = function(lead) return complete(lead) end, - desc = 'Build, stop, clean, watch, open, or check status of document preview', + desc = 'Compile, stop, clean, toggle, open, or check status of document preview', }) end diff --git a/lua/preview/compiler.lua b/lua/preview/compiler.lua index e19cbf0..eca9f4d 100644 --- a/lua/preview/compiler.lua +++ b/lua/preview/compiler.lua @@ -39,18 +39,6 @@ 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 @@ -72,6 +60,11 @@ 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) @@ -81,103 +74,6 @@ 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( @@ -208,12 +104,6 @@ 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) @@ -308,7 +198,6 @@ function M.stop_all() for bufnr, _ in pairs(watching) do M.unwatch(bufnr) end - require('preview.reload').stop() end ---@param bufnr integer @@ -316,19 +205,6 @@ 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) @@ -437,8 +313,8 @@ function M.status(bufnr) local proc = active[bufnr] if proc then return { - compiling = not proc.is_reload, - watching = watching[bufnr] ~= nil or proc.is_reload == true, + compiling = true, + watching = watching[bufnr] ~= nil, provider = proc.provider, output_file = proc.output_file, } diff --git a/lua/preview/init.lua b/lua/preview/init.lua index bbf70f1..2bee03a 100644 --- a/lua/preview/init.lua +++ b/lua/preview/init.lua @@ -9,7 +9,6 @@ ---@field errors? false|'diagnostic'|'quickfix' ---@field clean? string[]|fun(ctx: preview.Context): string[] ---@field open? boolean|string[] ----@field reload? boolean|string[]|fun(ctx: preview.Context): string[] ---@class preview.Config ---@field debug boolean|string @@ -35,14 +34,13 @@ ---@field obj table ---@field provider string ---@field output_file string ----@field is_reload? boolean ---@class preview ---@field setup fun(opts?: table) ----@field build fun(bufnr?: integer) +---@field compile fun(bufnr?: integer) ---@field stop fun(bufnr?: integer) ---@field clean fun(bufnr?: integer) ----@field watch fun(bufnr?: integer) +---@field toggle fun(bufnr?: integer) ---@field open fun(bufnr?: integer) ---@field status fun(bufnr?: integer): preview.Status ---@field statusline fun(bufnr?: integer): string @@ -100,7 +98,6 @@ function M.setup(opts) return x == nil or x == false or x == 'diagnostic' or x == 'quickfix' end, 'false, "diagnostic", or "quickfix"') vim.validate(prefix .. '.open', provider.open, { 'boolean', 'table' }, true) - vim.validate(prefix .. '.reload', provider.reload, { 'boolean', 'table', 'function' }, true) end config = vim.tbl_deep_extend('force', default_config, { @@ -144,7 +141,7 @@ function M.build_context(bufnr) end ---@param bufnr? integer -function M.build(bufnr) +function M.compile(bufnr) bufnr = bufnr or vim.api.nvim_get_current_buf() local name = M.resolve_provider(bufnr) if not name then @@ -176,7 +173,7 @@ function M.clean(bufnr) end ---@param bufnr? integer -function M.watch(bufnr) +function M.toggle(bufnr) bufnr = bufnr or vim.api.nvim_get_current_buf() local name = M.resolve_provider(bufnr) if not name then diff --git a/lua/preview/presets.lua b/lua/preview/presets.lua index fc20f89..c1de0df 100644 --- a/lua/preview/presets.lua +++ b/lua/preview/presets.lua @@ -107,9 +107,6 @@ M.typst = { return parse_typst(output) end, open = true, - reload = function(ctx) - return { 'typst', 'watch', ctx.file } - end, } ---@type preview.ProviderConfig @@ -120,7 +117,6 @@ M.latex = { return { '-pdf', '-interaction=nonstopmode', - '-synctex=1', '-pdflatex=pdflatex -file-line-error -interaction=nonstopmode %O %S', ctx.file, } @@ -154,7 +150,6 @@ M.markdown = { return { 'rm', '-f', (ctx.file:gsub('%.md$', '.html')) } end, open = true, - reload = true, } ---@type preview.ProviderConfig @@ -184,7 +179,6 @@ M.github = { return { 'rm', '-f', (ctx.file:gsub('%.md$', '.html')) } end, open = true, - reload = true, } return M diff --git a/lua/preview/reload.lua b/lua/preview/reload.lua deleted file mode 100644 index fd64309..0000000 --- a/lua/preview/reload.lua +++ /dev/null @@ -1,108 +0,0 @@ -local M = {} - -local PORT = 5554 -local server_handle = nil -local clients = {} - -local function make_script(port) - return '' -end - -function M.start(port) - port = port or PORT - if server_handle then - return - end - local server = vim.uv.new_tcp() - server:bind('127.0.0.1', port) - server:listen(128, function(err) - if err then - return - end - local client = vim.uv.new_tcp() - server:accept(client) - local buf = '' - client:read_start(function(read_err, data) - if read_err or not data then - if not client:is_closing() then - client:close() - end - return - end - buf = buf .. data - if buf:find('\r\n\r\n') or buf:find('\n\n') then - client:read_stop() - local first_line = buf:match('^([^\r\n]+)') - if first_line and first_line:find('/__live/events', 1, true) then - local response = 'HTTP/1.1 200 OK\r\n' - .. 'Content-Type: text/event-stream\r\n' - .. 'Cache-Control: no-cache\r\n' - .. 'Access-Control-Allow-Origin: *\r\n' - .. '\r\n' - client:write(response) - table.insert(clients, client) - else - client:close() - end - end - end) - end) - server_handle = server -end - -function M.stop() - for _, c in ipairs(clients) do - if not c:is_closing() then - c:close() - end - end - clients = {} - if server_handle then - server_handle:close() - server_handle = nil - end -end - -function M.broadcast() - local event = 'event: reload\ndata: {}\n\n' - local alive = {} - for _, c in ipairs(clients) do - if not c:is_closing() then - local ok = pcall(function() - c:write(event) - end) - if ok then - table.insert(alive, c) - end - end - end - clients = alive -end - -function M.inject(path, port) - port = port or PORT - local f = io.open(path, 'r') - if not f then - return - end - local content = f:read('*a') - f:close() - local script = make_script(port) - local new_content, n = content:gsub('
hello