Skip to content

Commit

Permalink
fix: injected formatter adds language to file extension
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc committed Nov 15, 2023
1 parent e76ad83 commit 66d38ee
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lua/conform/formatters/injected.lua
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ return {
end

local num_format = 0
local tmp_bufs = {}
local formatter_cb = function(err, idx, start_lnum, end_lnum, new_lines)
if err then
format_error = errors.coalesce(format_error, err)
Expand All @@ -159,6 +160,9 @@ return {
end
num_format = num_format - 1
if num_format == 0 then
for buf in pairs(tmp_bufs) do
vim.api.nvim_buf_delete(buf, { force = true })
end
apply_format_results()
end
end
Expand All @@ -181,11 +185,15 @@ return {
return f.name
end, formatters)
end
local format_opts = { async = true, bufnr = ctx.buf, quiet = true }
local idx = num_format
log.debug("Injected format %s:%d:%d: %s", lang, start_lnum, end_lnum, formatter_names)
log.trace("Injected format lines %s", input_lines)
local indent = remove_indent(input_lines, buf_lang)
-- Create a temporary buffer. This is only needed because some formatters rely on the file
-- extension to determine a run mode (see https://github.com/stevearc/conform.nvim/issues/194)
local buf = vim.fn.bufadd(string.format("%s.%s", vim.api.nvim_buf_get_name(ctx.buf), lang))
tmp_bufs[buf] = true
local format_opts = { async = true, bufnr = buf, quiet = true }
conform.format_lines(formatter_names, input_lines, format_opts, function(err, new_lines)
-- Preserve indentation in case the code block is indented
apply_indent(new_lines, indent)
Expand Down

0 comments on commit 66d38ee

Please sign in to comment.