From 0ef09ea16800a63d8cfe6ba76a6a4dc10ff601aa Mon Sep 17 00:00:00 2001 From: Jeremy Kaplan Date: Tue, 24 Sep 2024 12:49:00 -0400 Subject: [PATCH] neovim: Format-on-save for Prettier --- neovim/lazy-lock.json | 1 + neovim/lua/plugins/ide.lua | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/neovim/lazy-lock.json b/neovim/lazy-lock.json index 2910676..883d3f9 100644 --- a/neovim/lazy-lock.json +++ b/neovim/lazy-lock.json @@ -12,6 +12,7 @@ "mason.nvim": { "branch": "main", "commit": "0950b15060067f752fde13a779a994f59516ce3d" }, "nerdtree": { "branch": "master", "commit": "fbb71fcd90602e3ec77f40b864b5f9b437c496c5" }, "nim.nvim": { "branch": "master", "commit": "076239e8869e3e9b061b17cbca2cea2df73d5f92" }, + "none-ls.nvim": { "branch": "main", "commit": "5f041cfc2d763b2ba69b848854240308e0d88d5b" }, "nui.nvim": { "branch": "main", "commit": "322978c734866996274467de084a95e4f9b5e0b1" }, "nvim-autopairs": { "branch": "master", "commit": "c15de7e7981f1111642e7e53799e1211d4606cb9" }, "nvim-cmp": { "branch": "main", "commit": "a110e12d0b58eefcf5b771f533fc2cf3050680ac" }, diff --git a/neovim/lua/plugins/ide.lua b/neovim/lua/plugins/ide.lua index f6e2fdb..03db5dc 100644 --- a/neovim/lua/plugins/ide.lua +++ b/neovim/lua/plugins/ide.lua @@ -306,6 +306,44 @@ return { "alaviss/nim.nvim", "simrat39/rust-tools.nvim", "neovim/nvim-lspconfig", + { + "nvimtools/none-ls.nvim", + config = function(_plugin, opts) + local null_ls = require("null-ls") + + local group = vim.api.nvim_create_augroup("lsp_format_on_save", { clear = false }) + + null_ls.setup({ + sources = { + null_ls.builtins.formatting.prettier, + }, + on_attach = function(client, bufnr) + if client.supports_method("textDocument/formatting") then + vim.keymap.set("n", "f", function() + vim.lsp.buf.format({ bufnr = vim.api.nvim_get_current_buf() }) + end, { buffer = bufnr, desc = "[lsp] format" }) + + -- format on save + vim.api.nvim_clear_autocmds({ buffer = bufnr, group = group }) + vim.api.nvim_create_autocmd("BufWritePre", { + buffer = bufnr, + group = group, + callback = function() + vim.lsp.buf.format({ bufnr = bufnr, async = false }) + end, + desc = "[lsp] format on save", + }) + end + + if client.supports_method("textDocument/rangeFormatting") then + vim.keymap.set("x", "f", function() + vim.lsp.buf.format({ bufnr = vim.api.nvim_get_current_buf() }) + end, { buffer = bufnr, desc = "[lsp] format" }) + end + end, + }) + end, + }, { "williamboman/mason.nvim", build = function(_plugin)