From 7027ebbd772e2d3593f7dd566dea06d2d20622ee Mon Sep 17 00:00:00 2001 From: Steven Arcangeli <506791+stevearc@users.noreply.github.com> Date: Sun, 15 Oct 2023 16:18:08 -0700 Subject: feat!: merge configs in conform.formatters with defaults (#140) This breaking change should make it significantly easier to modify formatters. While I expect 99% of configs to be backwards-compatible, this can still potentially cause problems. If you: * define a formatter in the `formatters` option * that has the same name as a built-in formatter * and omits a property from the original formatter (e.g. leaves out `range_args` or `cwd`) Then you may encounter breaking behavior from this commit, because now your config definition will be merged with the built-in definition, and so will inherit those omitted properties. This config merging behavior can be opted-out of by adding `inherit = false` to your formatter config. --- lua/conform/init.lua | 46 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 10 deletions(-) (limited to 'lua/conform/init.lua') diff --git a/lua/conform/init.lua b/lua/conform/init.lua index 7a3cae6..724aad6 100644 --- a/lua/conform/init.lua +++ b/lua/conform/init.lua @@ -21,6 +21,7 @@ local M = {} ---@class (exact) conform.LuaFormatterConfig ---@field format fun(self: conform.LuaFormatterConfig, ctx: conform.Context, lines: string[], callback: fun(err: nil|string, new_lines: nil|string[])) ---@field condition? fun(self: conform.LuaFormatterConfig, ctx: conform.Context): boolean +---@field options? table ---@class (exact) conform.FileLuaFormatterConfig : conform.LuaFormatterConfig ---@field meta conform.FormatterMeta @@ -30,6 +31,12 @@ local M = {} ---@alias conform.FormatterConfig conform.JobFormatterConfig|conform.LuaFormatterConfig +---@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 options? table + ---@class (exact) conform.FormatterMeta ---@field url string ---@field description string @@ -53,7 +60,7 @@ local M = {} ---@type table M.formatters_by_ft = {} ----@type table +---@type table M.formatters = {} M.notify_on_error = true @@ -538,20 +545,39 @@ M.get_formatter_config = function(formatter, bufnr) if not bufnr or bufnr == 0 then bufnr = vim.api.nvim_get_current_buf() end - ---@type nil|conform.FormatterConfig|fun(bufnr: integer): nil|conform.FormatterConfig - local config = M.formatters[formatter] - if type(config) == "function" then - config = config(bufnr) + ---@type nil|conform.FormatterConfigOverride|fun(bufnr: integer): nil|conform.FormatterConfigOverride + local override = M.formatters[formatter] + if type(override) == "function" then + override = override(bufnr) end - if not config then - local ok - ok, config = pcall(require, "conform.formatters." .. formatter) - if not ok then + + ---@type nil|conform.FormatterConfig + local config = override + if not override or override.inherit ~= false then + local ok, mod_config = pcall(require, "conform.formatters." .. formatter) + if ok then + if override then + config = require("conform.util").merge_formatter_configs(mod_config, override) + else + config = mod_config + end + elseif override then + if override.command then + config = override + else + local msg = string.format( + "Formatter '%s' missing built-in definition\nSet `command` to get rid of this error.", + formatter + ) + vim.notify_once(msg, vim.log.levels.ERROR) + return nil + end + else return nil end end - if config.stdin == nil then + if config and config.stdin == nil then config.stdin = true end return config -- cgit v1.2.3-70-g09d2