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.
This commit is contained in:
Barrett Ruth 2026-03-02 23:37:44 -05:00
parent c62c930454
commit 673573044f
Signed by: barrett
GPG key ID: A6C96C9349D2FC81
12 changed files with 346 additions and 176 deletions

View file

@ -35,10 +35,10 @@ describe('commands', function()
end)
end)
it('does not error on :Preview watch with no provider', function()
it('does not error on :Preview toggle with no provider', function()
require('preview.commands').setup()
assert.has_no.errors(function()
vim.cmd('Preview watch')
vim.cmd('Preview toggle')
end)
end)
end)

View file

@ -174,7 +174,7 @@ describe('compiler', function()
end)
end)
describe('watch', function()
describe('toggle', function()
it('registers autocmd and tracks in watching table', function()
local bufnr = helpers.create_buffer({ 'hello' }, 'text')
vim.api.nvim_buf_set_name(bufnr, '/tmp/preview_test_watch.txt')
@ -184,7 +184,7 @@ describe('compiler', function()
return { bufnr = b, file = '/tmp/preview_test_watch.txt', root = '/tmp', ft = 'text' }
end
compiler.watch(bufnr, 'echo', provider, ctx_builder)
compiler.toggle(bufnr, 'echo', provider, ctx_builder)
assert.is_not_nil(compiler._test.watching[bufnr])
helpers.delete_buffer(bufnr)
@ -208,7 +208,7 @@ describe('compiler', function()
return { bufnr = b, file = '/tmp/preview_test_watch_event.txt', root = '/tmp', ft = 'text' }
end
compiler.watch(bufnr, 'echo', provider, ctx_builder)
compiler.toggle(bufnr, 'echo', provider, ctx_builder)
assert.is_true(fired)
compiler.unwatch(bufnr)
@ -224,10 +224,10 @@ describe('compiler', function()
return { bufnr = b, file = '/tmp/preview_test_watch_toggle.txt', root = '/tmp', ft = 'text' }
end
compiler.watch(bufnr, 'echo', provider, ctx_builder)
compiler.toggle(bufnr, 'echo', provider, ctx_builder)
assert.is_not_nil(compiler._test.watching[bufnr])
compiler.watch(bufnr, 'echo', provider, ctx_builder)
compiler.toggle(bufnr, 'echo', provider, ctx_builder)
assert.is_nil(compiler._test.watching[bufnr])
helpers.delete_buffer(bufnr)
@ -251,7 +251,7 @@ describe('compiler', function()
return { bufnr = b, file = '/tmp/preview_test_watch_stop.txt', root = '/tmp', ft = 'text' }
end
compiler.watch(bufnr, 'echo', provider, ctx_builder)
compiler.toggle(bufnr, 'echo', provider, ctx_builder)
compiler.unwatch(bufnr)
assert.is_true(stopped)
assert.is_nil(compiler._test.watching[bufnr])
@ -273,7 +273,7 @@ describe('compiler', function()
}
end
compiler.watch(bufnr, 'echo', provider, ctx_builder)
compiler.toggle(bufnr, 'echo', provider, ctx_builder)
assert.is_not_nil(compiler._test.watching[bufnr])
compiler.stop_all()
@ -294,7 +294,7 @@ describe('compiler', function()
return { bufnr = b, file = '/tmp/preview_test_watch_status.txt', root = '/tmp', ft = 'text' }
end
compiler.watch(bufnr, 'echo', provider, ctx_builder)
compiler.toggle(bufnr, 'echo', provider, ctx_builder)
s = compiler.status(bufnr)
assert.is_true(s.watching)

View file

@ -20,8 +20,11 @@ function M.delete_buffer(bufnr)
end
function M.reset_config(opts)
vim.g.preview = opts
require('preview')._test.reset()
local preview = require('preview')
preview._test.reset()
if opts then
preview.setup(opts)
end
end
return M

View file

@ -9,13 +9,7 @@ describe('preview', function()
end)
describe('config', function()
it('accepts nil config', function()
assert.has_no.errors(function()
preview.get_config()
end)
end)
it('applies default values', function()
it('returns defaults before setup is called', function()
local config = preview.get_config()
assert.is_false(config.debug)
assert.are.same({}, config.providers)
@ -28,26 +22,44 @@ describe('preview', function()
assert.are.same({}, config.providers)
end)
it('accepts full provider config', function()
it('accepts full provider config via hash entry', function()
helpers.reset_config({
providers = {
typst = {
cmd = { 'typst', 'compile' },
args = { '%s' },
},
typst = {
cmd = { 'typst', 'compile' },
args = { '%s' },
},
})
local config = require('preview').get_config()
assert.is_not_nil(config.providers.typst)
end)
it('resolves array preset names to provider configs', function()
helpers.reset_config({ 'typst', 'markdown' })
local config = require('preview').get_config()
local presets = require('preview.presets')
assert.are.same(presets.typst, config.providers.typst)
assert.are.same(presets.markdown, config.providers.markdown)
end)
it('resolves latex preset under tex filetype', function()
helpers.reset_config({ 'latex' })
local config = require('preview').get_config()
local presets = require('preview.presets')
assert.are.same(presets.latex, config.providers.tex)
end)
it('resolves github preset under markdown filetype', function()
helpers.reset_config({ 'github' })
local config = require('preview').get_config()
local presets = require('preview.presets')
assert.are.same(presets.github, config.providers.markdown)
end)
end)
describe('resolve_provider', function()
before_each(function()
helpers.reset_config({
providers = {
typst = { cmd = { 'typst', 'compile' } },
},
typst = { cmd = { 'typst', 'compile' } },
})
preview = require('preview')
end)

View file

@ -13,6 +13,10 @@ describe('presets', function()
}
describe('typst', function()
it('has ft', function()
assert.are.equal('typst', presets.typst.ft)
end)
it('has cmd', function()
assert.are.same({ 'typst', 'compile' }, presets.typst.cmd)
end)
@ -28,6 +32,10 @@ describe('presets', function()
assert.is_string(output)
assert.are.equal('/tmp/document.pdf', output)
end)
it('has open enabled', function()
assert.are.same({ 'xdg-open' }, presets.typst.open)
end)
end)
describe('latex', function()
@ -38,6 +46,10 @@ describe('presets', function()
ft = 'tex',
}
it('has ft', function()
assert.are.equal('tex', presets.latex.ft)
end)
it('has cmd', function()
assert.are.same({ 'latexmk' }, presets.latex.cmd)
end)
@ -59,6 +71,10 @@ describe('presets', function()
assert.is_table(clean)
assert.are.same({ 'latexmk', '-c', '/tmp/document.tex' }, clean)
end)
it('has open enabled', function()
assert.are.same({ 'xdg-open' }, presets.latex.open)
end)
end)
describe('markdown', function()
@ -69,20 +85,84 @@ describe('presets', function()
ft = 'markdown',
}
it('has ft', function()
assert.are.equal('markdown', presets.markdown.ft)
end)
it('has cmd', function()
assert.are.same({ 'pandoc' }, presets.markdown.cmd)
end)
it('returns args with file and output flag', function()
it('returns args with standalone and embed-resources flags', function()
local args = presets.markdown.args(md_ctx)
assert.is_table(args)
assert.are.same({ '/tmp/document.md', '-o', '/tmp/document.pdf' }, args)
assert.are.same(
{ '/tmp/document.md', '-s', '--embed-resources', '-o', '/tmp/document.html' },
args
)
end)
it('returns pdf output path', function()
it('returns html output path', function()
local output = presets.markdown.output(md_ctx)
assert.is_string(output)
assert.are.equal('/tmp/document.pdf', output)
assert.are.equal('/tmp/document.html', output)
end)
it('returns clean command', function()
local clean = presets.markdown.clean(md_ctx)
assert.is_table(clean)
assert.are.same({ 'rm', '-f', '/tmp/document.html' }, clean)
end)
it('has open enabled', function()
assert.are.same({ 'xdg-open' }, presets.markdown.open)
end)
end)
describe('github', function()
local md_ctx = {
bufnr = 1,
file = '/tmp/document.md',
root = '/tmp',
ft = 'markdown',
}
it('has ft', function()
assert.are.equal('markdown', presets.github.ft)
end)
it('has cmd', function()
assert.are.same({ 'pandoc' }, presets.github.cmd)
end)
it('returns args with standalone, embed-resources, and css flags', function()
local args = presets.github.args(md_ctx)
assert.is_table(args)
assert.are.same({
'/tmp/document.md',
'-s',
'--embed-resources',
'--css',
'https://cdn.jsdelivr.net/gh/pixelbrackets/gfm-stylesheet@master/github.css',
'-o',
'/tmp/document.html',
}, args)
end)
it('returns html output path', function()
local output = presets.github.output(md_ctx)
assert.is_string(output)
assert.are.equal('/tmp/document.html', output)
end)
it('returns clean command', function()
local clean = presets.github.clean(md_ctx)
assert.is_table(clean)
assert.are.same({ 'rm', '-f', '/tmp/document.html' }, clean)
end)
it('has open enabled', function()
assert.are.same({ 'xdg-open' }, presets.github.open)
end)
end)
end)