added auto-format on save using Conform

This commit is contained in:
psychhim
2025-09-21 18:12:55 +05:30
parent 01f7ed6eff
commit 4fb80c55df
2 changed files with 84 additions and 49 deletions

119
init.lua
View File

@@ -27,7 +27,7 @@ vim.opt.rtp:prepend(lazypath)
do do
local orig_notify = vim.notify local orig_notify = vim.notify
vim.notify = function(msg, ...) vim.notify = function(msg, ...)
if type(msg) == "string" and msg:match("which%-key") then if type(msg) == 'string' and msg:match 'which%-key' then
return -- ignore WhichKey health messages return -- ignore WhichKey health messages
end end
return orig_notify(msg, ...) return orig_notify(msg, ...)
@@ -53,6 +53,40 @@ require('lazy').setup({
'williamboman/mason.nvim', 'williamboman/mason.nvim',
'williamboman/mason-lspconfig.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 -- Useful status updates for LSP
-- NOTE: `opts = {}` is the same as calling `require('fidget').setup({})` -- NOTE: `opts = {}` is the same as calling `require('fidget').setup({})`
{ 'j-hui/fidget.nvim', opts = {} }, { 'j-hui/fidget.nvim', opts = {} },
@@ -66,7 +100,7 @@ require('lazy').setup({
dependencies = { dependencies = {
-- Snippet Engine & its associated nvim-cmp source -- Snippet Engine & its associated nvim-cmp source
'L3MON4D3/LuaSnip', 'L3MON4D3/LuaSnip',
dependencies = { "rafamadriz/friendly-snippets" }, dependencies = { 'rafamadriz/friendly-snippets' },
'saadparwaiz1/cmp_luasnip', 'saadparwaiz1/cmp_luasnip',
-- Adds LSP completion capabilities -- Adds LSP completion capabilities
@@ -156,7 +190,7 @@ require('lazy').setup({
}, },
{ {
--kanagawa colorscheme --kanagawa colorscheme
"rebelot/kanagawa.nvim", 'rebelot/kanagawa.nvim',
lazy = false, lazy = false,
priority = 1000, priority = 1000,
opts = {}, opts = {},
@@ -216,8 +250,8 @@ require('lazy').setup({
}, },
{ --Autopairs { --Autopairs
'windwp/nvim-autopairs', 'windwp/nvim-autopairs',
event = "InsertEnter", event = 'InsertEnter',
config = true config = true,
-- use opts = {} for passing setup options -- use opts = {} for passing setup options
-- this is equalent to setup({}) function -- this is equalent to setup({}) function
}, },
@@ -236,7 +270,6 @@ require('lazy').setup({
{ import = 'custom.plugins' }, { import = 'custom.plugins' },
}, {}) }, {})
-- [[ Setting options ]] -- [[ Setting options ]]
-- See `:help vim.o` -- See `:help vim.o`
-- NOTE: You can change these options as you wish! -- NOTE: You can change these options as you wish!
@@ -303,67 +336,67 @@ vim.keymap.set('n', '<leader>n', '<Cmd>cd %:p:h | Neotree toggle float<CR>')
vim.keymap.set('n', '<leader>t', '<Cmd>tabnew +term<CR>i') vim.keymap.set('n', '<leader>t', '<Cmd>tabnew +term<CR>i')
-- Create an empty buffer in a new tab -- Create an empty buffer in a new tab
vim.keymap.set("n", "<Leader>e", function() vim.keymap.set('n', '<Leader>e', function()
vim.cmd("tabnew") -- create a new tab vim.cmd 'tabnew' -- create a new tab
vim.cmd("enew") -- create a new empty buffer in it vim.cmd 'enew' -- create a new empty buffer in it
end, { noremap = true, silent = true }) end, { noremap = true, silent = true })
-- Save current buffer (asks for filename if new/unsaved) -- Save current buffer (asks for filename if new/unsaved)
vim.keymap.set('n', '<leader>w', function() vim.keymap.set('n', '<leader>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 -- Ask user for a filename
local filename = vim.fn.input("Save as: ", "", "file") local filename = vim.fn.input('Save as: ', '', 'file')
if filename ~= "" then if filename ~= '' then
vim.cmd("saveas " .. vim.fn.fnameescape(filename)) vim.cmd('saveas ' .. vim.fn.fnameescape(filename))
else else
print("Save cancelled") print 'Save cancelled'
end end
else else
vim.cmd("w") vim.cmd 'w'
end 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) -- Close current window (asks if buffer is unsaved)
vim.keymap.set('n', '<leader>q', function() vim.keymap.set('n', '<leader>q', function()
if vim.bo.modified then if vim.bo.modified then
local choice = vim.fn.input("Buffer modified! Save (y), Discard (n), Cancel (any other key)? ") local choice = vim.fn.input 'Buffer modified! Save (y), Discard (n), Cancel (any other key)? '
if choice:lower() == "y" then if choice:lower() == 'y' then
if vim.api.nvim_buf_get_name(0) == "" then if vim.api.nvim_buf_get_name(0) == '' then
local filename = vim.fn.input("Save as: ", "", "file") local filename = vim.fn.input('Save as: ', '', 'file')
if filename ~= "" then if filename ~= '' then
vim.cmd("saveas " .. vim.fn.fnameescape(filename)) vim.cmd('saveas ' .. vim.fn.fnameescape(filename))
vim.cmd("q") vim.cmd 'q'
else else
print("Save cancelled") print 'Save cancelled'
end end
else else
vim.cmd("wq") vim.cmd 'wq'
end end
elseif choice:lower() == "n" then elseif choice:lower() == 'n' then
vim.cmd("q!") vim.cmd 'q!'
else else
print("Quit cancelled") print 'Quit cancelled'
end end
else else
vim.cmd("q") vim.cmd 'q'
end 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) -- Save changes and close current window (asks for filename if new/unsaved)
vim.keymap.set('n', '<leader>qy', function() vim.keymap.set('n', '<leader>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 -- Ask user for a filename
local filename = vim.fn.input("Save as: ", "", "file") local filename = vim.fn.input('Save as: ', '', 'file')
if filename ~= "" then if filename ~= '' then
vim.cmd("saveas " .. vim.fn.fnameescape(filename)) vim.cmd('saveas ' .. vim.fn.fnameescape(filename))
vim.cmd("q") vim.cmd 'q'
else else
print("Save cancelled") print 'Save cancelled'
end end
else else
vim.cmd("wq") vim.cmd 'wq'
end end
end, { desc = "Save & quit (prompt if new file)" }) end, { desc = 'Save & quit (prompt if new file)' })
--Discard changes and Close current window --Discard changes and Close current window
vim.keymap.set('n', '<leader>qn', '<Cmd>q!<CR>') vim.keymap.set('n', '<leader>qn', '<Cmd>q!<CR>')
@@ -573,10 +606,10 @@ local on_attach = function(_, bufnr)
print(vim.inspect(vim.lsp.buf.list_workspace_folders())) print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
end, '[W]orkspace [L]ist Folders') end, '[W]orkspace [L]ist Folders')
-- Create a command `:Format` local to the LSP buffer -- Create a command `:Format` local to the buffer using Conform
vim.api.nvim_buf_create_user_command(bufnr, 'Format', function(_) vim.api.nvim_buf_create_user_command(bufnr, 'Format', function()
vim.lsp.buf.format() require('conform').format { bufnr = bufnr }
end, { desc = 'Format current buffer with LSP' }) end, { desc = 'Format current buffer with Conform' })
end end
-- document existing key chains -- document existing key chains
@@ -712,7 +745,7 @@ cmp.setup {
require('luasnip.loaders.from_vscode').lazy_load() require('luasnip.loaders.from_vscode').lazy_load()
-- Theme -- Theme
require('kanagawa').setup({ transparent = true }) require('kanagawa').setup { transparent = true }
vim.cmd [[colorscheme kanagawa]] vim.cmd [[colorscheme kanagawa]]
-- The line beneath this is called `modeline`. See `:help modeline` -- The line beneath this is called `modeline`. See `:help modeline`
-- vim: ts=2 sts=2 sw=2 et -- vim: ts=2 sts=2 sw=2 et

View File

@@ -1,9 +1,10 @@
{ {
"Comment.nvim": { "branch": "master", "commit": "e30b7f2008e52442154b66f7c519bfd2f1e32acb" }, "Comment.nvim": { "branch": "master", "commit": "e30b7f2008e52442154b66f7c519bfd2f1e32acb" },
"LuaSnip": { "branch": "master", "commit": "21f74f7ba8c49f95f9d7c8293b147c2901dd2d3a" }, "LuaSnip": { "branch": "master", "commit": "b3104910bb5ebf40492aadffae18f2528fa757d9" },
"cmp-nvim-lsp": { "branch": "main", "commit": "bd5a7d6db125d4654b50eeae9f5217f24bb22fd3" }, "cmp-nvim-lsp": { "branch": "main", "commit": "bd5a7d6db125d4654b50eeae9f5217f24bb22fd3" },
"cmp-path": { "branch": "main", "commit": "c642487086dbd9a93160e1679a1327be111cbc25" }, "cmp-path": { "branch": "main", "commit": "c642487086dbd9a93160e1679a1327be111cbc25" },
"cmp_luasnip": { "branch": "master", "commit": "98d9cb5c2c38532bd9bdb481067b20fea8f32e90" }, "cmp_luasnip": { "branch": "master", "commit": "98d9cb5c2c38532bd9bdb481067b20fea8f32e90" },
"conform.nvim": { "branch": "master", "commit": "b4aab989db276993ea5dcb78872be494ce546521" },
"fidget.nvim": { "branch": "main", "commit": "4d5858bd4c471c895060e1b9f3575f1551184dc5" }, "fidget.nvim": { "branch": "main", "commit": "4d5858bd4c471c895060e1b9f3575f1551184dc5" },
"friendly-snippets": { "branch": "main", "commit": "572f5660cf05f8cd8834e096d7b4c921ba18e175" }, "friendly-snippets": { "branch": "main", "commit": "572f5660cf05f8cd8834e096d7b4c921ba18e175" },
"gitsigns.nvim": { "branch": "main", "commit": "f780609807eca1f783a36a8a31c30a48fbe150c5" }, "gitsigns.nvim": { "branch": "main", "commit": "f780609807eca1f783a36a8a31c30a48fbe150c5" },
@@ -11,14 +12,15 @@
"kanagawa.nvim": { "branch": "master", "commit": "debe91547d7fb1eef34ce26a5106f277fbfdd109" }, "kanagawa.nvim": { "branch": "master", "commit": "debe91547d7fb1eef34ce26a5106f277fbfdd109" },
"lazy.nvim": { "branch": "main", "commit": "6c3bda4aca61a13a9c63f1c1d1b16b9d3be90d7a" }, "lazy.nvim": { "branch": "main", "commit": "6c3bda4aca61a13a9c63f1c1d1b16b9d3be90d7a" },
"lualine.nvim": { "branch": "master", "commit": "b8c23159c0161f4b89196f74ee3a6d02cdc3a955" }, "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" }, "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" }, "neodev.nvim": { "branch": "main", "commit": "46aa467dca16cf3dfe27098042402066d2ae242d" },
"nui.nvim": { "branch": "main", "commit": "de740991c12411b663994b2860f1a4fd0937c130" }, "nui.nvim": { "branch": "main", "commit": "de740991c12411b663994b2860f1a4fd0937c130" },
"nvim-autopairs": { "branch": "master", "commit": "23320e75953ac82e559c610bec5a90d9c6dfa743" }, "nvim-autopairs": { "branch": "master", "commit": "23320e75953ac82e559c610bec5a90d9c6dfa743" },
"nvim-cmp": { "branch": "main", "commit": "b5311ab3ed9c846b585c0c15b7559be131ec4be9" }, "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": { "branch": "master", "commit": "42fc28ba918343ebfd5565147a42a26580579482" },
"nvim-treesitter-textobjects": { "branch": "master", "commit": "71385f191ec06ffc60e80e6b0c9a9d5daed4824c" }, "nvim-treesitter-textobjects": { "branch": "master", "commit": "71385f191ec06ffc60e80e6b0c9a9d5daed4824c" },
"nvim-web-devicons": { "branch": "master", "commit": "6e51ca170563330e063720449c21f43e27ca0bc1" }, "nvim-web-devicons": { "branch": "master", "commit": "6e51ca170563330e063720449c21f43e27ca0bc1" },