aboutsummaryrefslogtreecommitdiffstats
path: root/lua/conform/init.lua
diff options
context:
space:
mode:
authorSteven Arcangeli <506791+stevearc@users.noreply.github.com>2023-12-06 22:20:29 -0800
committerGitHub <noreply@github.com>2023-12-06 22:20:29 -0800
commit659838ff4244ef6af095395ce68aaaf99fa8e696 (patch)
treeb92754f2de1a225187769ed2c30e01a3ea703b3c /lua/conform/init.lua
parent8d0421a11b2944c6c059e0936a95182972921c30 (diff)
refactor!: formatter config functions take self as first argument (#233)
This is a breaking API change, but there is a shim in place that will keep existing functions working, just with a warning notification. Most people should not encounter this at all. For anyone overriding a formatter config value with a function that takes `(ctx)` as the parameter, it will need to be updated to take `(self, ctx)`.
Diffstat (limited to 'lua/conform/init.lua')
-rw-r--r--lua/conform/init.lua26
1 files changed, 15 insertions, 11 deletions
diff --git a/lua/conform/init.lua b/lua/conform/init.lua
index 50ae6ba..d980b8f 100644
--- a/lua/conform/init.lua
+++ b/lua/conform/init.lua
@@ -8,15 +8,16 @@ local M = {}
---@field available_msg? string
---@class (exact) conform.JobFormatterConfig
----@field command string|fun(ctx: conform.Context): 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 command string|fun(self: conform.JobFormatterConfig, ctx: conform.Context): string
+---@field args? string|string[]|fun(self: conform.JobFormatterConfig, ctx: conform.Context): string|string[]
+---@field range_args? fun(self: conform.JobFormatterConfig, ctx: conform.RangeContext): string|string[]
+---@field cwd? fun(self: conform.JobFormatterConfig, 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)
----@field condition? fun(ctx: conform.Context): boolean
+---@field condition? fun(self: conform.JobFormatterConfig, ctx: conform.Context): boolean
---@field exit_codes? integer[] Exit codes that indicate success (default {0})
----@field env? table<string, any>|fun(ctx: conform.Context): table<string, any>
+---@field env? table<string, any>|fun(self: conform.JobFormatterConfig, ctx: conform.Context): table<string, any>
+---@field options? table
---@class (exact) conform.LuaFormatterConfig
---@field format fun(self: conform.LuaFormatterConfig, ctx: conform.Context, lines: string[], callback: fun(err: nil|string, new_lines: nil|string[]))
@@ -33,8 +34,8 @@ local M = {}
---@class (exact) conform.FormatterConfigOverride : conform.JobFormatterConfig
---@field inherit? boolean
----@field command? string|fun(ctx: conform.Context): string
----@field prepend_args? string|string[]|fun(ctx: conform.Context): string|string[]
+---@field command? string|fun(self: conform.FormatterConfig, ctx: conform.Context): string
+---@field prepend_args? string|string[]|fun(self: conform.FormatterConfig, ctx: conform.Context): string|string[]
---@field options? table
---@class (exact) conform.FormatterMeta
@@ -606,6 +607,7 @@ end
---@param bufnr? integer
---@return conform.FormatterInfo
M.get_formatter_info = function(formatter, bufnr)
+ local util = require("conform.util")
if not bufnr or bufnr == 0 then
bufnr = vim.api.nvim_get_current_buf()
end
@@ -639,19 +641,21 @@ M.get_formatter_info = function(formatter, bufnr)
local command = config.command
if type(command) == "function" then
- command = command(ctx)
+ command = util.compat_call_with_self(formatter, config, command, ctx)
end
if vim.fn.executable(command) == 0 then
available = false
available_msg = "Command not found"
- elseif config.condition and not config.condition(ctx) then
+ elseif
+ config.condition and not util.compat_call_with_self(formatter, config, config.condition, ctx)
+ then
available = false
available_msg = "Condition failed"
end
local cwd = nil
if config.cwd then
- cwd = config.cwd(ctx)
+ cwd = util.compat_call_with_self(formatter, config, config.cwd, ctx)
if available and not cwd and config.require_cwd then
available = false
available_msg = "Root directory not found"