preview.nvim/lua/preview/commands.lua
Barrett Ruth 673573044f
feat: rename watch → toggle, auto-compile on start, built-in opener
Problem: :Preview watch only registered a BufWritePost autocmd without
compiling immediately, required boilerplate to open output files after
first compilation, and was misleadingly named.

Solution: Rename watch → toggle throughout. M.toggle now compiles
immediately on activation. Add an open field to ProviderConfig: true
calls vim.ui.open(), a string[] runs the command with the output path
appended, tracked per-buffer so the file opens only once. All presets
default to { 'xdg-open' }. Health check validates opener binaries.
Guard the async compile callback against invalid buffer ids.
2026-03-02 23:37:44 -05:00

54 lines
1.4 KiB
Lua

local M = {}
local subcommands = { 'compile', 'stop', 'clean', 'toggle', 'status' }
---@param args string
local function dispatch(args)
local subcmd = args ~= '' and args or 'compile'
if subcmd == 'compile' then
require('preview').compile()
elseif subcmd == 'stop' then
require('preview').stop()
elseif subcmd == 'clean' then
require('preview').clean()
elseif subcmd == 'toggle' then
require('preview').toggle()
elseif subcmd == 'status' then
local s = require('preview').status()
local parts = {}
if s.compiling then
table.insert(parts, 'compiling with "' .. s.provider .. '"')
else
table.insert(parts, 'idle')
end
if s.watching then
table.insert(parts, 'watching')
end
vim.notify('[preview.nvim] ' .. table.concat(parts, ', '), vim.log.levels.INFO)
else
vim.notify('[preview.nvim] unknown subcommand: ' .. subcmd, vim.log.levels.ERROR)
end
end
---@param lead string
---@return string[]
local function complete(lead)
return vim.tbl_filter(function(s)
return s:find(lead, 1, true) == 1
end, subcommands)
end
function M.setup()
vim.api.nvim_create_user_command('Preview', function(opts)
dispatch(opts.args)
end, {
nargs = '?',
complete = function(lead)
return complete(lead)
end,
desc = 'Compile, stop, clean, toggle, or check status of document preview',
})
end
return M