From 0b16ff7178e41ed5e389769d8704061ec2ede7d7 Mon Sep 17 00:00:00 2001 From: Barrett Ruth <62671086+barrettruth@users.noreply.github.com> Date: Tue, 3 Mar 2026 00:49:10 -0500 Subject: [PATCH] Refactor/preset name true syntax (#4) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * refactor(config): replace array preset syntax with preset_name = true Problem: setup() mixed array entries (preset names) and hash entries (custom providers keyed by filetype), requiring verbose vim.tbl_deep_extend boilerplate to override presets. Solution: unify under a single key=value model. Keys are preset names or filetypes; true registers the preset as-is, a table deep-merges with the matching preset (or registers a custom provider if no preset matches), and false is a no-op. Array entries are dropped. Also adds -f gfm to presets.github args so pandoc parses input as GFM. * ci: format * fix(presets): parenthesize gsub output to suppress redundant-return-value * ci: remove superfluous things * refactor: remove PreviewWatch* events and clean up docs Problem: PreviewWatchStarted/PreviewWatchStopped were redundant with the status() API, and the doc had a wrong author, stale INSTALLATION format, and "watch mode" language left over from the watch → toggle rename. Solution: Remove the events and their tests. Fix the doc author, rename INSTALLATION → SETUP to match sibling plugins, replace "watch mode" with "auto-compile" throughout, and drop the events from EVENTS. --- README.md | 2 -- doc/preview.nvim.txt | 27 +++++++-------------- lua/preview/compiler.lua | 10 -------- spec/compiler_spec.lua | 51 ---------------------------------------- 4 files changed, 8 insertions(+), 82 deletions(-) diff --git a/README.md b/README.md index 3e85634..da5f3cf 100644 --- a/README.md +++ b/README.md @@ -71,5 +71,3 @@ require('preview').setup({ typst = { open = { 'sioyek', '--new-instance' } }, }) ``` - -See `:h preview.nvim` for more information. diff --git a/doc/preview.nvim.txt b/doc/preview.nvim.txt index 753fefc..0b44a91 100644 --- a/doc/preview.nvim.txt +++ b/doc/preview.nvim.txt @@ -1,6 +1,6 @@ *preview.nvim.txt* Async document compilation for Neovim -Author: Raphael +Author: Barrett Ruth License: MIT ============================================================================== @@ -20,19 +20,14 @@ REQUIREMENTS *preview.nvim-requirements - A compiler binary for each configured provider (e.g. `typst`, `latexmk`) ============================================================================== -INSTALLATION *preview.nvim-installation* +SETUP *preview.nvim-setup* -With luarocks (recommended): -> - :Rocks install preview.nvim -< - -With lazy.nvim: ->lua +Load preview.nvim with your package manager. For example, with lazy.nvim: >lua { 'barrettruth/preview.nvim', } < +Call |preview.setup()| to configure providers before use. ============================================================================== CONFIGURATION *preview.nvim-configuration* @@ -161,7 +156,7 @@ COMMANDS *preview.nvim-commands* `stop` Kill active compilation for the current buffer. `clean` Run the provider's clean command. `toggle` Toggle auto-compile on save for the current buffer. - `status` Echo compilation status (idle, compiling, watching). + `status` Echo compilation status (idle, compiling, toggled). ============================================================================== API *preview.nvim-api* @@ -176,9 +171,9 @@ preview.clean({bufnr?}) *preview.clean()* Run the provider's clean command for the buffer. preview.toggle({bufnr?}) *preview.toggle()* - Toggle watch mode for the buffer. When enabled, the buffer is + Toggle auto-compile for the buffer. When enabled, the buffer is immediately compiled and automatically recompiled on each save - (`BufWritePost`). Call again to stop watching. + (`BufWritePost`). Call again to stop. preview.status({bufnr?}) *preview.status()* Returns a |preview.Status| table. @@ -187,7 +182,7 @@ preview.status({bufnr?}) *preview.status()* Status fields:~ `compiling` boolean Whether compilation is active. - `watching` boolean Whether watch mode is active. + `watching` boolean Whether auto-compile is active. `provider` string? Name of the active provider. `output_file` string? Path to the output file. @@ -208,12 +203,6 @@ preview.nvim fires User autocmds with structured data: `PreviewCompileFailed` Compilation failed (non-zero exit). data: `{ bufnr, provider, code, stderr }` -`PreviewWatchStarted` Watch mode enabled for a buffer. - data: `{ bufnr, provider }` - -`PreviewWatchStopped` Watch mode disabled for a buffer. - data: `{ bufnr }` - Example:~ >lua vim.api.nvim_create_autocmd('User', { diff --git a/lua/preview/compiler.lua b/lua/preview/compiler.lua index 6907524..b0c7402 100644 --- a/lua/preview/compiler.lua +++ b/lua/preview/compiler.lua @@ -188,11 +188,6 @@ function M.toggle(bufnr, name, provider, ctx_builder) end, }) - vim.api.nvim_exec_autocmds('User', { - pattern = 'PreviewWatchStarted', - data = { bufnr = bufnr, provider = name }, - }) - M.compile(bufnr, name, provider, ctx_builder(bufnr)) end @@ -205,11 +200,6 @@ function M.unwatch(bufnr) vim.api.nvim_del_autocmd(au_id) watching[bufnr] = nil log.dbg('unwatched buffer %d', bufnr) - - vim.api.nvim_exec_autocmds('User', { - pattern = 'PreviewWatchStopped', - data = { bufnr = bufnr }, - }) end ---@param bufnr integer diff --git a/spec/compiler_spec.lua b/spec/compiler_spec.lua index 7360ceb..6802dee 100644 --- a/spec/compiler_spec.lua +++ b/spec/compiler_spec.lua @@ -190,31 +190,6 @@ describe('compiler', function() helpers.delete_buffer(bufnr) end) - it('fires PreviewWatchStarted event', function() - local bufnr = helpers.create_buffer({ 'hello' }, 'text') - vim.api.nvim_buf_set_name(bufnr, '/tmp/preview_test_watch_event.txt') - - local fired = false - vim.api.nvim_create_autocmd('User', { - pattern = 'PreviewWatchStarted', - once = true, - callback = function() - fired = true - end, - }) - - local provider = { cmd = { 'echo', 'ok' } } - local ctx_builder = function(b) - return { bufnr = b, file = '/tmp/preview_test_watch_event.txt', root = '/tmp', ft = 'text' } - end - - compiler.toggle(bufnr, 'echo', provider, ctx_builder) - assert.is_true(fired) - - compiler.unwatch(bufnr) - helpers.delete_buffer(bufnr) - end) - it('toggles off when called again', function() local bufnr = helpers.create_buffer({ 'hello' }, 'text') vim.api.nvim_buf_set_name(bufnr, '/tmp/preview_test_watch_toggle.txt') @@ -233,32 +208,6 @@ describe('compiler', function() helpers.delete_buffer(bufnr) end) - it('fires PreviewWatchStopped on unwatch', function() - local bufnr = helpers.create_buffer({ 'hello' }, 'text') - vim.api.nvim_buf_set_name(bufnr, '/tmp/preview_test_watch_stop.txt') - - local stopped = false - vim.api.nvim_create_autocmd('User', { - pattern = 'PreviewWatchStopped', - once = true, - callback = function() - stopped = true - end, - }) - - local provider = { cmd = { 'echo', 'ok' } } - local ctx_builder = function(b) - return { bufnr = b, file = '/tmp/preview_test_watch_stop.txt', root = '/tmp', ft = 'text' } - end - - compiler.toggle(bufnr, 'echo', provider, ctx_builder) - compiler.unwatch(bufnr) - assert.is_true(stopped) - assert.is_nil(compiler._test.watching[bufnr]) - - helpers.delete_buffer(bufnr) - end) - it('stop_all clears watches', function() local bufnr = helpers.create_buffer({ 'hello' }, 'text') vim.api.nvim_buf_set_name(bufnr, '/tmp/preview_test_watch_stopall.txt')