From 0283cf7ad0f857706aaac9d1f61edddd37ef1506 Mon Sep 17 00:00:00 2001 From: "Gustavo L de Mello (Guz)" Date: Sat, 7 Dec 2024 13:13:29 -0300 Subject: [PATCH] feat(formatting): new formatting configuration --- lua/dot013/plugins/formatting.lua | 90 +++++++++++++++++++++++++++++++ lua/dot013/plugins/init.lua | 1 + neovim.nix | 1 + 3 files changed, 92 insertions(+) create mode 100644 lua/dot013/plugins/formatting.lua diff --git a/lua/dot013/plugins/formatting.lua b/lua/dot013/plugins/formatting.lua new file mode 100644 index 0000000..84bbfd1 --- /dev/null +++ b/lua/dot013/plugins/formatting.lua @@ -0,0 +1,90 @@ +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 then + table.insert(f, "deno_fmt") + end + + return f +end + +local formatters_by_ft = { + ["lua"] = { "stylua" }, + ["nix"] = { "alejandra" }, + ["rust"] = { "rustfmt", lsp_format = "fallback" }, + + ["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, + + ["javascript"] = js_fmt, + ["javascriptreact"] = js_fmt, + ["typescript"] = js_fmt, + ["typescriptreact"] = js_fmt, + ["json"] = function(bufnr) + return { table.unpack(js_fmt(bufnr)), "jq" } + end, + + ["*"] = { "codespell" }, + ["_"] = { "trim_whitespace" }, +} + +-- go = { +-- function() +-- if vim.fn.executable("gofumpt") == 1 then +-- return require("formatter.filetypes.go").gofumpt() +-- end +-- return require("formatter.filetypes.go").gofmt() +-- end, +-- function() +-- if vim.fn.executable("golines") == 1 then +-- return require("formatter.filetypes.go").golines() +-- elseif vim.fn.executable("goimports") == 1 then +-- return require("formatter.filetypes.go").goimports() +-- end +-- return nil +-- end, +-- }, +return { + "conform.nvim", + ft = (function() + local fts = {} + for k, _ in pairs(formatters_by_ft) do + table.insert(fts, k) + end + return fts + end)(), + after = function() + require("conform").setup({ + formatters_by_ft = formatters_by_ft, + format_on_save = { + timeout_ms = 500, + lsp_format = "fallback", + }, + }) + end, +} diff --git a/lua/dot013/plugins/init.lua b/lua/dot013/plugins/init.lua index c16d908..5d4772e 100644 --- a/lua/dot013/plugins/init.lua +++ b/lua/dot013/plugins/init.lua @@ -1,6 +1,7 @@ return { { import = "dot013.plugins.appearance" }, { import = "dot013.plugins.debugger" }, + { import = "dot013.plugins.formatting" }, { import = "dot013.plugins.ide" }, { import = "dot013.plugins.lsp" }, { import = "dot013.plugins.navigation" }, diff --git a/neovim.nix b/neovim.nix index ecabf43..dad29cc 100644 --- a/neovim.nix +++ b/neovim.nix @@ -29,6 +29,7 @@ auto-session blink-cmp cloak-nvim + conform-nvim friendly-snippets gitsigns-nvim harpoon2