From 12cb20d154dd4ccbb5144855ebdba96e97fb68f6 Mon Sep 17 00:00:00 2001 From: Barrett Ruth <62671086+barrettruth@users.noreply.github.com> Date: Thu, 5 Mar 2026 22:26:28 -0500 Subject: [PATCH] feat: add `extra_args` provider field (#51) * feat: add `extra_args` provider field Problem: Overriding a single flag (e.g. `-outdir=build`) required redefining the entire `args` function, duplicating all preset defaults. Solution: Add `extra_args` field that appends to the resolved `args` after evaluation. Accepts a static table or a context function. * docs: document `extra_args` provider field --- doc/preview.txt | 4 ++++ lua/preview/compiler.lua | 3 +++ lua/preview/init.lua | 2 ++ 3 files changed, 9 insertions(+) diff --git a/doc/preview.txt b/doc/preview.txt index 383a8f5..b4b74f5 100644 --- a/doc/preview.txt +++ b/doc/preview.txt @@ -68,6 +68,10 @@ Provider fields: ~ receives a |preview.Context| and returns a string[]. + {extra_args} (string[]|function) Appended to {args} after evaluation. + Useful for adding flags to a preset + without replacing its defaults. + {cwd} (string|function) Working directory. If a function, receives a |preview.Context|. Default: git root or file directory. diff --git a/lua/preview/compiler.lua b/lua/preview/compiler.lua index a193ad2..3e8de12 100644 --- a/lua/preview/compiler.lua +++ b/lua/preview/compiler.lua @@ -309,6 +309,9 @@ function M.compile(bufnr, name, provider, ctx, opts) if provider.args then vim.list_extend(cmd, eval_list(provider.args, resolved_ctx)) end + if provider.extra_args then + vim.list_extend(cmd, eval_list(provider.extra_args, resolved_ctx)) + end log.dbg('compiling buffer %d with provider "%s": %s', bufnr, name, table.concat(cmd, ' ')) diff --git a/lua/preview/init.lua b/lua/preview/init.lua index 7cb982b..489679f 100644 --- a/lua/preview/init.lua +++ b/lua/preview/init.lua @@ -2,6 +2,7 @@ ---@field ft? string ---@field cmd string[] ---@field args? string[]|fun(ctx: preview.Context): string[] +---@field extra_args? string[]|fun(ctx: preview.Context): string[] ---@field cwd? string|fun(ctx: preview.Context): string ---@field env? table ---@field output? string|fun(ctx: preview.Context): string @@ -94,6 +95,7 @@ function M.setup(opts) vim.validate(prefix .. '.cmd', provider.cmd, 'table') vim.validate(prefix .. '.cmd[1]', provider.cmd[1], 'string') vim.validate(prefix .. '.args', provider.args, { 'table', 'function' }, true) + vim.validate(prefix .. '.extra_args', provider.extra_args, { 'table', 'function' }, true) vim.validate(prefix .. '.cwd', provider.cwd, { 'string', 'function' }, true) vim.validate(prefix .. '.output', provider.output, { 'string', 'function' }, true) vim.validate(prefix .. '.error_parser', provider.error_parser, 'function', true)