aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMicah Halter <micah@mehalter.com>2023-12-06 22:14:50 -0800
committerGitHub <noreply@github.com>2023-12-06 22:14:50 -0800
commit07fcbfc13490786f5983bce3f404643fcfd83775 (patch)
tree19894decfe37f81a17274588ed291e44591a11c8
parente5840429ac49c7acbf702cd2cff8bc6557e08c75 (diff)
fix: various fixes for the `injected` formatter (#235)
* fix: load temp buffer once created to load the context for formatters * fix: use `_injection_query` to decide if the `injection` formatter is valid
-rw-r--r--lua/conform/formatters/injected.lua7
1 files changed, 5 insertions, 2 deletions
diff --git a/lua/conform/formatters/injected.lua b/lua/conform/formatters/injected.lua
index b3d80a9..77a9c0d 100644
--- a/lua/conform/formatters/injected.lua
+++ b/lua/conform/formatters/injected.lua
@@ -74,9 +74,10 @@ return {
ignore_errors = false,
},
condition = function(self, ctx)
- local ok = pcall(vim.treesitter.get_parser, ctx.buf)
+ local ok, parser = pcall(vim.treesitter.get_parser, ctx.buf)
-- Require Neovim 0.9 because the treesitter API has changed significantly
- return ok and vim.fn.has("nvim-0.9") == 1
+ ---@diagnostic disable-next-line: invisible
+ return ok and parser._injection_query and vim.fn.has("nvim-0.9") == 1
end,
format = function(self, ctx, lines, callback)
local conform = require("conform")
@@ -226,6 +227,8 @@ return {
-- This is using the language name as the file extension, but that is a reasonable
-- approximation for now. We can add special cases as the need arises.
local buf = vim.fn.bufadd(string.format("%s.%s", vim.api.nvim_buf_get_name(ctx.buf), lang))
+ -- Actually load the buffer to set the buffer context which is required by some formatters such as `filetype`
+ vim.fn.bufload(buf)
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)