diff --git a/lua/dot/init.lua b/lua/dot/init.lua index 2187b31..96e4a9d 100644 --- a/lua/dot/init.lua +++ b/lua/dot/init.lua @@ -94,6 +94,13 @@ vim.api.nvim_create_autocmd("TextYankPost", { end, }) +vim.api.nvim_create_autocmd("BufWritePre", { + pattern = "*", + callback = function(e) + require("conform").format({ bufnr = e.buf }) + end, +}) + vim.api.nvim_create_autocmd("LspAttach", { group = vim.api.nvim_create_augroup("dot-lsp-attach-autocmds", { clear = true }), callback = function(e) diff --git a/lua/dot/plugins.lua b/lua/dot/plugins.lua index 4a469f2..609c27d 100644 --- a/lua/dot/plugins.lua +++ b/lua/dot/plugins.lua @@ -20,6 +20,119 @@ lze.load({ }, { "lazydev.nvim", on_require = "lazydev" }, { import = "dot.lsp" }, + + -- Formatting + { + "conform.nvim", + after = function() + local function js_fmt(bufnr) + local f = {} + + if require("conform").get_formatter_info("prettierd", bufnr).available then + table.insert(f, "prettierd") + elseif require("conform").get_formatter_info("prettier", bufnr).available then + table.insert(f, "prettier") + end + + if require("conform").get_formatter_info("eslint_d", bufnr).available then + table.insert(f, "eslint_d") + end + + if + require("conform").get_formatter_info("deno_fmt", bufnr).available + and ( + require("dot013.utils").is_in_cwd("deno.json") + or require("dot013.utils").is_in_cwd("deno.jsonc") + ) + then + table.insert(f, "deno_fmt") + end + + return f + end + require("conform").setup({ + formatters_by_ft = { + -- Simple formatters + lua = { "stylua" }, + nix = { "alejandra", stop_after_first = true }, + rust = { "rustfmt", lsp_format = "fallback" }, + sh = { "shellharden", "shfmt" }, + xml = { "xmllint", "xmltidy" }, + xhtml = { "xmllint", "xmltidy" }, + markdown = { "mdfmt" }, + + html = { "prettierd", "prettier", stop_after_first = true }, + css = { "prettierd", "prettier", stop_after_first = true }, + scss = { "prettierd", "prettier", stop_after_first = true }, + less = { "prettierd", "prettier", stop_after_first = true }, + yaml = { "prettierd", "prettier", stop_after_first = true }, + + gdscript = { "gdformat" }, + -- Golang's formatters used by priority + go = function(bufnr) + local f = {} + + if require("conform").get_formatter_info("gofumpt", bufnr).available then + table.insert(f, "gofumpt") + else + table.insert(f, "gofmt") + end + + if require("conform").get_formatter_info("golines", bufnr).available then + table.insert(f, "golines") + elseif require("conform").get_formatter_info("goimports", bufnr).available then + table.insert(f, "goimports") + end + + return f + end, + templ = { "templ" }, + + -- JavaScript's ecosystem + javascript = js_fmt, + javascriptreact = js_fmt, + typescript = js_fmt, + typescriptreact = js_fmt, + json = function(bufnr) + local fmts = js_fmt(bufnr) + if fmts then + return fmts + end + return { "jq" } + end, + + -- Fallback for any filetype + ["*"] = { "codespell" }, + ["_"] = { "trim_whitespace" }, + }, + format_on_save = { + timeout_ms = 500, + lsp_format = "fallback", + }, + formatters = { + mdfmt = { + command = "mdfmt", + }, + xmltidy = { + inherit = false, + -- Uses HTML Tidy + command = "tidy", + args = { "-xml", "-indent", "yes", "2", "-wrap", "100", "-" }, + }, + --[[ nixcmdfmt = { + inherit = false, + stdin = false, + cwd = require("conform.util").root_file({ "flake.nix" }), + require_cwd = true, + command = "nix", + args = { "fmt", "$FILENAME" }, + }, ]] + }, + }) + end, + on_require = "conform", + }, + -- Fuzzy Finding { "telescope.nvim",