From fbb18a5b92e2f11aaaef379d74d4a1132a138cb3 Mon Sep 17 00:00:00 2001 From: Steven Arcangeli <506791+stevearc@users.noreply.github.com> Date: Fri, 15 Sep 2023 08:35:28 -0700 Subject: feat: allow running commands in a shell (#49) --- lua/conform/init.lua | 4 ++-- lua/conform/runner.lua | 25 ++++++++++++++++--------- 2 files changed, 18 insertions(+), 11 deletions(-) (limited to 'lua') 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 -- cgit v1.2.3-70-g09d2