From a8057cb6a9076c6e60044e8e714d6f443b5b729d Mon Sep 17 00:00:00 2001 From: Mikkel Svartveit Date: Thu, 29 May 2025 13:18:17 +0200 Subject: [PATCH] Set up format on save --- nvim/lazy-lock.json | 1 + nvim/lua/plugins.lua | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/nvim/lazy-lock.json b/nvim/lazy-lock.json index 9c8ec8f..521110b 100644 --- a/nvim/lazy-lock.json +++ b/nvim/lazy-lock.json @@ -10,6 +10,7 @@ "mason-lspconfig.nvim": { "branch": "main", "commit": "67da97f8c2fd12d05427bb485ce07ee6418e0a51" }, "mason.nvim": { "branch": "main", "commit": "8024d64e1330b86044fed4c8494ef3dcd483a67c" }, "neoscroll.nvim": { "branch": "master", "commit": "f957373912e88579e26fdaea4735450ff2ef5c9c" }, + "none-ls.nvim": { "branch": "main", "commit": "db2a48b79cfcdab8baa5d3f37f21c78b6705c62e" }, "nvim-autopairs": { "branch": "master", "commit": "4d74e75913832866aa7de35e4202463ddf6efd1b" }, "nvim-cmp": { "branch": "main", "commit": "b5311ab3ed9c846b585c0c15b7559be131ec4be9" }, "nvim-lspconfig": { "branch": "master", "commit": "b8e7957bde4cbb3cb25a13a62548f7c273b026e9" }, diff --git a/nvim/lua/plugins.lua b/nvim/lua/plugins.lua index 1fd0b19..f1d1319 100644 --- a/nvim/lua/plugins.lua +++ b/nvim/lua/plugins.lua @@ -96,6 +96,40 @@ require("lazy").setup({ end, }, + -- Formatting with null-ls + { + "nvimtools/none-ls.nvim", + dependencies = { "nvim-lua/plenary.nvim" }, + config = function() + local null_ls = require("null-ls") + local augroup = vim.api.nvim_create_augroup("LspFormatting", {}) + null_ls.setup({ + sources = { + null_ls.builtins.formatting.prettierd, + null_ls.builtins.formatting.black, + null_ls.builtins.formatting.stylua, + }, + 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({ + async = false, + filter = function(client) + return client.name == "null-ls" + end + }) + end, + }) + end + end, + }) + end, + }, + -- Easy commenting/uncommenting { "numToStr/Comment.nvim",