diff options
-rw-r--r-- | doc/conform.txt | 24 | ||||
-rw-r--r-- | lua/conform/init.lua | 10 | ||||
-rw-r--r-- | lua/conform/runner.lua | 11 | ||||
-rw-r--r-- | lua/conform/util.lua | 46 | ||||
-rwxr-xr-x | scripts/generate.py | 39 |
5 files changed, 13 insertions, 117 deletions
diff --git a/doc/conform.txt b/doc/conform.txt index e647ce5..8182be5 100644 --- a/doc/conform.txt +++ b/doc/conform.txt @@ -6,7 +6,6 @@ CONTENTS *conform-content 1. Options |conform-options| 2. Api |conform-api| 3. Formatters |conform-formatters| - 4. Self argument migration |conform-self-args| -------------------------------------------------------------------------------- OPTIONS *conform-options* @@ -355,28 +354,5 @@ FORMATTERS *conform-formatter `zigfmt` - Reformat Zig source into canonical form. `zprint` - Formatter for Clojure and EDN. --------------------------------------------------------------------------------- -SELF ARGUMENT MIGRATION *conform-self-args* - -The function arguments for formatter config functions have changed. Previously, -they took a single `ctx` argument. ->lua - { - command = "phpcbf", - args = function(ctx) - return { "-q", "--stdin-path=" .. ctx.filename, "-" } - end - } -<Now, they take `self` as the first argument, and `ctx` as the second. ->lua - { - command = "phpcbf", - args = function(self, ctx) - return { "-q", "--stdin-path=" .. ctx.filename, "-" } - end - } -<The config values that can be defined as functions are: `command`, `args`, -`range_args`, `cwd`, `env`, and `condition`. - ================================================================================ vim:tw=80:ts=2:ft=help:norl:syntax=help: diff --git a/lua/conform/init.lua b/lua/conform/init.lua index db0e939..02d8f7a 100644 --- a/lua/conform/init.lua +++ b/lua/conform/init.lua @@ -657,21 +657,21 @@ M.get_formatter_info = function(formatter, bufnr) local command = config.command if type(command) == "function" then - command = util.compat_call_with_self(formatter, config, command, ctx) + ---@cast config conform.JobFormatterConfig + command = command(config, ctx) end if vim.fn.executable(command) == 0 then available = false available_msg = "Command not found" - elseif - config.condition and not util.compat_call_with_self(formatter, config, config.condition, ctx) - then + elseif config.condition and not config.condition(config, ctx) then available = false available_msg = "Condition failed" end local cwd = nil if config.cwd then - cwd = util.compat_call_with_self(formatter, config, config.cwd, ctx) + ---@cast config conform.JobFormatterConfig + cwd = config.cwd(config, ctx) if available and not cwd and config.require_cwd then available = false available_msg = "Root directory not found" diff --git a/lua/conform/runner.lua b/lua/conform/runner.lua index 725e824..2810948 100644 --- a/lua/conform/runner.lua +++ b/lua/conform/runner.lua @@ -16,16 +16,17 @@ local M = {} M.build_cmd = function(formatter_name, ctx, config) local command = config.command if type(command) == "function" then - command = util.compat_call_with_self(formatter_name, config, command, ctx) + command = command(config, ctx) end ---@type string|string[] local args = {} if ctx.range and config.range_args then - args = util.compat_call_with_self(formatter_name, config, config.range_args, ctx) + ---@cast ctx conform.RangeContext + args = config.range_args(config, ctx) elseif config.args then local computed_args = config.args if type(computed_args) == "function" then - args = util.compat_call_with_self(formatter_name, config, computed_args, ctx) + args = computed_args(config, ctx) else ---@diagnostic disable-next-line: cast-local-type args = computed_args @@ -279,11 +280,11 @@ local function run_formatter(bufnr, formatter, config, ctx, input_lines, opts, c local cmd = M.build_cmd(formatter.name, ctx, config) local cwd = nil if config.cwd then - cwd = util.compat_call_with_self(formatter.name, config, config.cwd, ctx) + cwd = config.cwd(config, ctx) end local env = config.env if type(env) == "function" then - env = util.compat_call_with_self(formatter.name, config, env, ctx) + env = env(config, ctx) end local buffer_text diff --git a/lua/conform/util.lua b/lua/conform/util.lua index 0ff54de..e967918 100644 --- a/lua/conform/util.lua +++ b/lua/conform/util.lua @@ -115,10 +115,10 @@ M.extend_args = function(args, extra_args, opts) opts = opts or {} return function(self, ctx) if type(args) == "function" then - args = M.compat_call_with_self("unknown", self, args, ctx) + args = args(self, ctx) end if type(extra_args) == "function" then - extra_args = M.compat_call_with_self("unknown", self, extra_args, ctx) + extra_args = extra_args(self, ctx) end if type(args) == "string" then if type(extra_args) ~= "string" then @@ -183,46 +183,4 @@ M.buf_get_changedtick = function(bufnr) end end ----Returns true if the function takes no args or has self as the first arg ----@param name string ----@param fn function(...: any): T ----@return boolean -local function has_self_arg(name, fn) - local first_arg_name = nil - debug.sethook(function() - local info = debug.getinfo(3) - if info.name ~= "pcall" then - return - end - first_arg_name = debug.getlocal(2, 1) - error() - end, "c") - pcall(fn) - debug.sethook() - - return first_arg_name == "self" or first_arg_name == nil -end - ----@generic T ----@param formatter_name string ----@param self any ----@param fn fun(...: any): T ----@param ... any ----@return T -M.compat_call_with_self = function(formatter_name, self, fn, ...) - local has_self = has_self_arg(formatter_name, fn) - if has_self then - return fn(self, ...) - else - vim.notify_once( - string.format( - "[conform] formatter %s should take 'self' as the first argument for args, range_args, cwd, condition, and env functions (see :help conform-self-args)\nCompatibility will be dropped on 2024-03-01", - formatter_name - ), - vim.log.levels.WARN - ) - return fn(...) - end -end - return M diff --git a/scripts/generate.py b/scripts/generate.py index 032a86a..e671ce0 100755 --- a/scripts/generate.py +++ b/scripts/generate.py @@ -176,44 +176,6 @@ def gen_options_vimdoc() -> VimdocSection: return section -def gen_self_compat_vimdoc() -> VimdocSection: - section = VimdocSection("self argument migration", "conform-self-args", ["\n"]) - section.body.extend( - wrap( - "The function arguments for formatter config functions have changed. Previously, they took a single `ctx` argument." - ) - ) - section.body.append( - """>lua - { - command = "phpcbf", - args = function(ctx) - return { "-q", "--stdin-path=" .. ctx.filename, "-" } - end - } -<""" - ) - section.body.extend( - wrap("Now, they take `self` as the first argument, and `ctx` as the second.") - ) - section.body.append( - """>lua - { - command = "phpcbf", - args = function(self, ctx) - return { "-q", "--stdin-path=" .. ctx.filename, "-" } - end - } -<""" - ) - section.body.extend( - wrap( - "The config values that can be defined as functions are: `command`, `args`, `range_args`, `cwd`, `env`, and `condition`." - ) - ) - return section - - def gen_formatter_vimdoc() -> VimdocSection: section = VimdocSection("Formatters", "conform-formatters", ["\n"]) for formatter in get_all_formatters(): @@ -230,7 +192,6 @@ def generate_vimdoc(): gen_options_vimdoc(), VimdocSection("API", "conform-api", render_vimdoc_api("conform", funcs)), gen_formatter_vimdoc(), - gen_self_compat_vimdoc(), ] ) |