From 4fb80c55df658bf4bc2aa5bf5eab9b123de9ef1d Mon Sep 17 00:00:00 2001 From: psychhim Date: Sun, 21 Sep 2025 18:12:55 +0530 Subject: [PATCH] added auto-format on save using Conform --- init.lua | 123 +++++++++++++++++++++++++++++++------------------ lazy-lock.json | 10 ++-- 2 files changed, 84 insertions(+), 49 deletions(-) diff --git a/init.lua b/init.lua index 0649fce..5728202 100644 --- a/init.lua +++ b/init.lua @@ -27,8 +27,8 @@ vim.opt.rtp:prepend(lazypath) do local orig_notify = vim.notify vim.notify = function(msg, ...) - if type(msg) == "string" and msg:match("which%-key") then - return -- ignore WhichKey health messages + if type(msg) == 'string' and msg:match 'which%-key' then + return -- ignore WhichKey health messages end return orig_notify(msg, ...) end @@ -53,6 +53,40 @@ require('lazy').setup({ 'williamboman/mason.nvim', 'williamboman/mason-lspconfig.nvim', + -- Conform + { + 'stevearc/conform.nvim', + opts = { + -- Set formatters per filetype + formatters_by_ft = { + lua = { 'stylua' }, + python = { 'black', 'isort' }, + javascript = { 'prettierd', 'prettier' }, + typescript = { 'prettierd', 'prettier' }, + css = { 'prettier' }, + html = { 'prettier' }, + json = { 'prettier' }, + c = { 'clang-format' }, + cpp = { 'clang-format' }, + bash = { 'shfmt' }, + rust = { 'rustfmt' }, + }, + format_on_save = { + timeout_ms = 500, + lsp_format = 'fallback', -- fall back to LSP if no external formatter + }, + }, + }, + -- Install formatters automatically + { + 'zapling/mason-conform.nvim', + dependencies = { 'williamboman/mason.nvim', 'stevearc/conform.nvim' }, + config = function() + require('mason-conform').setup { + ensure_installed = true, -- auto-install all formatters listed in Conform + } + end, + }, -- Useful status updates for LSP -- NOTE: `opts = {}` is the same as calling `require('fidget').setup({})` { 'j-hui/fidget.nvim', opts = {} }, @@ -66,7 +100,7 @@ require('lazy').setup({ dependencies = { -- Snippet Engine & its associated nvim-cmp source 'L3MON4D3/LuaSnip', - dependencies = { "rafamadriz/friendly-snippets" }, + dependencies = { 'rafamadriz/friendly-snippets' }, 'saadparwaiz1/cmp_luasnip', -- Adds LSP completion capabilities @@ -79,7 +113,7 @@ require('lazy').setup({ }, }, -- Useful plugin to show you pending keybinds. - { 'folke/which-key.nvim', opts = {} }, + { 'folke/which-key.nvim', opts = {} }, { -- Adds git related signs to the gutter, as well as utilities for managing changes 'lewis6991/gitsigns.nvim', @@ -156,7 +190,7 @@ require('lazy').setup({ }, { --kanagawa colorscheme - "rebelot/kanagawa.nvim", + 'rebelot/kanagawa.nvim', lazy = false, priority = 1000, opts = {}, @@ -216,8 +250,8 @@ require('lazy').setup({ }, { --Autopairs 'windwp/nvim-autopairs', - event = "InsertEnter", - config = true + event = 'InsertEnter', + config = true, -- use opts = {} for passing setup options -- this is equalent to setup({}) function }, @@ -236,7 +270,6 @@ require('lazy').setup({ { import = 'custom.plugins' }, }, {}) - -- [[ Setting options ]] -- See `:help vim.o` -- NOTE: You can change these options as you wish! @@ -303,67 +336,67 @@ vim.keymap.set('n', 'n', 'cd %:p:h | Neotree toggle float') vim.keymap.set('n', 't', 'tabnew +termi') -- Create an empty buffer in a new tab -vim.keymap.set("n", "e", function() - vim.cmd("tabnew") -- create a new tab - vim.cmd("enew") -- create a new empty buffer in it +vim.keymap.set('n', 'e', function() + vim.cmd 'tabnew' -- create a new tab + vim.cmd 'enew' -- create a new empty buffer in it end, { noremap = true, silent = true }) -- Save current buffer (asks for filename if new/unsaved) vim.keymap.set('n', 'w', function() - if vim.api.nvim_buf_get_name(0) == "" then + if vim.api.nvim_buf_get_name(0) == '' then -- Ask user for a filename - local filename = vim.fn.input("Save as: ", "", "file") - if filename ~= "" then - vim.cmd("saveas " .. vim.fn.fnameescape(filename)) + local filename = vim.fn.input('Save as: ', '', 'file') + if filename ~= '' then + vim.cmd('saveas ' .. vim.fn.fnameescape(filename)) else - print("Save cancelled") + print 'Save cancelled' end else - vim.cmd("w") + vim.cmd 'w' end -end, { desc = "Save buffer (prompt if new file)" }) +end, { desc = 'Save buffer (prompt if new file)' }) -- Close current window (asks if buffer is unsaved) vim.keymap.set('n', 'q', function() if vim.bo.modified then - local choice = vim.fn.input("Buffer modified! Save (y), Discard (n), Cancel (any other key)? ") - if choice:lower() == "y" then - if vim.api.nvim_buf_get_name(0) == "" then - local filename = vim.fn.input("Save as: ", "", "file") - if filename ~= "" then - vim.cmd("saveas " .. vim.fn.fnameescape(filename)) - vim.cmd("q") + local choice = vim.fn.input 'Buffer modified! Save (y), Discard (n), Cancel (any other key)? ' + if choice:lower() == 'y' then + if vim.api.nvim_buf_get_name(0) == '' then + local filename = vim.fn.input('Save as: ', '', 'file') + if filename ~= '' then + vim.cmd('saveas ' .. vim.fn.fnameescape(filename)) + vim.cmd 'q' else - print("Save cancelled") + print 'Save cancelled' end else - vim.cmd("wq") + vim.cmd 'wq' end - elseif choice:lower() == "n" then - vim.cmd("q!") + elseif choice:lower() == 'n' then + vim.cmd 'q!' else - print("Quit cancelled") + print 'Quit cancelled' end else - vim.cmd("q") + vim.cmd 'q' end -end, { desc = "Close buffer (prompt if modified)" }) +end, { desc = 'Close buffer (prompt if modified)' }) -- Save changes and close current window (asks for filename if new/unsaved) vim.keymap.set('n', 'qy', function() - if vim.api.nvim_buf_get_name(0) == "" then + if vim.api.nvim_buf_get_name(0) == '' then -- Ask user for a filename - local filename = vim.fn.input("Save as: ", "", "file") - if filename ~= "" then - vim.cmd("saveas " .. vim.fn.fnameescape(filename)) - vim.cmd("q") + local filename = vim.fn.input('Save as: ', '', 'file') + if filename ~= '' then + vim.cmd('saveas ' .. vim.fn.fnameescape(filename)) + vim.cmd 'q' else - print("Save cancelled") + print 'Save cancelled' end else - vim.cmd("wq") + vim.cmd 'wq' end -end, { desc = "Save & quit (prompt if new file)" }) +end, { desc = 'Save & quit (prompt if new file)' }) --Discard changes and Close current window vim.keymap.set('n', 'qn', 'q!') @@ -573,10 +606,10 @@ local on_attach = function(_, bufnr) print(vim.inspect(vim.lsp.buf.list_workspace_folders())) end, '[W]orkspace [L]ist Folders') - -- Create a command `:Format` local to the LSP buffer - vim.api.nvim_buf_create_user_command(bufnr, 'Format', function(_) - vim.lsp.buf.format() - end, { desc = 'Format current buffer with LSP' }) + -- Create a command `:Format` local to the buffer using Conform + vim.api.nvim_buf_create_user_command(bufnr, 'Format', function() + require('conform').format { bufnr = bufnr } + end, { desc = 'Format current buffer with Conform' }) end -- document existing key chains @@ -712,7 +745,7 @@ cmp.setup { require('luasnip.loaders.from_vscode').lazy_load() -- Theme -require('kanagawa').setup({ transparent = true }) +require('kanagawa').setup { transparent = true } vim.cmd [[colorscheme kanagawa]] -- The line beneath this is called `modeline`. See `:help modeline` -- vim: ts=2 sts=2 sw=2 et diff --git a/lazy-lock.json b/lazy-lock.json index e2c7b2e..e6b2bf8 100644 --- a/lazy-lock.json +++ b/lazy-lock.json @@ -1,9 +1,10 @@ { "Comment.nvim": { "branch": "master", "commit": "e30b7f2008e52442154b66f7c519bfd2f1e32acb" }, - "LuaSnip": { "branch": "master", "commit": "21f74f7ba8c49f95f9d7c8293b147c2901dd2d3a" }, + "LuaSnip": { "branch": "master", "commit": "b3104910bb5ebf40492aadffae18f2528fa757d9" }, "cmp-nvim-lsp": { "branch": "main", "commit": "bd5a7d6db125d4654b50eeae9f5217f24bb22fd3" }, "cmp-path": { "branch": "main", "commit": "c642487086dbd9a93160e1679a1327be111cbc25" }, "cmp_luasnip": { "branch": "master", "commit": "98d9cb5c2c38532bd9bdb481067b20fea8f32e90" }, + "conform.nvim": { "branch": "master", "commit": "b4aab989db276993ea5dcb78872be494ce546521" }, "fidget.nvim": { "branch": "main", "commit": "4d5858bd4c471c895060e1b9f3575f1551184dc5" }, "friendly-snippets": { "branch": "main", "commit": "572f5660cf05f8cd8834e096d7b4c921ba18e175" }, "gitsigns.nvim": { "branch": "main", "commit": "f780609807eca1f783a36a8a31c30a48fbe150c5" }, @@ -11,14 +12,15 @@ "kanagawa.nvim": { "branch": "master", "commit": "debe91547d7fb1eef34ce26a5106f277fbfdd109" }, "lazy.nvim": { "branch": "main", "commit": "6c3bda4aca61a13a9c63f1c1d1b16b9d3be90d7a" }, "lualine.nvim": { "branch": "master", "commit": "b8c23159c0161f4b89196f74ee3a6d02cdc3a955" }, - "mason-lspconfig.nvim": { "branch": "main", "commit": "7f9a39fcd2ac6e979001f857727d606888f5909c" }, + "mason-conform.nvim": { "branch": "main", "commit": "48da2ebd5efbaf8a99566eadc3ece82d523f03c5" }, + "mason-lspconfig.nvim": { "branch": "main", "commit": "a1067cf84b4ff81b66d2bf4d01f4cbdb5de40bd0" }, "mason.nvim": { "branch": "main", "commit": "7dc4facca9702f95353d5a1f87daf23d78e31c2a" }, - "neo-tree.nvim": { "branch": "main", "commit": "f1deac7ecec88c28a250d890ba7bb35843e69cbd" }, + "neo-tree.nvim": { "branch": "main", "commit": "ed057048a281b418d5318dd5153f9486daa517a3" }, "neodev.nvim": { "branch": "main", "commit": "46aa467dca16cf3dfe27098042402066d2ae242d" }, "nui.nvim": { "branch": "main", "commit": "de740991c12411b663994b2860f1a4fd0937c130" }, "nvim-autopairs": { "branch": "master", "commit": "23320e75953ac82e559c610bec5a90d9c6dfa743" }, "nvim-cmp": { "branch": "main", "commit": "b5311ab3ed9c846b585c0c15b7559be131ec4be9" }, - "nvim-lspconfig": { "branch": "master", "commit": "d89f4891f0720cd2598e4bdd60010d8784b2ac8a" }, + "nvim-lspconfig": { "branch": "master", "commit": "107c2458cdc780c4ed2c2b5e1b7800cd019010bd" }, "nvim-treesitter": { "branch": "master", "commit": "42fc28ba918343ebfd5565147a42a26580579482" }, "nvim-treesitter-textobjects": { "branch": "master", "commit": "71385f191ec06ffc60e80e6b0c9a9d5daed4824c" }, "nvim-web-devicons": { "branch": "master", "commit": "6e51ca170563330e063720449c21f43e27ca0bc1" },