Skip to content

Commit

Permalink
fix: various fixes for the injected formatter (#235)
Browse files Browse the repository at this point in the history
* fix: load temp buffer once created to load the context for formatters

* fix: use `_injection_query` to decide if the `injection` formatter is valid
  • Loading branch information
mehalter authored Dec 7, 2023
1 parent e584042 commit 07fcbfc
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lua/conform/formatters/injected.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 07fcbfc

Please sign in to comment.