ci: format
This commit is contained in:
commit
e49a664d48
30 changed files with 1612 additions and 0 deletions
47
lua/render/commands.lua
Normal file
47
lua/render/commands.lua
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
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
|
||||
184
lua/render/compiler.lua
Normal file
184
lua/render/compiler.lua
Normal file
|
|
@ -0,0 +1,184 @@
|
|||
local M = {}
|
||||
|
||||
local diagnostic = require('render.diagnostic')
|
||||
local log = require('render.log')
|
||||
|
||||
---@type table<integer, render.Process>
|
||||
local active = {}
|
||||
|
||||
---@param val string[]|fun(ctx: render.Context): string[]
|
||||
---@param ctx render.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: render.Context): string
|
||||
---@param ctx render.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 render.ProviderConfig
|
||||
---@param ctx render.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 = 'RenderCompileSuccess',
|
||||
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 = 'RenderCompileFailed',
|
||||
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 = 'RenderCompileStarted',
|
||||
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)
|
||||
proc.obj:kill('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
|
||||
proc.obj:kill('sigkill')
|
||||
active[bufnr] = nil
|
||||
end
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
||||
function M.stop_all()
|
||||
for bufnr, _ in pairs(active) do
|
||||
M.stop(bufnr)
|
||||
end
|
||||
end
|
||||
|
||||
---@param bufnr integer
|
||||
---@param name string
|
||||
---@param provider render.ProviderConfig
|
||||
---@param ctx render.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)
|
||||
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('[render.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)
|
||||
end
|
||||
end)
|
||||
)
|
||||
end
|
||||
|
||||
---@param bufnr integer
|
||||
---@return render.Status
|
||||
function M.status(bufnr)
|
||||
local proc = active[bufnr]
|
||||
if proc then
|
||||
return { compiling = true, provider = proc.provider, output_file = proc.output_file }
|
||||
end
|
||||
return { compiling = false }
|
||||
end
|
||||
|
||||
M._test = {
|
||||
active = active,
|
||||
}
|
||||
|
||||
return M
|
||||
40
lua/render/diagnostic.lua
Normal file
40
lua/render/diagnostic.lua
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
local M = {}
|
||||
|
||||
local log = require('render.log')
|
||||
|
||||
local ns = vim.api.nvim_create_namespace('render')
|
||||
|
||||
---@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: render.Context): vim.Diagnostic[]
|
||||
---@param stderr string
|
||||
---@param ctx render.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
|
||||
42
lua/render/health.lua
Normal file
42
lua/render/health.lua
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
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
|
||||
168
lua/render/init.lua
Normal file
168
lua/render/init.lua
Normal file
|
|
@ -0,0 +1,168 @@
|
|||
---@class render.ProviderConfig
|
||||
---@field cmd string[]
|
||||
---@field args? string[]|fun(ctx: render.Context): string[]
|
||||
---@field cwd? string|fun(ctx: render.Context): string
|
||||
---@field env? table<string, string>
|
||||
---@field output? string|fun(ctx: render.Context): string
|
||||
---@field error_parser? fun(stderr: string, ctx: render.Context): vim.Diagnostic[]
|
||||
---@field clean? string[]|fun(ctx: render.Context): string[]
|
||||
|
||||
---@class render.Config
|
||||
---@field debug boolean|string
|
||||
---@field providers table<string, render.ProviderConfig>
|
||||
---@field providers_by_ft table<string, string>
|
||||
|
||||
---@class render.Context
|
||||
---@field bufnr integer
|
||||
---@field file string
|
||||
---@field root string
|
||||
---@field ft string
|
||||
|
||||
---@class render.Process
|
||||
---@field obj vim.SystemObj
|
||||
---@field provider string
|
||||
---@field output_file string
|
||||
|
||||
---@class render
|
||||
---@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
|
||||
local M = {}
|
||||
|
||||
local compiler = require('render.compiler')
|
||||
local log = require('render.log')
|
||||
|
||||
---@type render.Config
|
||||
local default_config = {
|
||||
debug = false,
|
||||
providers = {},
|
||||
providers_by_ft = {},
|
||||
}
|
||||
|
||||
---@type render.Config
|
||||
local config = vim.deepcopy(default_config)
|
||||
|
||||
local initialized = false
|
||||
|
||||
local function init()
|
||||
if initialized then
|
||||
return
|
||||
end
|
||||
initialized = true
|
||||
|
||||
local opts = vim.g.render or {}
|
||||
|
||||
vim.validate('render config', opts, 'table')
|
||||
if opts.debug ~= nil then
|
||||
vim.validate('render 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')
|
||||
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 render.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
|
||||
local name = config.providers_by_ft[ft]
|
||||
if not name then
|
||||
log.dbg('no provider mapped 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
|
||||
end
|
||||
|
||||
---@param bufnr? integer
|
||||
---@return render.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('[render.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('[render.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
|
||||
|
||||
---@class render.Status
|
||||
---@field compiling boolean
|
||||
---@field provider? string
|
||||
---@field output_file? string
|
||||
|
||||
---@param bufnr? integer
|
||||
---@return render.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/render/log.lua
Normal file
35
lua/render/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 = '[render.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
|
||||
Loading…
Add table
Add a link
Reference in a new issue