Files
LazyVim/lua/lazyvim/plugins/extras/formatting/prettier.lua
Rick Harris 4ff36062dd fix(prettier): keep existing formatters_by_ft (#4719)
## Description

<!-- Describe the big picture of your changes to communicate to the
maintainers
  why we should accept this pull request. -->

This change makes it possible to configure other formatters for
filetypes supported by the prettier extra, for instance

```js
return {
  {
    "stevearc/conform.nvim",
    opts = {
      formatters_by_ft = {
        css = { "stylelint" },
      },
    },
  },
}
```

Currently the prettier extra overwrites any existing `formatters_by_ft`
for the filetypes it supports. I've changed it to use `table.insert`
[like the sql extra
does](75750be1c0/lua/lazyvim/plugins/extras/lang/sql.lua (L148-L149)).

## Related Issue(s)

<!--
  If this PR fixes any issues, please link to the issue here.
  - Fixes #<issue_number>
-->

None that I know of

## Screenshots

<!-- Add screenshots of the changes if applicable. -->

N/A

## Checklist

- [x] I've read the
[CONTRIBUTING](https://github.com/LazyVim/LazyVim/blob/main/CONTRIBUTING.md)
guidelines.
2024-11-07 16:11:36 +01:00

97 lines
2.5 KiB
Lua

---@diagnostic disable: inject-field
if lazyvim_docs then
-- Enable the option to require a Prettier config file
-- If no prettier config file is found, the formatter will not be used
vim.g.lazyvim_prettier_needs_config = false
end
---@alias ConformCtx {buf: number, filename: string, dirname: string}
local M = {}
local supported = {
"css",
"graphql",
"handlebars",
"html",
"javascript",
"javascriptreact",
"json",
"jsonc",
"less",
"markdown",
"markdown.mdx",
"scss",
"typescript",
"typescriptreact",
"vue",
"yaml",
}
--- Checks if a Prettier config file exists for the given context
---@param ctx ConformCtx
function M.has_config(ctx)
vim.fn.system({ "prettier", "--find-config-path", ctx.filename })
return vim.v.shell_error == 0
end
--- Checks if a parser can be inferred for the given context:
--- * If the filetype is in the supported list, return true
--- * Otherwise, check if a parser can be inferred
---@param ctx ConformCtx
function M.has_parser(ctx)
local ft = vim.bo[ctx.buf].filetype --[[@as string]]
-- default filetypes are always supported
if vim.tbl_contains(supported, ft) then
return true
end
-- otherwise, check if a parser can be inferred
local ret = vim.fn.system({ "prettier", "--file-info", ctx.filename })
---@type boolean, string?
local ok, parser = pcall(function()
return vim.fn.json_decode(ret).inferredParser
end)
return ok and parser and parser ~= vim.NIL
end
M.has_config = LazyVim.memoize(M.has_config)
M.has_parser = LazyVim.memoize(M.has_parser)
return {
{
"williamboman/mason.nvim",
opts = { ensure_installed = { "prettier" } },
},
-- conform
{
"stevearc/conform.nvim",
optional = true,
---@param opts ConformOpts
opts = function(_, opts)
opts.formatters_by_ft = opts.formatters_by_ft or {}
for _, ft in ipairs(supported) do
opts.formatters_by_ft[ft] = opts.formatters_by_ft[ft] or {}
table.insert(opts.formatters_by_ft[ft], "prettier")
end
opts.formatters = opts.formatters or {}
opts.formatters.prettier = {
condition = function(_, ctx)
return M.has_parser(ctx) and (vim.g.lazyvim_prettier_needs_config ~= true or M.has_config(ctx))
end,
}
end,
},
-- none-ls support
{
"nvimtools/none-ls.nvim",
optional = true,
opts = function(_, opts)
local nls = require("null-ls")
opts.sources = opts.sources or {}
table.insert(opts.sources, nls.builtins.formatting.prettier)
end,
},
}