feat: rename

This commit is contained in:
Barrett Ruth 2026-03-02 21:23:40 -05:00
parent e1d7abf58e
commit 942438f817
Signed by: barrett
GPG key ID: A6C96C9349D2FC81
21 changed files with 660 additions and 305 deletions

54
lua/preview/commands.lua Normal file
View file

@ -0,0 +1,54 @@
local M = {}
local subcommands = { 'compile', 'stop', 'clean', 'watch', 'status' }
---@param args string
local function dispatch(args)
local subcmd = args ~= '' and args or 'compile'
if subcmd == 'compile' then
require('preview').compile()
elseif subcmd == 'stop' then
require('preview').stop()
elseif subcmd == 'clean' then
require('preview').clean()
elseif subcmd == 'watch' then
require('preview').watch()
elseif subcmd == 'status' then
local s = require('preview').status()
local parts = {}
if s.compiling then
table.insert(parts, 'compiling with "' .. s.provider .. '"')
else
table.insert(parts, 'idle')
end
if s.watching then
table.insert(parts, 'watching')
end
vim.notify('[preview.nvim] ' .. table.concat(parts, ', '), vim.log.levels.INFO)
else
vim.notify('[preview.nvim] unknown subcommand: ' .. subcmd, vim.log.levels.ERROR)
end
end
---@param lead string
---@return string[]
local function complete(lead)
return vim.tbl_filter(function(s)
return s:find(lead, 1, true) == 1
end, subcommands)
end
function M.setup()
vim.api.nvim_create_user_command('Preview', function(opts)
dispatch(opts.args)
end, {
nargs = '?',
complete = function(lead)
return complete(lead)
end,
desc = 'Compile, stop, clean, watch, or check status of document preview',
})
end
return M

View file

@ -1,13 +1,16 @@
local M = {}
local diagnostic = require('render.diagnostic')
local log = require('render.log')
local diagnostic = require('preview.diagnostic')
local log = require('preview.log')
---@type table<integer, render.Process>
---@type table<integer, preview.Process>
local active = {}
---@param val string[]|fun(ctx: render.Context): string[]
---@param ctx render.Context
---@type table<integer, integer>
local watching = {}
---@param val string[]|fun(ctx: preview.Context): string[]
---@param ctx preview.Context
---@return string[]
local function eval_list(val, ctx)
if type(val) == 'function' then
@ -16,8 +19,8 @@ local function eval_list(val, ctx)
return val
end
---@param val string|fun(ctx: render.Context): string
---@param ctx render.Context
---@param val string|fun(ctx: preview.Context): string
---@param ctx preview.Context
---@return string
local function eval_string(val, ctx)
if type(val) == 'function' then
@ -28,8 +31,8 @@ end
---@param bufnr integer
---@param name string
---@param provider render.ProviderConfig
---@param ctx render.Context
---@param provider preview.ProviderConfig
---@param ctx preview.Context
function M.compile(bufnr, name, provider, ctx)
if vim.bo[bufnr].modified then
vim.cmd('silent! update')
@ -70,7 +73,7 @@ function M.compile(bufnr, name, provider, ctx)
log.dbg('compilation succeeded for buffer %d', bufnr)
diagnostic.clear(bufnr)
vim.api.nvim_exec_autocmds('User', {
pattern = 'RenderCompileSuccess',
pattern = 'PreviewCompileSuccess',
data = { bufnr = bufnr, provider = name, output = output_file },
})
else
@ -79,7 +82,7 @@ function M.compile(bufnr, name, provider, ctx)
diagnostic.set(bufnr, name, provider.error_parser, result.stderr or '', ctx)
end
vim.api.nvim_exec_autocmds('User', {
pattern = 'RenderCompileFailed',
pattern = 'PreviewCompileFailed',
data = {
bufnr = bufnr,
provider = name,
@ -102,7 +105,7 @@ function M.compile(bufnr, name, provider, ctx)
})
vim.api.nvim_exec_autocmds('User', {
pattern = 'RenderCompileStarted',
pattern = 'PreviewCompileStarted',
data = { bufnr = bufnr, provider = name },
})
end
@ -134,15 +137,69 @@ function M.stop_all()
for bufnr, _ in pairs(active) do
M.stop(bufnr)
end
for bufnr, _ in pairs(watching) do
M.unwatch(bufnr)
end
end
---@param bufnr integer
---@param name string
---@param provider render.ProviderConfig
---@param ctx render.Context
---@param provider preview.ProviderConfig
---@param ctx_builder fun(bufnr: integer): preview.Context
function M.watch(bufnr, name, provider, ctx_builder)
if watching[bufnr] then
M.unwatch(bufnr)
return
end
local au_id = vim.api.nvim_create_autocmd('BufWritePost', {
buffer = bufnr,
callback = function()
local ctx = ctx_builder(bufnr)
M.compile(bufnr, name, provider, ctx)
end,
})
watching[bufnr] = au_id
log.dbg('watching buffer %d with provider "%s"', bufnr, name)
vim.api.nvim_create_autocmd('BufWipeout', {
buffer = bufnr,
once = true,
callback = function()
M.unwatch(bufnr)
end,
})
vim.api.nvim_exec_autocmds('User', {
pattern = 'PreviewWatchStarted',
data = { bufnr = bufnr, provider = name },
})
end
---@param bufnr integer
function M.unwatch(bufnr)
local au_id = watching[bufnr]
if not au_id then
return
end
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
---@param name string
---@param provider preview.ProviderConfig
---@param ctx preview.Context
function M.clean(bufnr, name, provider, ctx)
if not provider.clean then
vim.notify('[render.nvim] provider "' .. name .. '" has no clean command', vim.log.levels.WARN)
vim.notify('[preview.nvim] provider "' .. name .. '" has no clean command', vim.log.levels.WARN)
return
end
@ -160,27 +217,33 @@ function M.clean(bufnr, name, provider, ctx)
vim.schedule_wrap(function(result)
if result.code == 0 then
log.dbg('clean succeeded for buffer %d', bufnr)
vim.notify('[render.nvim] clean complete', vim.log.levels.INFO)
vim.notify('[preview.nvim] clean complete', vim.log.levels.INFO)
else
log.dbg('clean failed for buffer %d (exit code %d)', bufnr, result.code)
vim.notify('[render.nvim] clean failed: ' .. (result.stderr or ''), vim.log.levels.ERROR)
vim.notify('[preview.nvim] clean failed: ' .. (result.stderr or ''), vim.log.levels.ERROR)
end
end)
)
end
---@param bufnr integer
---@return render.Status
---@return preview.Status
function M.status(bufnr)
local proc = active[bufnr]
if proc then
return { compiling = true, provider = proc.provider, output_file = proc.output_file }
return {
compiling = true,
watching = watching[bufnr] ~= nil,
provider = proc.provider,
output_file = proc.output_file,
}
end
return { compiling = false }
return { compiling = false, watching = watching[bufnr] ~= nil }
end
M._test = {
active = active,
watching = watching,
}
return M

