Skip to content

Commit

Permalink
fix: _injection_query doesn't exist for all parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
mehalter committed Dec 5, 2023
1 parent 18638fe commit 7633252
Showing 1 changed file with 38 additions and 35 deletions.
73 changes: 38 additions & 35 deletions lua/conform/formatters/injected.lua
Original file line number Diff line number Diff line change
Expand Up @@ -100,45 +100,48 @@ return {
local root_lang = parser:lang()
local regions = {}

for _, tree in pairs(parser:trees()) do
local root_node = tree:root()
local start_line, _, end_line, _ = root_node:range()
---@diagnostic disable-next-line: invisible
if parser._injection_query then
for _, tree in pairs(parser:trees()) do
local root_node = tree:root()
local start_line, _, end_line, _ = root_node:range()

-- I don't like using these private methods, but critically we do _not_ want to format
-- "combined" injections (they contain the metadata "injection.combined"). These injections
-- will merge all of their regions into a single LanguageTree. If we then try to format the
-- range defined by that LanguageTree, we will likely end up with a range that contains all
-- sorts of content. As a concrete example, consider the following markdown:
-- This is some text
-- <!-- Here is a comment -->
-- Some more text
-- <!-- Another comment -->
-- Since the html injection is combined, the range will contain "Some more text", which is not
-- what we want.
-- To avoid this, don't parse with injections. Instead, we use private methods to run the
-- injection queries ourselves, and then filter out the combined injections.
for _, match, metadata in
---@diagnostic disable-next-line: invisible
parser._injection_query:iter_matches(root_node, text, start_line, end_line + 1)
do
---@diagnostic disable-next-line: invisible
local lang, combined, ranges = parser:_get_injection(match, metadata)
local has_formatters = conform.formatters_by_ft[lang] ~= nil
if lang and has_formatters and not combined and #ranges > 0 and lang ~= root_lang then
local start_lnum
local end_lnum
-- Merge all of the ranges into a single range
for _, range in ipairs(ranges) do
if not start_lnum or start_lnum > range[1] + 1 then
start_lnum = range[1] + 1
-- I don't like using these private methods, but critically we do _not_ want to format
-- "combined" injections (they contain the metadata "injection.combined"). These injections
-- will merge all of their regions into a single LanguageTree. If we then try to format the
-- range defined by that LanguageTree, we will likely end up with a range that contains all
-- sorts of content. As a concrete example, consider the following markdown:
-- This is some text
-- <!-- Here is a comment -->
-- Some more text
-- <!-- Another comment -->
-- Since the html injection is combined, the range will contain "Some more text", which is not
-- what we want.
-- To avoid this, don't parse with injections. Instead, we use private methods to run the
-- injection queries ourselves, and then filter out the combined injections.
for _, match, metadata in
---@diagnostic disable-next-line: invisible
parser._injection_query:iter_matches(root_node, text, start_line, end_line + 1)
do
---@diagnostic disable-next-line: invisible
local lang, combined, ranges = parser:_get_injection(match, metadata)
local has_formatters = conform.formatters_by_ft[lang] ~= nil
if lang and has_formatters and not combined and #ranges > 0 and lang ~= root_lang then
local start_lnum
local end_lnum
-- Merge all of the ranges into a single range
for _, range in ipairs(ranges) do
if not start_lnum or start_lnum > range[1] + 1 then
start_lnum = range[1] + 1
end
if not end_lnum or end_lnum < range[4] then
end_lnum = range[4]
end
end
if not end_lnum or end_lnum < range[4] then
end_lnum = range[4]
if in_range(ctx.range, start_lnum, end_lnum) then
table.insert(regions, { lang, start_lnum, end_lnum })
end
end
if in_range(ctx.range, start_lnum, end_lnum) then
table.insert(regions, { lang, start_lnum, end_lnum })
end
end
end
end
Expand Down

0 comments on commit 7633252

Please sign in to comment.