local null_ls = require("null-ls") local augroup = vim.api.nvim_create_augroup("LspFormatting", {}) local opts = { sources = { -- Lua null_ls.builtins.formatting.stylua, -- Python null_ls.builtins.formatting.black, -- null_ls.builtins.diagnostics.ruff, -- Javascript -- null_ls.builtins.diagnostics.eslint_d, -- null_ls.builtins.diagnostics.jsonlint, -- C/C++ null_ls.builtins.formatting.clang_format, -- Nix null_ls.builtins.formatting.alejandra, -- Rust -- null_ls.builtins.formatting.rustfmt, -- YAML null_ls.builtins.formatting.yamlfmt, -- Typescript null_ls.builtins.formatting.prettier.with({ condition = function(utils) return utils.has_file({ ".prettierrc.js" }) end, }), }, on_attach = function(client, bufnr) if client.supports_method("textDocument/formatting") then vim.api.nvim_clear_autocmds({ group = augroup, buffer = bufnr, }) vim.api.nvim_create_autocmd("BufWritePre", { group = augroup, buffer = bufnr, callback = function() vim.lsp.buf.format({ bufnr = bufnr }) end, }) end end, } null_ls.setup(opts) vim.keymap.set("n", "gf", vim.lsp.buf.format, {})