View file

@ -1,8 +1,8 @@
local M = {}
local log = require('render.log')
local log = require('preview.log')
local ns = vim.api.nvim_create_namespace('render')
local ns = vim.api.nvim_create_namespace('preview')
---@param bufnr integer
function M.clear(bufnr)
@ -12,9 +12,9 @@ end
---@param bufnr integer
---@param name string
---@param error_parser fun(stderr: string, ctx: render.Context): render.Diagnostic[]
---@param error_parser fun(stderr: string, ctx: preview.Context): preview.Diagnostic[]
---@param stderr string
---@param ctx render.Context
---@param ctx preview.Context
function M.set(bufnr, name, error_parser, stderr, ctx)
local ok, diagnostics = pcall(error_parser, stderr, ctx)
if not ok then

31
lua/preview/health.lua Normal file
View file

@ -0,0 +1,31 @@
local M = {}
function M.check()
vim.health.start('preview.nvim')
if vim.fn.has('nvim-0.11.0') == 1 then
vim.health.ok('Neovim 0.11.0+ detected')
else
vim.health.error('preview.nvim requires Neovim 0.11.0+')
end
local config = require('preview').get_config()
local provider_count = vim.tbl_count(config.providers)
if provider_count == 0 then
vim.health.warn('no providers configured')
else
vim.health.ok(provider_count .. ' provider(s) configured')
end
for ft, provider in pairs(config.providers) do
local bin = provider.cmd[1]
if vim.fn.executable(bin) == 1 then
vim.health.ok('filetype "' .. ft .. '": ' .. bin .. ' found')
else
vim.health.error('filetype "' .. ft .. '": ' .. bin .. ' not found')
end
end
end
return M

View file

