From 07fcbfc13490786f5983bce3f404643fcfd83775 Mon Sep 17 00:00:00 2001 From: Micah Halter Date: Wed, 6 Dec 2023 22:14:50 -0800 Subject: 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 --- lua/conform/formatters/injected.lua | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'lua/conform/formatters') 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) -- cgit v1.2.3-70-g09d2