diff --git a/lua/lazyvim/plugins/extras/formatting/conform.lua b/lua/lazyvim/plugins/extras/formatting/conform.lua new file mode 100644 index 00000000..9858c08e --- /dev/null +++ b/lua/lazyvim/plugins/extras/formatting/conform.lua @@ -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 + 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, + }, +} diff --git a/lua/lazyvim/plugins/extras/formatting/prettier.lua b/lua/lazyvim/plugins/extras/formatting/prettier.lua index 3e4d461f..81fc1939 100644 --- a/lua/lazyvim/plugins/extras/formatting/prettier.lua +++ b/lua/lazyvim/plugins/extras/formatting/prettier.lua @@ -13,4 +13,28 @@ return { table.insert(opts.sources, nls.builtins.formatting.prettierd) 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" } }, + }, + }, + }, } diff --git a/lua/lazyvim/plugins/extras/lang/go.lua b/lua/lazyvim/plugins/extras/lang/go.lua index c2946adb..0b0cab70 100644 --- a/lua/lazyvim/plugins/extras/lang/go.lua +++ b/lua/lazyvim/plugins/extras/lang/go.lua @@ -97,6 +97,15 @@ return { end end, }, + { + "stevearc/conform.nvim", + optional = true, + opts = { + formatters_by_ft = { + go = { "goimports", "gofumpt" }, + }, + }, + }, { "mfussenegger/nvim-dap", optional = true, diff --git a/lua/lazyvim/plugins/extras/lang/omnisharp.lua b/lua/lazyvim/plugins/extras/lang/omnisharp.lua index 14f9e34d..d244fd77 100644 --- a/lua/lazyvim/plugins/extras/lang/omnisharp.lua +++ b/lua/lazyvim/plugins/extras/lang/omnisharp.lua @@ -16,6 +16,21 @@ return { table.insert(opts.sources, nls.builtins.formatting.csharpier) end, }, + { + "stevearc/conform.nvim", + optional = true, + opts = { + formatters_by_ft = { + cs = { "csharpier" }, + }, + formatters = { + csharpier = { + command = "dotnet-csharpier", + args = { "--write-stdout" }, + }, + }, + }, + }, { "williamboman/mason.nvim", opts = function(_, opts) diff --git a/lua/lazyvim/plugins/extras/lang/terraform.lua b/lua/lazyvim/plugins/extras/lang/terraform.lua index 182c9f52..cb3aeaa2 100644 --- a/lua/lazyvim/plugins/extras/lang/terraform.lua +++ b/lua/lazyvim/plugins/extras/lang/terraform.lua @@ -37,4 +37,15 @@ return { end end, }, + { + "stevearc/conform.nvim", + optional = true, + opts = { + formatters_by_ft = { + terraform = { "terraform_fmt" }, + tf = { "terraform_fmt" }, + ["terraform-vars"] = { "terraform_fmt" }, + }, + }, + }, } diff --git a/lua/lazyvim/plugins/lsp/format.lua b/lua/lazyvim/plugins/lsp/format.lua index 64c4becf..f862a8a7 100644 --- a/lua/lazyvim/plugins/lsp/format.lua +++ b/lua/lazyvim/plugins/lsp/format.lua @@ -5,6 +5,11 @@ local M = {} ---@type PluginLspOpts 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() return M.opts.autoformat end @@ -30,6 +35,10 @@ function M.format(opts) return end + if M.custom_format and M.custom_format(buf) then + return + end + local formatters = M.get_formatters(buf) local client_ids = vim.tbl_map(function(client) return client.id