Skip to content

Commit

Permalink
feat: expose configuration options for injected formatter (#118)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc authored Oct 6, 2023
1 parent 0bffab5 commit ba1ca20
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
35 changes: 30 additions & 5 deletions lua/conform/formatters/injected.lua
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,29 @@ local function apply_indent(lines, indentation)
end
end

---@type conform.FileLuaFormatterConfig
---@class conform.InjectedFormatterConfig : conform.FileLuaFormatterConfig
---@field format fun(self: conform.InjectedFormatterConfig, ctx: conform.Context, lines: string[], callback: fun(err: nil|string, new_lines: nil|string[]))
---@field condition? fun(self: conform.InjectedFormatterConfig, ctx: conform.Context): boolean
---@field options conform.InjectedFormatterOptions

---@class (exact) conform.InjectedFormatterOptions
---@field ignore_errors boolean

---@type conform.InjectedFormatterConfig
return {
meta = {
url = "lua/conform/formatters/injected.lua",
description = "Format treesitter injected languages.",
},
condition = function(ctx)
options = {
-- Set to true to ignore errors
ignore_errors = false,
},
condition = function(self, ctx)
local ok = pcall(vim.treesitter.get_parser, ctx.buf)
return ok
end,
format = function(ctx, lines, callback)
format = function(self, ctx, lines, callback)
local conform = require("conform")
local log = require("conform.log")
local util = require("conform.util")
Expand Down Expand Up @@ -112,8 +124,20 @@ return {

local function apply_format_results()
if format_error then
callback(format_error)
return
if self.options.ignore_errors then
-- Find all of the conform errors in the replacements table and remove them
local i = 1
while i <= #replacements do
if replacements[i].code then
table.remove(replacements, i)
else
i = i + 1
end
end
else
callback(format_error)
return
end
end

local formatted_lines = vim.deepcopy(lines)
Expand All @@ -133,6 +157,7 @@ return {
local formatter_cb = function(err, idx, start_lnum, end_lnum, new_lines)
if err then
format_error = err
replacements[idx] = err
else
replacements[idx] = { start_lnum, end_lnum, new_lines }
end
Expand Down
7 changes: 4 additions & 3 deletions lua/conform/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ local M = {}
---@field env? table<string, any>|fun(ctx: conform.Context): table<string, any>

---@class (exact) conform.LuaFormatterConfig
---@field format fun(ctx: conform.Context, lines: string[], callback: fun(err: nil|string, new_lines: nil|string[]))
---@field condition? fun(ctx: conform.Context): boolean
---@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

---@class (exact) conform.FileLuaFormatterConfig : conform.LuaFormatterConfig
---@field meta conform.FormatterMeta
Expand Down Expand Up @@ -574,7 +574,8 @@ M.get_formatter_info = function(formatter, bufnr)
local available = true
local available_msg = nil
if config.format then
if config.condition and not config.condition(ctx) then
---@cast config conform.LuaFormatterConfig
if config.condition and not config:condition(ctx) then
available = false
available_msg = "Condition failed"
end
Expand Down
2 changes: 1 addition & 1 deletion lua/conform/runner.lua
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ local function run_formatter(bufnr, formatter, config, ctx, input_lines, opts, c
log.trace("Input lines: %s", input_lines)
if config.format then
---@cast config conform.LuaFormatterConfig
local ok, err = pcall(config.format, ctx, input_lines, callback)
local ok, err = pcall(config.format, config, ctx, input_lines, callback)
if not ok then
callback({
code = M.ERROR_CODE.RUNTIME,
Expand Down

0 comments on commit ba1ca20

Please sign in to comment.