@ -1,24 +1,23 @@
---@class render.ProviderConfig
---@class preview.ProviderConfig
---@field cmd string[]
---@field args? string[]|fun(ctx: render.Context): string[]
---@field cwd? string|fun(ctx: render.Context): string
---@field args? string[]|fun(ctx: preview.Context): string[]
---@field cwd? string|fun(ctx: preview.Context): string
---@field env? table<string, string>
---@field output? string|fun(ctx: render.Context): string
---@field error_parser? fun(stderr: string, ctx: render.Context): render.Diagnostic[]
---@field clean? string[]|fun(ctx: render.Context): string[]
---@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[]
---@class render.Config
---@class preview.Config
---@field debug boolean|string
---@field providers table<string, render.ProviderConfig>
---@field providers_by_ft table<string, string>
---@field providers table<string, preview.ProviderConfig>
---@class render.Context
---@class preview.Context
---@field bufnr integer
---@field file string
---@field root string
---@field ft string
---@class render.Diagnostic
---@class preview.Diagnostic
---@field lnum integer
---@field col integer
---@field message string
@ -27,30 +26,30 @@
---@field end_col? integer
---@field source? string
---@class render.Process
---@class preview.Process
---@field obj table
---@field provider string
---@field output_file string
---@class render
---@class preview
---@field compile fun(bufnr?: integer)
---@field stop fun(bufnr?: integer)
---@field clean fun(bufnr?: integer)
---@field status fun(bufnr?: integer): render.Status
---@field get_config fun(): render.Config
---@field watch fun(bufnr?: integer)
---@field status fun(bufnr?: integer): preview.Status
---@field get_config fun(): preview.Config
local M = {}
local compiler = require('render.compiler')
local log = require('render.log')
local compiler = require('preview.compiler')
local log = require('preview.log')
---@type render.Config
---@type preview.Config
local default_config = {
debug = false,
providers = {},
providers_by_ft = {},
}
---@type render.Config
---@type preview.Config
local config = vim.deepcopy(default_config)
local initialized = false
@ -61,17 +60,14 @@ local function init()
end
initialized = true
local opts = vim.g.render or {}
local opts = vim.g.preview or {}
vim.validate('render config', opts, 'table')
vim.validate('preview config', opts, 'table')
if opts.debug ~= nil then
vim.validate('render config.debug', opts.debug, { 'boolean', 'string' })
vim.validate('preview config.debug', opts.debug, { 'boolean', 'string' })
end
if opts.providers ~= nil then
vim.validate('render config.providers', opts.providers, 'table')
end
if opts.providers_by_ft ~= nil then
vim.validate('render config.providers_by_ft', opts.providers_by_ft, 'table')
vim.validate('preview config.providers', opts.providers, 'table')
end
config = vim.tbl_deep_extend('force', default_config, opts)
@ -79,7 +75,7 @@ local function init()
log.dbg('initialized with %d providers', vim.tbl_count(config.providers))
end
---@return render.Config
---@return preview.Config
function M.get_config()
init()
return config
@ -91,20 +87,15 @@ function M.resolve_provider(bufnr)
init()
bufnr = bufnr or vim.api.nvim_get_current_buf()
local ft = vim.bo[bufnr].filetype
local name = config.providers_by_ft[ft]
if not name then
log.dbg('no provider mapped for filetype: %s', ft)
if not config.providers[ft] then
log.dbg('no provider configured for filetype: %s', ft)
return nil
end
if not config.providers[name] then
log.dbg('provider "%s" mapped for ft "%s" but not configured', name, ft)
return nil
end
return name
return ft
end
---@param bufnr? integer
---@return render.Context
---@return preview.Context
function M.build_context(bufnr)
init()
bufnr = bufnr or vim.api.nvim_get_current_buf()
@ -124,7 +115,7 @@ function M.compile(bufnr)
bufnr = bufnr or vim.api.nvim_get_current_buf()
local name = M.resolve_provider(bufnr)
if not name then
vim.notify('[render.nvim] no provider configured for this filetype', vim.log.levels.WARN)
vim.notify('[preview.nvim] no provider configured for this filetype', vim.log.levels.WARN)
return
end
local provider = config.providers[name]
@ -145,7 +136,7 @@ function M.clean(bufnr)
bufnr = bufnr or vim.api.nvim_get_current_buf()
local name = M.resolve_provider(bufnr)
if not name then
vim.notify('[render.nvim] no provider configured for this filetype', vim.log.levels.WARN)
vim.notify('[preview.nvim] no provider configured for this filetype', vim.log.levels.WARN)
return
end
local provider = config.providers[name]
@ -153,13 +144,27 @@ function M.clean(bufnr)
compiler.clean(bufnr, name, provider, ctx)
end
---@class render.Status
---@param bufnr? integer
function M.watch(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]
compiler.watch(bufnr, name, provider, M.build_context)
end
---@class preview.Status
---@field compiling boolean
---@field watching boolean
---@field provider? string
---@field output_file? string
---@param bufnr? integer
---@return render.Status
---@return preview.Status
function M.status(bufnr)
init()
bufnr = bufnr or vim.api.nvim_get_current_buf()

View file

@ -20,7 +20,7 @@ function M.dbg(msg, ...)
if not enabled then
return
end
local formatted = '[render.nvim]: ' .. string.format(msg, ...)
local formatted = '[preview.nvim]: ' .. string.format(msg, ...)
if log_file then
local f = io.open(log_file, 'a')
if f then

40
lua/preview/presets.lua Normal file
View file

@ -0,0 +1,40 @@
local M = {}
---@type preview.ProviderConfig
M.typst = {
cmd = { 'typst', 'compile' },
args = function(ctx)
return { ctx.file }
end,
output = function(ctx)
return ctx.file:gsub('%.typ$', '.pdf')
end,
}
---@type preview.ProviderConfig
M.latex = {
cmd = { 'latexmk' },
args = function(ctx)
return { '-pdf', '-interaction=nonstopmode', ctx.file }
end,
output = function(ctx)
return ctx.file:gsub('%.tex$', '.pdf')
end,
clean = function(ctx)
return { 'latexmk', '-c', ctx.file }
end,
}
---@type preview.ProviderConfig
M.markdown = {
cmd = { 'pandoc' },
args = function(ctx)
local output = ctx.file:gsub('%.md$', '.pdf')
return { ctx.file, '-o', output }
end,
output = function(ctx)
return ctx.file:gsub('%.md$', '.pdf')
end,
}
return M

View file

@ -1,47 +0,0 @@
local M = {}
local subcommands = { 'compile', 'stop', 'clean', 'status' }
---@param args string
local function dispatch(args)
local subcmd = args ~= '' and args or 'compile'
if subcmd == 'compile' then
require('render').compile()
elseif subcmd == 'stop' then
require('render').stop()
elseif subcmd == 'clean' then
require('render').clean()
elseif subcmd == 'status' then
local s = require('render').status()
if s.compiling then
vim.notify('[render.nvim] compiling with "' .. s.provider .. '"', vim.log.levels.INFO)
else
vim.notify('[render.nvim] idle', vim.log.levels.INFO)
end
else
vim.notify('[render.nvim] unknown subcommand: ' .. subcmd, vim.log.levels.ERROR)
end
end
---@param lead string
---@return string[]
local function complete(lead)
return vim.tbl_filter(function(s)
return s:find(lead, 1, true) == 1
end, subcommands)
end
function M.setup()
vim.api.nvim_create_user_command('Render', function(opts)
dispatch(opts.args)
end, {
nargs = '?',
complete = function(lead)
return complete(lead)
end,
desc = 'Compile, stop, clean, or check status of document rendering',
})
end
return M

View file

@ -1,42 +0,0 @@
local M = {}
function M.check()
vim.health.start('render.nvim')
if vim.fn.has('nvim-0.10.0') == 1 then
vim.health.ok('Neovim 0.10.0+ detected')
else
vim.health.error('render.nvim requires Neovim 0.10.0+')
end
local config = require('render').get_config()
local provider_count = vim.tbl_count(config.providers)
if provider_count == 0 then
vim.health.warn('no providers configured')
else
vim.health.ok(provider_count .. ' provider(s) configured')
end
for name, provider in pairs(config.providers) do
local bin = provider.cmd[1]
if vim.fn.executable(bin) == 1 then
vim.health.ok('provider "' .. name .. '": ' .. bin .. ' found')
else
vim.health.error('provider "' .. name .. '": ' .. bin .. ' not found')
end
end
local ft_count = vim.tbl_count(config.providers_by_ft)
if ft_count > 0 then
for ft, name in pairs(config.providers_by_ft) do
if config.providers[name] then
vim.health.ok('filetype "' .. ft .. '" -> provider "' .. name .. '"')
else
vim.health.error('filetype "' .. ft .. '" maps to unknown provider "' .. name .. '"')
end
end
end
end
return M