Skip to content

Commit

Permalink
feat: formatter config function is passed the buffer number (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc committed Aug 29, 2023
1 parent 03a37f1 commit 8b2a574
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
17 changes: 11 additions & 6 deletions lua/conform/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ local M = {}
---@type table<string, string[]|conform.FormatterList>
M.formatters_by_ft = {}

---@type table<string, conform.FormatterConfig|fun(): conform.FormatterConfig>
---@type table<string, conform.FormatterConfig|fun(bufnr: integer): nil|conform.FormatterConfig>
M.formatters = {}

M.setup = function(opts)
Expand Down Expand Up @@ -334,19 +334,24 @@ end

---@private
---@param formatter string
---@param bufnr? integer
---@return nil|conform.FormatterConfig
M.get_formatter_config = function(formatter)
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)
end
if not config then
local ok
ok, config = pcall(require, "conform.formatters." .. formatter)
if not ok then
return nil
end
end
if type(config) == "function" then
config = config()
end

if config.stdin == nil then
config.stdin = true
Expand All @@ -362,7 +367,7 @@ M.get_formatter_info = function(formatter, bufnr)
if not bufnr or bufnr == 0 then
bufnr = vim.api.nvim_get_current_buf()
end
local config = M.get_formatter_config(formatter)
local config = M.get_formatter_config(formatter, bufnr)
if not config then
return {
name = formatter,
Expand Down
4 changes: 2 additions & 2 deletions lua/conform/runner.lua
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ M.format_async = function(bufnr, formatters, range, callback)
end
idx = idx + 1

local config = assert(require("conform").get_formatter_config(formatter.name))
local config = assert(require("conform").get_formatter_config(formatter.name, bufnr))
local ctx = M.build_context(bufnr, config, range)
local jid
jid = run_formatter(bufnr, formatter, config, ctx, input_lines, function(err, output)
Expand Down Expand Up @@ -322,7 +322,7 @@ M.format_sync = function(bufnr, formatters, timeout_ms, quiet, range)
end
local done = false
local result = nil
local config = assert(require("conform").get_formatter_config(formatter.name))
local config = assert(require("conform").get_formatter_config(formatter.name, bufnr))
local ctx = M.build_context(bufnr, config, range)
local jid = run_formatter(bufnr, formatter, config, ctx, input_lines, function(err, output)
if err then
Expand Down

0 comments on commit 8b2a574

Please sign in to comment.