feat(extras): added conform.nvim extra you can use instead of null-ls/none-ls

This commit is contained in:
Folke Lemaitre 2023-09-29 13:37:00 +02:00
parent 6b05ed7dcd
commit f61a243d1a
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040
6 changed files with 115 additions and 0 deletions

View file

@ -0,0 +1,47 @@
-- When `conform.nvim` is enabled, it will automatically be used as the
-- formatter for all files that it supports.
-- When no conform formatter is available for a filetype, LSP format
-- will be used instead.
--
-- LazyVim adds a custom `formatter_opts` option to allow overriding the
-- default options for each formatter.
return {
{
"stevearc/conform.nvim",
dependencies = { "mason.nvim" },
lazy = true,
cmd = "ConformInfo",
init = function()
-- Install the conform formatter on VeryLazy
require("lazyvim.util").on_very_lazy(function()
require("lazyvim.plugins.lsp.format").custom_format = function(buf)
return require("conform").format({ bufnr = buf })
end
end)
end,
opts = {
formatters_by_ft = {
lua = { "stylua" },
fish = { "fish_indent" },
sh = { "shfmt" },
},
-- LazyVim extension to easily override formatter options
---@type table<string,table>
formatter_opts = {
-- -- Example of using dprint only when a dprint.json file is present
-- dprint = {
-- condition = function(ctx)
-- return vim.fs.find({ "dprint.json" }, { path = ctx.filename, upward = true })[1]
-- end,
-- },
},
},
config = function(_, opts)
opts.formatters = opts.formatters or {}
for f, o in pairs(opts.formatter_opts or {}) do
opts.formatters[f] = vim.tbl_deep_extend("force", require("conform.formatters." .. f), o)
end
require("conform").setup(opts)
end,
},
}

View file

@ -13,4 +13,28 @@ return {
table.insert(opts.sources, nls.builtins.formatting.prettierd) table.insert(opts.sources, nls.builtins.formatting.prettierd)
end, end,
}, },
{
"stevearc/conform.nvim",
optional = true,
opts = {
formatters_by_ft = {
["javascript"] = { { "prettierd", "prettier" } },
["javascriptreact"] = { { "prettierd", "prettier" } },
["typescript"] = { { "prettierd", "prettier" } },
["typescriptreact"] = { { "prettierd", "prettier" } },
["vue"] = { { "prettierd", "prettier" } },
["css"] = { { "prettierd", "prettier" } },
["scss"] = { { "prettierd", "prettier" } },
["less"] = { { "prettierd", "prettier" } },
["html"] = { { "prettierd", "prettier" } },
["json"] = { { "prettierd", "prettier" } },
["jsonc"] = { { "prettierd", "prettier" } },
["yaml"] = { { "prettierd", "prettier" } },
["markdown"] = { { "prettierd", "prettier" } },
["markdown.mdx"] = { { "prettierd", "prettier" } },
["graphql"] = { { "prettierd", "prettier" } },
["handlebars"] = { { "prettierd", "prettier" } },
},
},
},
} }

View file

@ -97,6 +97,15 @@ return {
end end
end, end,
}, },
{
"stevearc/conform.nvim",
optional = true,
opts = {
formatters_by_ft = {
go = { "goimports", "gofumpt" },
},
},
},
{ {
"mfussenegger/nvim-dap", "mfussenegger/nvim-dap",
optional = true, optional = true,

View file

@ -16,6 +16,21 @@ return {
table.insert(opts.sources, nls.builtins.formatting.csharpier) table.insert(opts.sources, nls.builtins.formatting.csharpier)
end, end,
}, },
{
"stevearc/conform.nvim",
optional = true,
opts = {
formatters_by_ft = {
cs = { "csharpier" },
},
formatters = {
csharpier = {
command = "dotnet-csharpier",
args = { "--write-stdout" },
},
},
},
},
{ {
"williamboman/mason.nvim", "williamboman/mason.nvim",
opts = function(_, opts) opts = function(_, opts)

View file

@ -37,4 +37,15 @@ return {
end end
end, end,
}, },
{
"stevearc/conform.nvim",
optional = true,
opts = {
formatters_by_ft = {
terraform = { "terraform_fmt" },
tf = { "terraform_fmt" },
["terraform-vars"] = { "terraform_fmt" },
},
},
},
} }

View file

@ -5,6 +5,11 @@ local M = {}
---@type PluginLspOpts ---@type PluginLspOpts
M.opts = nil M.opts = nil
-- Allow plugins to set a custom formatter for a buffer.
-- See the conform extra for an example.
---@type fun(bufnr:number):boolean
M.custom_format = nil
function M.enabled() function M.enabled()
return M.opts.autoformat return M.opts.autoformat
end end
@ -30,6 +35,10 @@ function M.format(opts)
return return
end end
if M.custom_format and M.custom_format(buf) then
return
end
local formatters = M.get_formatters(buf) local formatters = M.get_formatters(buf)
local client_ids = vim.tbl_map(function(client) local client_ids = vim.tbl_map(function(client)
return client.id return client.id