Skip to content

Commit

Permalink
fix: injected formatter works for bash
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc committed Dec 21, 2024
1 parent f4e8837 commit 62055b4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 4 additions & 0 deletions doc/formatter_options.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ require("conform").formatters.injected = {
options = {
-- Set to true to ignore errors
ignore_errors = false,
-- Map of treesitter language to filetype
lang_to_ft = {
bash = "sh",
},
-- Map of treesitter language to file extension
-- A temporary file name with this extension will be generated during formatting
-- because some formatters care about the filename.
Expand Down
12 changes: 10 additions & 2 deletions lua/conform/formatters/injected.lua
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ end
---@class (exact) conform.InjectedFormatterOptions
---@field ignore_errors boolean
---@field lang_to_ext table<string, string>
---@field lang_to_ft table<string, string>
---@field lang_to_formatters table<string, conform.FiletypeFormatter>

---@type conform.FileLuaFormatterConfig
Expand All @@ -116,6 +117,10 @@ return {
options = {
-- Set to true to ignore errors
ignore_errors = false,
-- Map of treesitter language to filetype
lang_to_ft = {
bash = "sh",
},
-- Map of treesitter language to file extension
-- A temporary file name with this extension will be generated during formatting
-- because some formatters care about the filename.
Expand Down Expand Up @@ -163,7 +168,8 @@ return {
---@param lang string
---@return nil|conform.FiletypeFormatter
local function get_formatters(lang)
return options.lang_to_formatters[lang] or conform.formatters_by_ft[lang]
local ft = options.lang_to_ft[lang] or lang
return options.lang_to_formatters[ft] or conform.formatters_by_ft[ft]
end

--- Disable diagnostic to pass the typecheck github action
Expand All @@ -180,7 +186,9 @@ return {
for _, ranges in ipairs(lang_tree:included_regions()) do
for _, region in ipairs(ranges) do
local formatters = get_formatters(lang)
if formatters ~= nil then
if formatters == nil then
log.info("No formatters found for injected treesitter language %s", lang)
else
-- 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)
Expand Down

0 comments on commit 62055b4

Please sign in to comment.