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:
Barrett Ruth 2026-03-04 13:16:01 -05:00 committed by GitHub
parent 4732696a62
commit 75b855438a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 47 additions and 38 deletions

View file

@ -1,17 +1,14 @@
local M = {}
local handlers = {
build = function()
require('preview').build()
end,
stop = function()
require('preview').stop()
compile = function()
require('preview').compile()
end,
clean = function()
require('preview').clean()
end,
watch = function()
require('preview').watch()
toggle = function()
require('preview').toggle()
end,
open = function()
require('preview').open()
@ -33,7 +30,7 @@ local handlers = {
---@param args string
local function dispatch(args)
local subcmd = args ~= '' and args or 'build'
local subcmd = args ~= '' and args or 'toggle'
local handler = handlers[subcmd]
if handler then
handler()
@ -58,7 +55,7 @@ function M.setup()
complete = function(lead)
return complete(lead)
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

View file

@ -39,10 +39,10 @@
---@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
@ -144,7 +144,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 +176,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