aboutsummaryrefslogtreecommitdiffstats
path: root/lua
diff options
context:
space:
mode:
authorSteven Arcangeli <506791+stevearc@users.noreply.github.com>2023-09-15 08:35:28 -0700
committerGitHub <noreply@github.com>2023-09-15 08:35:28 -0700
commitfbb18a5b92e2f11aaaef379d74d4a1132a138cb3 (patch)
tree8a9193f0be376d9317f447790f23a5a0f1554a7c /lua
parent3f855f3675a22cb52a6754f3ab073d13528ce0ca (diff)
feat: allow running commands in a shell (#49)
Diffstat (limited to 'lua')
-rw-r--r--lua/conform/init.lua4
-rw-r--r--lua/conform/runner.lua25
2 files changed, 18 insertions, 11 deletions
diff --git a/lua/conform/init.lua b/lua/conform/init.lua
index ff30686..a19813e 100644
--- a/lua/conform/init.lua
+++ b/lua/conform/init.lua
@@ -9,8 +9,8 @@ local M = {}
---@class (exact) conform.FormatterConfig
---@field command string|fun(ctx: conform.Context): string
----@field args? string[]|fun(ctx: conform.Context): string[]
----@field range_args? fun(ctx: conform.RangeContext): string[]
+---@field args? string|string[]|fun(ctx: conform.Context): string|string[]
+---@field range_args? fun(ctx: conform.RangeContext): string|string[]
---@field cwd? fun(ctx: conform.Context): nil|string
---@field require_cwd? boolean When cwd is not found, don't run the formatter (default false)
---@field stdin? boolean Send buffer contents to stdin (default true)
diff --git a/lua/conform/runner.lua b/lua/conform/runner.lua
index ff8fbef..fff638b 100644
--- a/lua/conform/runner.lua
+++ b/lua/conform/runner.lua
@@ -48,12 +48,13 @@ end
---@param ctx conform.Context
---@param config conform.FormatterConfig
+---@return string|string[]
M.build_cmd = function(ctx, config)
local command = config.command
if type(command) == "function" then
command = command(ctx)
end
- local cmd = { command }
+ ---@type string|string[]
local args = {}
if ctx.range and config.range_args then
---@cast ctx conform.RangeContext
@@ -67,16 +68,22 @@ M.build_cmd = function(ctx, config)
end
end
- ---@diagnostic disable-next-line: param-type-mismatch
- for _, v in ipairs(args) do
- if v == "$FILENAME" then
- v = ctx.filename
- elseif v == "$DIRNAME" then
- v = ctx.dirname
+ if type(args) == "string" then
+ local interpolated = args:gsub("$FILENAME", ctx.filename):gsub("$DIRNAME", ctx.dirname)
+ return command .. " " .. interpolated
+ else
+ local cmd = { command }
+ ---@diagnostic disable-next-line: param-type-mismatch
+ for _, v in ipairs(args) do
+ if v == "$FILENAME" then
+ v = ctx.filename
+ elseif v == "$DIRNAME" then
+ v = ctx.dirname
+ end
+ table.insert(cmd, v)
end
- table.insert(cmd, v)
+ return cmd
end
- return cmd
end
---@param range? conform.Range