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:
parent
c62c930454
commit
673573044f
12 changed files with 346 additions and 176 deletions
|
|
@ -1,6 +1,6 @@
|
|||
local M = {}
|
||||
|
||||
local subcommands = { 'compile', 'stop', 'clean', 'watch', 'status' }
|
||||
local subcommands = { 'compile', 'stop', 'clean', 'toggle', 'status' }
|
||||
|
||||
---@param args string
|
||||
local function dispatch(args)
|
||||
|
|
@ -12,8 +12,8 @@ local function dispatch(args)
|
|||
require('preview').stop()
|
||||
elseif subcmd == 'clean' then
|
||||
require('preview').clean()
|
||||
elseif subcmd == 'watch' then
|
||||
require('preview').watch()
|
||||
elseif subcmd == 'toggle' then
|
||||
require('preview').toggle()
|
||||
elseif subcmd == 'status' then
|
||||
local s = require('preview').status()
|
||||
local parts = {}
|
||||
|
|
@ -47,7 +47,7 @@ function M.setup()
|
|||
complete = function(lead)
|
||||
return complete(lead)
|
||||
end,
|
||||
desc = 'Compile, stop, clean, watch, or check status of document preview',
|
||||
desc = 'Compile, stop, clean, toggle, or check status of document preview',
|
||||
})
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -9,6 +9,9 @@ local active = {}
|
|||
---@type table<integer, integer>
|
||||
local watching = {}
|
||||
|
||||
---@type table<integer, true>
|
||||
local opened = {}
|
||||
|
||||
---@param val string[]|fun(ctx: preview.Context): string[]
|
||||
---@param ctx preview.Context
|
||||
---@return string[]
|
||||
|
|
@ -68,6 +71,9 @@ function M.compile(bufnr, name, provider, ctx)
|
|||
},
|
||||
vim.schedule_wrap(function(result)
|
||||
active[bufnr] = nil
|
||||
if not vim.api.nvim_buf_is_valid(bufnr) then
|
||||
return
|
||||
end
|
||||
|
||||
if result.code == 0 then
|
||||
log.dbg('compilation succeeded for buffer %d', bufnr)
|
||||
|
|
@ -76,6 +82,16 @@ function M.compile(bufnr, name, provider, ctx)
|
|||
pattern = 'PreviewCompileSuccess',
|
||||
data = { bufnr = bufnr, provider = name, output = output_file },
|
||||
})
|
||||
if provider.open and not opened[bufnr] and output_file ~= '' then
|
||||
if provider.open == true then
|
||||
vim.ui.open(output_file)
|
||||
elseif type(provider.open) == 'table' then
|
||||
local open_cmd = vim.list_extend({}, provider.open)
|
||||
table.insert(open_cmd, output_file)
|
||||
vim.system(open_cmd)
|
||||
end
|
||||
opened[bufnr] = true
|
||||
end
|
||||
else
|
||||
log.dbg('compilation failed for buffer %d (exit code %d)', bufnr, result.code)
|
||||
if provider.error_parser then
|
||||
|
|
@ -146,7 +162,7 @@ end
|
|||
---@param name string
|
||||
---@param provider preview.ProviderConfig
|
||||
---@param ctx_builder fun(bufnr: integer): preview.Context
|
||||
function M.watch(bufnr, name, provider, ctx_builder)
|
||||
function M.toggle(bufnr, name, provider, ctx_builder)
|
||||
if watching[bufnr] then
|
||||
M.unwatch(bufnr)
|
||||
return
|
||||
|
|
@ -168,6 +184,7 @@ function M.watch(bufnr, name, provider, ctx_builder)
|
|||
once = true,
|
||||
callback = function()
|
||||
M.unwatch(bufnr)
|
||||
opened[bufnr] = nil
|
||||
end,
|
||||
})
|
||||
|
||||
|
|
@ -175,6 +192,8 @@ function M.watch(bufnr, name, provider, ctx_builder)
|
|||
pattern = 'PreviewWatchStarted',
|
||||
data = { bufnr = bufnr, provider = name },
|
||||
})
|
||||
|
||||
M.compile(bufnr, name, provider, ctx_builder(bufnr))
|
||||
end
|
||||
|
||||
---@param bufnr integer
|
||||
|
|
@ -244,6 +263,7 @@ end
|
|||
M._test = {
|
||||
active = active,
|
||||
watching = watching,
|
||||
opened = opened,
|
||||
}
|
||||
|
||||
return M
|
||||
|
|
|
|||
|
|
@ -25,6 +25,14 @@ function M.check()
|
|||
else
|
||||
vim.health.error('filetype "' .. ft .. '": ' .. bin .. ' not found')
|
||||
end
|
||||
if type(provider.open) == 'table' then
|
||||
local opener = provider.open[1]
|
||||
if vim.fn.executable(opener) == 1 then
|
||||
vim.health.ok('filetype "' .. ft .. '": opener ' .. opener .. ' found')
|
||||
else
|
||||
vim.health.error('filetype "' .. ft .. '": opener ' .. opener .. ' not found')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
---@class preview.ProviderConfig
|
||||
---@field ft? string
|
||||
---@field cmd string[]
|
||||
---@field args? string[]|fun(ctx: preview.Context): string[]
|
||||
---@field cwd? string|fun(ctx: preview.Context): string
|
||||
|
|
@ -6,6 +7,7 @@
|
|||
---@field output? string|fun(ctx: preview.Context): string
|
||||
---@field error_parser? fun(stderr: string, ctx: preview.Context): preview.Diagnostic[]
|
||||
---@field clean? string[]|fun(ctx: preview.Context): string[]
|
||||
---@field open? boolean|string[]
|
||||
|
||||
---@class preview.Config
|
||||
---@field debug boolean|string
|
||||
|
|
@ -32,10 +34,11 @@
|
|||
---@field output_file string
|
||||
|
||||
---@class preview
|
||||
---@field setup fun(opts?: table)
|
||||
---@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 status fun(bufnr?: integer): preview.Status
|
||||
---@field get_config fun(): preview.Config
|
||||
local M = {}
|
||||
|
|
@ -52,39 +55,48 @@ local default_config = {
|
|||
---@type preview.Config
|
||||
local config = vim.deepcopy(default_config)
|
||||
|
||||
local initialized = false
|
||||
---@param opts? table
|
||||
function M.setup(opts)
|
||||
opts = opts or {}
|
||||
vim.validate('preview.setup opts', opts, 'table')
|
||||
|
||||
local function init()
|
||||
if initialized then
|
||||
return
|
||||
end
|
||||
initialized = true
|
||||
local presets = require('preview.presets')
|
||||
local providers = {}
|
||||
local debug = false
|
||||
|
||||
local opts = vim.g.preview or {}
|
||||
|
||||
vim.validate('preview config', opts, 'table')
|
||||
if opts.debug ~= nil then
|
||||
vim.validate('preview config.debug', opts.debug, { 'boolean', 'string' })
|
||||
end
|
||||
if opts.providers ~= nil then
|
||||
vim.validate('preview config.providers', opts.providers, 'table')
|
||||
for k, v in pairs(opts) do
|
||||
if k == 'debug' then
|
||||
vim.validate('preview.setup opts.debug', v, { 'boolean', 'string' })
|
||||
debug = v
|
||||
elseif type(k) == 'number' then
|
||||
vim.validate('preview.setup preset name', v, 'string')
|
||||
local preset = presets[v]
|
||||
if preset then
|
||||
providers[preset.ft] = preset
|
||||
end
|
||||
else
|
||||
vim.validate('preview.setup provider config', v, 'table')
|
||||
providers[k] = v
|
||||
end
|
||||
end
|
||||
|
||||
config = vim.tbl_deep_extend('force', default_config, opts)
|
||||
config = vim.tbl_deep_extend('force', default_config, {
|
||||
debug = debug,
|
||||
providers = providers,
|
||||
})
|
||||
|
||||
log.set_enabled(config.debug)
|
||||
log.dbg('initialized with %d providers', vim.tbl_count(config.providers))
|
||||
end
|
||||
|
||||
---@return preview.Config
|
||||
function M.get_config()
|
||||
init()
|
||||
return config
|
||||
end
|
||||
|
||||
---@param bufnr? integer
|
||||
---@return string?
|
||||
function M.resolve_provider(bufnr)
|
||||
init()
|
||||
bufnr = bufnr or vim.api.nvim_get_current_buf()
|
||||
local ft = vim.bo[bufnr].filetype
|
||||
if not config.providers[ft] then
|
||||
|
|
@ -97,7 +109,6 @@ end
|
|||
---@param bufnr? integer
|
||||
---@return preview.Context
|
||||
function M.build_context(bufnr)
|
||||
init()
|
||||
bufnr = bufnr or vim.api.nvim_get_current_buf()
|
||||
local file = vim.api.nvim_buf_get_name(bufnr)
|
||||
local root = vim.fs.root(bufnr, { '.git' }) or vim.fn.fnamemodify(file, ':h')
|
||||
|
|
@ -111,42 +122,38 @@ end
|
|||
|
||||
---@param bufnr? integer
|
||||
function M.compile(bufnr)
|
||||
init()
|
||||
bufnr = bufnr or vim.api.nvim_get_current_buf()
|
||||
local name = M.resolve_provider(bufnr)
|
||||
if not name then
|
||||
vim.notify('[preview.nvim] no provider configured for this filetype', vim.log.levels.WARN)
|
||||
return
|
||||
end
|
||||
local provider = config.providers[name]
|
||||
local ctx = M.build_context(bufnr)
|
||||
local provider = config.providers[name]
|
||||
compiler.compile(bufnr, name, provider, ctx)
|
||||
end
|
||||
|
||||
---@param bufnr? integer
|
||||
function M.stop(bufnr)
|
||||
init()
|
||||
bufnr = bufnr or vim.api.nvim_get_current_buf()
|
||||
compiler.stop(bufnr)
|
||||
end
|
||||
|
||||
---@param bufnr? integer
|
||||
function M.clean(bufnr)
|
||||
init()
|
||||
bufnr = bufnr or vim.api.nvim_get_current_buf()
|
||||
local name = M.resolve_provider(bufnr)
|
||||
if not name then
|
||||
vim.notify('[preview.nvim] no provider configured for this filetype', vim.log.levels.WARN)
|
||||
return
|
||||
end
|
||||
local provider = config.providers[name]
|
||||
local ctx = M.build_context(bufnr)
|
||||
local provider = config.providers[name]
|
||||
compiler.clean(bufnr, name, provider, ctx)
|
||||
end
|
||||
|
||||
---@param bufnr? integer
|
||||
function M.watch(bufnr)
|
||||
init()
|
||||
function M.toggle(bufnr)
|
||||
bufnr = bufnr or vim.api.nvim_get_current_buf()
|
||||
local name = M.resolve_provider(bufnr)
|
||||
if not name then
|
||||
|
|
@ -154,7 +161,7 @@ function M.watch(bufnr)
|
|||
return
|
||||
end
|
||||
local provider = config.providers[name]
|
||||
compiler.watch(bufnr, name, provider, M.build_context)
|
||||
compiler.toggle(bufnr, name, provider, M.build_context)
|
||||
end
|
||||
|
||||
---@class preview.Status
|
||||
|
|
@ -166,7 +173,6 @@ end
|
|||
---@param bufnr? integer
|
||||
---@return preview.Status
|
||||
function M.status(bufnr)
|
||||
init()
|
||||
bufnr = bufnr or vim.api.nvim_get_current_buf()
|
||||
return compiler.status(bufnr)
|
||||
end
|
||||
|
|
@ -174,7 +180,6 @@ end
|
|||
M._test = {
|
||||
---@diagnostic disable-next-line: assign-type-mismatch
|
||||
reset = function()
|
||||
initialized = false
|
||||
config = vim.deepcopy(default_config)
|
||||
end,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ local M = {}
|
|||
|
||||
---@type preview.ProviderConfig
|
||||
M.typst = {
|
||||
ft = 'typst',
|
||||
cmd = { 'typst', 'compile' },
|
||||
args = function(ctx)
|
||||
return { ctx.file }
|
||||
|
|
@ -9,10 +10,12 @@ M.typst = {
|
|||
output = function(ctx)
|
||||
return ctx.file:gsub('%.typ$', '.pdf')
|
||||
end,
|
||||
open = { 'xdg-open' },
|
||||
}
|
||||
|
||||
---@type preview.ProviderConfig
|
||||
M.latex = {
|
||||
ft = 'tex',
|
||||
cmd = { 'latexmk' },
|
||||
args = function(ctx)
|
||||
return { '-pdf', '-interaction=nonstopmode', ctx.file }
|
||||
|
|
@ -23,18 +26,49 @@ M.latex = {
|
|||
clean = function(ctx)
|
||||
return { 'latexmk', '-c', ctx.file }
|
||||
end,
|
||||
open = { 'xdg-open' },
|
||||
}
|
||||
|
||||
---@type preview.ProviderConfig
|
||||
M.markdown = {
|
||||
ft = 'markdown',
|
||||
cmd = { 'pandoc' },
|
||||
args = function(ctx)
|
||||
local output = ctx.file:gsub('%.md$', '.pdf')
|
||||
return { ctx.file, '-o', output }
|
||||
local output = ctx.file:gsub('%.md$', '.html')
|
||||
return { ctx.file, '-s', '--embed-resources', '-o', output }
|
||||
end,
|
||||
output = function(ctx)
|
||||
return ctx.file:gsub('%.md$', '.pdf')
|
||||
return ctx.file:gsub('%.md$', '.html')
|
||||
end,
|
||||
clean = function(ctx)
|
||||
return { 'rm', '-f', (ctx.file:gsub('%.md$', '.html')) }
|
||||
end,
|
||||
open = { 'xdg-open' },
|
||||
}
|
||||
|
||||
---@type preview.ProviderConfig
|
||||
M.github = {
|
||||
ft = 'markdown',
|
||||
cmd = { 'pandoc' },
|
||||
args = function(ctx)
|
||||
local output = ctx.file:gsub('%.md$', '.html')
|
||||
return {
|
||||
ctx.file,
|
||||
'-s',
|
||||
'--embed-resources',
|
||||
'--css',
|
||||
'https://cdn.jsdelivr.net/gh/pixelbrackets/gfm-stylesheet@master/github.css',
|
||||
'-o',
|
||||
output,
|
||||
}
|
||||
end,
|
||||
output = function(ctx)
|
||||
return ctx.file:gsub('%.md$', '.html')
|
||||
end,
|
||||
clean = function(ctx)
|
||||
return { 'rm', '-f', (ctx.file:gsub('%.md$', '.html')) }
|
||||
end,
|
||||
open = { 'xdg-open' },
|
||||
}
|
||||
|
||||
return M
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue