feat: rename
This commit is contained in:
parent
e1d7abf58e
commit
942438f817
21 changed files with 660 additions and 305 deletions
54
lua/preview/commands.lua
Normal file
54
lua/preview/commands.lua
Normal 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
|
||||
249
lua/preview/compiler.lua
Normal file
249
lua/preview/compiler.lua
Normal file
|
|
@ -0,0 +1,249 @@
|
|||
local M = {}
|
||||
|
||||
local diagnostic = require('preview.diagnostic')
|
||||
local log = require('preview.log')
|
||||
|
||||
---@type table<integer, preview.Process>
|
||||
local active = {}
|
||||
|
||||
---@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
|
||||
return val(ctx)
|
||||
end
|
||||
return val
|
||||
end
|
||||
|
||||
---@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
|
||||
return val(ctx)
|
||||
end
|
||||
return val
|
||||
end
|
||||
|
||||
---@param bufnr integer
|
||||
---@param name string
|
||||
---@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')
|
||||
end
|
||||
|
||||
if active[bufnr] then
|
||||
log.dbg('killing existing process for buffer %d before recompile', bufnr)
|
||||
M.stop(bufnr)
|
||||
end
|
||||
|
||||
local cmd = vim.list_extend({}, provider.cmd)
|
||||
if provider.args then
|
||||
vim.list_extend(cmd, eval_list(provider.args, ctx))
|
||||
end
|
||||
|
||||
local cwd = ctx.root
|
||||
if provider.cwd then
|
||||
cwd = eval_string(provider.cwd, ctx)
|
||||
end
|
||||
|
||||
local output_file = ''
|
||||
if provider.output then
|
||||
output_file = eval_string(provider.output, ctx)
|
||||
end
|
||||
|
||||
log.dbg('compiling buffer %d with provider "%s": %s', bufnr, name, table.concat(cmd, ' '))
|
||||
|
||||
local obj = vim.system(
|
||||
cmd,
|
||||
{
|
||||
cwd = cwd,
|
||||
env = provider.env,
|
||||
},
|
||||
vim.schedule_wrap(function(result)
|
||||
active[bufnr] = nil
|
||||
|
||||
if result.code == 0 then
|
||||
log.dbg('compilation succeeded for buffer %d', bufnr)
|
||||
diagnostic.clear(bufnr)
|
||||
vim.api.nvim_exec_autocmds('User', {
|
||||
pattern = 'PreviewCompileSuccess',
|
||||
data = { bufnr = bufnr, provider = name, output = output_file },
|
||||
})
|
||||
else
|
||||
log.dbg('compilation failed for buffer %d (exit code %d)', bufnr, result.code)
|
||||
if provider.error_parser then
|
||||
diagnostic.set(bufnr, name, provider.error_parser, result.stderr or '', ctx)
|
||||
end
|
||||
vim.api.nvim_exec_autocmds('User', {
|
||||
pattern = 'PreviewCompileFailed',
|
||||
data = {
|
||||
bufnr = bufnr,
|
||||
provider = name,
|
||||
code = result.code,
|
||||
stderr = result.stderr or '',
|
||||
},
|
||||
})
|
||||
end
|
||||
end)
|
||||
)
|
||||
|
||||
active[bufnr] = { obj = obj, provider = name, output_file = output_file }
|
||||
|
||||
vim.api.nvim_create_autocmd('BufWipeout', {
|
||||
buffer = bufnr,
|
||||
once = true,
|
||||
callback = function()
|
||||
M.stop(bufnr)
|
||||
end,
|
||||
})
|
||||
|
||||
vim.api.nvim_exec_autocmds('User', {
|
||||
pattern = 'PreviewCompileStarted',
|
||||
data = { bufnr = bufnr, provider = name },
|
||||
})
|
||||
end
|
||||
|
||||
---@param bufnr integer
|
||||
function M.stop(bufnr)
|
||||
local proc = active[bufnr]
|
||||
if not proc then
|
||||
return
|
||||
end
|
||||
log.dbg('stopping process for buffer %d', bufnr)
|
||||
---@type fun(self: table, signal: string|integer)
|
||||
local kill = proc.obj.kill
|
||||
kill(proc.obj, 'sigterm')
|
||||
|
||||
local timer = vim.uv.new_timer()
|
||||
if timer then
|
||||
timer:start(5000, 0, function()
|
||||
timer:close()
|
||||
if active[bufnr] and active[bufnr].obj == proc.obj then
|
||||
kill(proc.obj, 'sigkill')
|
||||
active[bufnr] = nil
|
||||
end
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
||||
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 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('[preview.nvim] provider "' .. name .. '" has no clean command', vim.log.levels.WARN)
|
||||
return
|
||||
end
|
||||
|
||||
local cmd = eval_list(provider.clean, ctx)
|
||||
local cwd = ctx.root
|
||||
if provider.cwd then
|
||||
cwd = eval_string(provider.cwd, ctx)
|
||||
end
|
||||
|
||||
log.dbg('cleaning buffer %d with provider "%s": %s', bufnr, name, table.concat(cmd, ' '))
|
||||
|
||||
vim.system(
|
||||
cmd,
|
||||
{ cwd = cwd },
|
||||
vim.schedule_wrap(function(result)
|
||||
if result.code == 0 then
|
||||
log.dbg('clean succeeded for buffer %d', bufnr)
|
||||
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('[preview.nvim] clean failed: ' .. (result.stderr or ''), vim.log.levels.ERROR)
|
||||
end
|
||||
end)
|
||||
)
|
||||
end
|
||||
|
||||
---@param bufnr integer
|
||||
---@return preview.Status
|
||||
function M.status(bufnr)
|
||||
local proc = active[bufnr]
|
||||
if proc then
|
||||
return {
|
||||
compiling = true,
|
||||
watching = watching[bufnr] ~= nil,
|
||||
provider = proc.provider,
|
||||
output_file = proc.output_file,
|
||||
}
|
||||
end
|
||||
return { compiling = false, watching = watching[bufnr] ~= nil }
|
||||
end
|
||||
|
||||
M._test = {
|
||||
active = active,
|
||||
watching = watching,
|
||||
}
|
||||
|
||||
return M
|
||||
40
lua/preview/diagnostic.lua
Normal file
40
lua/preview/diagnostic.lua
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
local M = {}
|
||||
|
||||
local log = require('preview.log')
|
||||
|
||||
local ns = vim.api.nvim_create_namespace('preview')
|
||||
|
||||
---@param bufnr integer
|
||||
function M.clear(bufnr)
|
||||
vim.diagnostic.set(ns, bufnr, {})
|
||||
log.dbg('cleared diagnostics for buffer %d', bufnr)
|
||||
end
|
||||
|
||||
---@param bufnr integer
|
||||
---@param name string
|
||||
---@param error_parser fun(stderr: string, ctx: preview.Context): preview.Diagnostic[]
|
||||
---@param stderr string
|
||||
---@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
|
||||
log.dbg('error_parser for "%s" failed: %s', name, diagnostics)
|
||||
return
|
||||
end
|
||||
if not diagnostics or #diagnostics == 0 then
|
||||
log.dbg('error_parser for "%s" returned no diagnostics', name)
|
||||
return
|
||||
end
|
||||
for _, d in ipairs(diagnostics) do
|
||||
d.source = d.source or name
|
||||
end
|
||||
vim.diagnostic.set(ns, bufnr, diagnostics)
|
||||
log.dbg('set %d diagnostics for buffer %d from provider "%s"', #diagnostics, bufnr, name)
|
||||
end
|
||||
|
||||
---@return integer
|
||||
function M.get_namespace()
|
||||
return ns
|
||||
end
|
||||
|
||||
return M
|
||||
31
lua/preview/health.lua
Normal file
31
lua/preview/health.lua
Normal 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
|
||||
182
lua/preview/init.lua
Normal file
182
lua/preview/init.lua
Normal file
|
|
@ -0,0 +1,182 @@
|
|||
---@class preview.ProviderConfig
|
||||
---@field cmd 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: preview.Context): string
|
||||
---@field error_parser? fun(stderr: string, ctx: preview.Context): preview.Diagnostic[]
|
||||
---@field clean? string[]|fun(ctx: preview.Context): string[]
|
||||
|
||||
---@class preview.Config
|
||||
---@field debug boolean|string
|
||||
---@field providers table<string, preview.ProviderConfig>
|
||||
|
||||
---@class preview.Context
|
||||
---@field bufnr integer
|
||||
---@field file string
|
||||
---@field root string
|
||||
---@field ft string
|
||||
|
||||
---@class preview.Diagnostic
|
||||
---@field lnum integer
|
||||
---@field col integer
|
||||
---@field message string
|
||||
---@field severity? integer
|
||||
---@field end_lnum? integer
|
||||
---@field end_col? integer
|
||||
---@field source? string
|
||||
|
||||
---@class preview.Process
|
||||
---@field obj table
|
||||
---@field provider string
|
||||
---@field output_file string
|
||||
|
||||
---@class preview
|
||||
---@field compile fun(bufnr?: integer)
|
||||
---@field stop fun(bufnr?: integer)
|
||||
---@field clean fun(bufnr?: integer)
|
||||
---@field watch fun(bufnr?: integer)
|
||||
---@field status fun(bufnr?: integer): preview.Status
|
||||
---@field get_config fun(): preview.Config
|
||||
local M = {}
|
||||
|
||||
local compiler = require('preview.compiler')
|
||||
local log = require('preview.log')
|
||||
|
||||
---@type preview.Config
|
||||
local default_config = {
|
||||
debug = false,
|
||||
providers = {},
|
||||
}
|
||||
|
||||
---@type preview.Config
|
||||
local config = vim.deepcopy(default_config)
|
||||
|
||||
local initialized = false
|
||||
|
||||
local function init()
|
||||
if initialized then
|
||||
return
|
||||
end
|
||||
initialized = true
|
||||
|
||||
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')
|
||||
end
|
||||
|
||||
config = vim.tbl_deep_extend('force', default_config, opts)
|
||||
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
|
||||
log.dbg('no provider configured for filetype: %s', ft)
|
||||
return nil
|
||||
end
|
||||
return ft
|
||||
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')
|
||||
return {
|
||||
bufnr = bufnr,
|
||||
file = file,
|
||||
root = root,
|
||||
ft = vim.bo[bufnr].filetype,
|
||||
}
|
||||
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)
|
||||
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)
|
||||
compiler.clean(bufnr, name, provider, ctx)
|
||||
end
|
||||
|
||||
---@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 preview.Status
|
||||
function M.status(bufnr)
|
||||
init()
|
||||
bufnr = bufnr or vim.api.nvim_get_current_buf()
|
||||
return compiler.status(bufnr)
|
||||
end
|
||||
|
||||
M._test = {
|
||||
---@diagnostic disable-next-line: assign-type-mismatch
|
||||
reset = function()
|
||||
initialized = false
|
||||
config = vim.deepcopy(default_config)
|
||||
end,
|
||||
}
|
||||
|
||||
return M
|
||||
35
lua/preview/log.lua
Normal file
35
lua/preview/log.lua
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
local M = {}
|
||||
|
||||
local enabled = false
|
||||
local log_file = nil
|
||||
|
||||
---@param val boolean|string
|
||||
function M.set_enabled(val)
|
||||
if type(val) == 'string' then
|
||||
enabled = true
|
||||
log_file = val
|
||||
else
|
||||
enabled = val
|
||||
log_file = nil
|
||||
end
|
||||
end
|
||||
|
||||
---@param msg string
|
||||
---@param ... any
|
||||
function M.dbg(msg, ...)
|
||||
if not enabled then
|
||||
return
|
||||
end
|
||||
local formatted = '[preview.nvim]: ' .. string.format(msg, ...)
|
||||
if log_file then
|
||||
local f = io.open(log_file, 'a')
|
||||
if f then
|
||||
f:write(string.format('%.6fs', vim.uv.hrtime() / 1e9) .. ' ' .. formatted .. '\n')
|
||||
f:close()
|
||||
end
|
||||
else
|
||||
vim.notify(formatted, vim.log.levels.DEBUG)
|
||||
end
|
||||
end
|
||||
|
||||
return M
|
||||
40
lua/preview/presets.lua
Normal file
40
lua/preview/presets.lua
Normal 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
|
||||
Loading…
Add table
Add a link
Reference in a new issue