aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSteven Arcangeli <506791+stevearc@users.noreply.github.com>2023-12-26 09:09:41 -0800
committerGitHub <noreply@github.com>2023-12-26 09:09:41 -0800
commitf4f4e6c813338e3904dcecb0fa68f61a9ee621d6 (patch)
treef0b6d68576f8584dceacb047b8229e3ec02025a1
parent9245b616d1edb159775a0832c03324bf92884494 (diff)
refactor(injected): use included_regions instead of private methods (#253)
-rw-r--r--lua/conform/formatters/injected.lua43
-rw-r--r--tests/injected/combined_injections.md.formatted4
2 files changed, 13 insertions, 34 deletions
diff --git a/lua/conform/formatters/injected.lua b/lua/conform/formatters/injected.lua
index fd67439..cc09479 100644
--- a/lua/conform/formatters/injected.lua
+++ b/lua/conform/formatters/injected.lua
@@ -155,38 +155,17 @@ return {
---@type LangRange[]
local regions = {}
- 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)
- if
- lang
- and get_formatters(lang) ~= nil
- and not combined
- and #ranges > 0
- and lang ~= root_lang
- then
- for _, range in ipairs(ranges) do
- accum_range(regions, { lang, range[1] + 1, range[2], range[4] + 1, range[5] })
+ for lang, lang_tree in pairs(parser:children()) do
+ if lang ~= root_lang then
+ for _, ranges in ipairs(lang_tree:included_regions()) do
+ for _, region in ipairs(ranges) do
+ local formatters = get_formatters(lang)
+ if formatters ~= nil then
+ -- The types are wrong. included_regions should be Range[][] not integer[][]
+ ---@diagnostic disable-next-line: param-type-mismatch
+ local start_row, start_col, _, end_row, end_col, _ = unpack(region)
+ accum_range(regions, { lang, start_row + 1, start_col, end_row + 1, end_col })
+ end
end
end
end
diff --git a/tests/injected/combined_injections.md.formatted b/tests/injected/combined_injections.md.formatted
index d7f46a4..50dea5f 100644
--- a/tests/injected/combined_injections.md.formatted
+++ b/tests/injected/combined_injections.md.formatted
@@ -1,13 +1,13 @@
text
-<!-- comment -->
+|<!-- comment -->|
```lua
|local foo = 'bar'|
```
-<!-- comment -->
+|<!-- comment -->|
```lua
|local foo = 'bar'|