refactor: simplify command surface (#28)
* refactor: rename build to compile and watch to toggle in public API
Problem: the code used build/watch while the help file already
documented compile/toggle, creating a confusing mismatch.
Solution: rename M.build() to M.compile() and M.watch() to M.toggle()
in init.lua, update handler keys in commands.lua, and update the test
file to match.
* refactor(commands): make toggle the default subcommand
Problem: bare :Preview ran a one-shot compile, but users reaching for a
"preview" plugin expect it to start previewing (i.e. watch mode).
Solution: change the fallback subcommand from compile to toggle so
:Preview starts/stops auto-compile on save.
* refactor(commands): remove stop subcommand
Problem: :Preview stop had a subtle distinction from toggle-off (kill
process but keep autocmd) that nobody reaches for deliberately from
the command line.
Solution: remove stop from the command dispatch table. The Lua API
require('preview').stop() remains as a programmatic escape hatch.
* docs: update help file for new command surface and document reload
Problem: the help file listed compile as the default subcommand, still
included the stop subcommand, omitted the reload provider field, and
had a misleading claim about shipping with zero defaults.
Solution: make toggle the default in the commands section, remove stop
from subcommands, add reload to provider fields, fix the introduction
text, reorder API entries to match new primacy, and add an output path
override example addressing #26/#27.
This commit is contained in:
parent
4732696a62
commit
75b855438a
4 changed files with 47 additions and 38 deletions
|
|
@ -10,8 +10,8 @@ preview.nvim is an extensible framework for compiling documents asynchronously
|
||||||
in Neovim. It provides a unified interface for any compilation workflow —
|
in Neovim. It provides a unified interface for any compilation workflow —
|
||||||
LaTeX, Typst, Markdown, or anything else with a CLI compiler.
|
LaTeX, Typst, Markdown, or anything else with a CLI compiler.
|
||||||
|
|
||||||
The plugin ships with zero provider defaults. Users must explicitly configure
|
The plugin ships with opt-in presets for common tools (Typst, LaTeX, Pandoc)
|
||||||
their compiler commands. preview.nvim is purely an orchestration framework.
|
and supports fully custom providers. See |preview.nvim-presets|.
|
||||||
|
|
||||||
==============================================================================
|
==============================================================================
|
||||||
REQUIREMENTS *preview.nvim-requirements*
|
REQUIREMENTS *preview.nvim-requirements*
|
||||||
|
|
@ -93,6 +93,14 @@ Provider fields:~
|
||||||
|vim.ui.open()|. A string[] is run as
|
|vim.ui.open()|. A string[] is run as
|
||||||
a command with the output path appended.
|
a command with the output path appended.
|
||||||
|
|
||||||
|
`reload` boolean|string[]|function
|
||||||
|
Reload the output after recompilation.
|
||||||
|
`true` uses a built-in SSE server for
|
||||||
|
HTML files. A string[] is run as a
|
||||||
|
command. If a function, receives a
|
||||||
|
|preview.Context| and returns a
|
||||||
|
string[].
|
||||||
|
|
||||||
*preview.Context*
|
*preview.Context*
|
||||||
Context fields:~
|
Context fields:~
|
||||||
|
|
||||||
|
|
@ -115,6 +123,17 @@ Example overriding a preset field:~
|
||||||
})
|
})
|
||||||
<
|
<
|
||||||
|
|
||||||
|
Example overriding the output path (e.g. latexmk `$out_dir`):~
|
||||||
|
>lua
|
||||||
|
require('preview').setup({
|
||||||
|
latex = {
|
||||||
|
output = function(ctx)
|
||||||
|
return 'build/' .. vim.fn.fnamemodify(ctx.file, ':t:r') .. '.pdf'
|
||||||
|
end,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
<
|
||||||
|
|
||||||
Example with a fully custom provider (key is not a preset name):~
|
Example with a fully custom provider (key is not a preset name):~
|
||||||
>lua
|
>lua
|
||||||
require('preview').setup({
|
require('preview').setup({
|
||||||
|
|
@ -160,30 +179,30 @@ COMMANDS *preview.nvim-commands*
|
||||||
|
|
||||||
Subcommands:~
|
Subcommands:~
|
||||||
|
|
||||||
`compile` Compile the current buffer (default if omitted).
|
`toggle` Toggle auto-compile on save (default if omitted).
|
||||||
`stop` Kill active compilation for the current buffer.
|
`compile` One-shot compile of the current buffer.
|
||||||
`clean` Run the provider's clean command.
|
`clean` Run the provider's clean command.
|
||||||
`toggle` Toggle auto-compile on save for the current buffer.
|
|
||||||
`open` Open the last compiled output without recompiling.
|
`open` Open the last compiled output without recompiling.
|
||||||
`status` Echo compilation status (idle, compiling, watching).
|
`status` Echo compilation status (idle, compiling, watching).
|
||||||
|
|
||||||
==============================================================================
|
==============================================================================
|
||||||
API *preview.nvim-api*
|
API *preview.nvim-api*
|
||||||
|
|
||||||
preview.compile({bufnr?}) *preview.compile()*
|
|
||||||
Compile the document in the given buffer (default: current).
|
|
||||||
|
|
||||||
preview.stop({bufnr?}) *preview.stop()*
|
|
||||||
Kill the active compilation process for the buffer.
|
|
||||||
|
|
||||||
preview.clean({bufnr?}) *preview.clean()*
|
|
||||||
Run the provider's clean command for the buffer.
|
|
||||||
|
|
||||||
preview.toggle({bufnr?}) *preview.toggle()*
|
preview.toggle({bufnr?}) *preview.toggle()*
|
||||||
Toggle auto-compile 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
|
immediately compiled and automatically recompiled on each save
|
||||||
(`BufWritePost`). Call again to stop.
|
(`BufWritePost`). Call again to stop.
|
||||||
|
|
||||||
|
preview.compile({bufnr?}) *preview.compile()*
|
||||||
|
One-shot compile the document in the given buffer (default: current).
|
||||||
|
|
||||||
|
preview.stop({bufnr?}) *preview.stop()*
|
||||||
|
Kill the active compilation process for the buffer. Programmatic
|
||||||
|
escape hatch — not exposed as a subcommand.
|
||||||
|
|
||||||
|
preview.clean({bufnr?}) *preview.clean()*
|
||||||
|
Run the provider's clean command for the buffer.
|
||||||
|
|
||||||
preview.open({bufnr?}) *preview.open()*
|
preview.open({bufnr?}) *preview.open()*
|
||||||
Open the last compiled output for the buffer without recompiling.
|
Open the last compiled output for the buffer without recompiling.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,14 @@
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
local handlers = {
|
local handlers = {
|
||||||
build = function()
|
compile = function()
|
||||||
require('preview').build()
|
require('preview').compile()
|
||||||
end,
|
|
||||||
stop = function()
|
|
||||||
require('preview').stop()
|
|
||||||
end,
|
end,
|
||||||
clean = function()
|
clean = function()
|
||||||
require('preview').clean()
|
require('preview').clean()
|
||||||
end,
|
end,
|
||||||
watch = function()
|
toggle = function()
|
||||||
require('preview').watch()
|
require('preview').toggle()
|
||||||
end,
|
end,
|
||||||
open = function()
|
open = function()
|
||||||
require('preview').open()
|
require('preview').open()
|
||||||
|
|
@ -33,7 +30,7 @@ local handlers = {
|
||||||
|
|
||||||
---@param args string
|
---@param args string
|
||||||
local function dispatch(args)
|
local function dispatch(args)
|
||||||
local subcmd = args ~= '' and args or 'build'
|
local subcmd = args ~= '' and args or 'toggle'
|
||||||
local handler = handlers[subcmd]
|
local handler = handlers[subcmd]
|
||||||
if handler then
|
if handler then
|
||||||
handler()
|
handler()
|
||||||
|
|
@ -58,7 +55,7 @@ function M.setup()
|
||||||
complete = function(lead)
|
complete = function(lead)
|
||||||
return complete(lead)
|
return complete(lead)
|
||||||
end,
|
end,
|
||||||
desc = 'Build, stop, clean, watch, open, or check status of document preview',
|
desc = 'Toggle, compile, clean, open, or check status of document preview',
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -39,10 +39,10 @@
|
||||||
|
|
||||||
---@class preview
|
---@class preview
|
||||||
---@field setup fun(opts?: table)
|
---@field setup fun(opts?: table)
|
||||||
---@field build fun(bufnr?: integer)
|
---@field compile fun(bufnr?: integer)
|
||||||
---@field stop fun(bufnr?: integer)
|
---@field stop fun(bufnr?: integer)
|
||||||
---@field clean fun(bufnr?: integer)
|
---@field clean fun(bufnr?: integer)
|
||||||
---@field watch fun(bufnr?: integer)
|
---@field toggle fun(bufnr?: integer)
|
||||||
---@field open fun(bufnr?: integer)
|
---@field open fun(bufnr?: integer)
|
||||||
---@field status fun(bufnr?: integer): preview.Status
|
---@field status fun(bufnr?: integer): preview.Status
|
||||||
---@field statusline fun(bufnr?: integer): string
|
---@field statusline fun(bufnr?: integer): string
|
||||||
|
|
@ -144,7 +144,7 @@ function M.build_context(bufnr)
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param bufnr? integer
|
---@param bufnr? integer
|
||||||
function M.build(bufnr)
|
function M.compile(bufnr)
|
||||||
bufnr = bufnr or vim.api.nvim_get_current_buf()
|
bufnr = bufnr or vim.api.nvim_get_current_buf()
|
||||||
local name = M.resolve_provider(bufnr)
|
local name = M.resolve_provider(bufnr)
|
||||||
if not name then
|
if not name then
|
||||||
|
|
@ -176,7 +176,7 @@ function M.clean(bufnr)
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param bufnr? integer
|
---@param bufnr? integer
|
||||||
function M.watch(bufnr)
|
function M.toggle(bufnr)
|
||||||
bufnr = bufnr or vim.api.nvim_get_current_buf()
|
bufnr = bufnr or vim.api.nvim_get_current_buf()
|
||||||
local name = M.resolve_provider(bufnr)
|
local name = M.resolve_provider(bufnr)
|
||||||
if not name then
|
if not name then
|
||||||
|
|
|
||||||
|
|
@ -14,17 +14,10 @@ describe('commands', function()
|
||||||
end)
|
end)
|
||||||
|
|
||||||
describe('dispatch', function()
|
describe('dispatch', function()
|
||||||
it('does not error on :Preview with no provider', function()
|
it('does not error on :Preview compile with no provider', function()
|
||||||
require('preview.commands').setup()
|
require('preview.commands').setup()
|
||||||
assert.has_no.errors(function()
|
assert.has_no.errors(function()
|
||||||
vim.cmd('Preview build')
|
vim.cmd('Preview compile')
|
||||||
end)
|
|
||||||
end)
|
|
||||||
|
|
||||||
it('does not error on :Preview stop', function()
|
|
||||||
require('preview.commands').setup()
|
|
||||||
assert.has_no.errors(function()
|
|
||||||
vim.cmd('Preview stop')
|
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
|
@ -42,10 +35,10 @@ describe('commands', function()
|
||||||
end)
|
end)
|
||||||
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()
|
require('preview.commands').setup()
|
||||||
assert.has_no.errors(function()
|
assert.has_no.errors(function()
|
||||||
vim.cmd('Preview watch')
|
vim.cmd('Preview toggle')
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue