From fc86df21d632c5ce429fa2eee42ffb5c29c5a1cf Mon Sep 17 00:00:00 2001 From: Hadeed Ahmad Date: Sat, 27 Apr 2024 03:04:56 +0500 Subject: [PATCH] Make prettier format on save --- nvim/lua/filetypes.lua | 25 ++++++++++++++++++++----- nvim/lua/keymaps.lua | 8 +++++++- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/nvim/lua/filetypes.lua b/nvim/lua/filetypes.lua index 00c149f..ba92ad8 100644 --- a/nvim/lua/filetypes.lua +++ b/nvim/lua/filetypes.lua @@ -10,13 +10,28 @@ vim.filetype.add({ ["dot-zshenv"] = "zsh", ["dot-zshrc"] = "zsh" }, + pattern = { - [".*/git/config"] = "gitconfig" + [".*/git/config"] = "gitconfig" } }) -vim.api.nvim_create_autocmd('Filetype', { - group = vim.api.nvim_create_augroup('setIndent', { clear = true }), - pattern = { "lua", "html", "css", "javascript", "javascriptreact", "typescript", "typescriptreact" }, - command = 'setlocal tabstop=2 softtabstop=2 shiftwidth=2' +vim.api.nvim_create_autocmd("Filetype", { + group = vim.api.nvim_create_augroup("setIndent", {}), + pattern = { "lua" }, + command = "setlocal tabstop=2 softtabstop=2 shiftwidth=2" +}) + +vim.api.nvim_create_autocmd("Filetype", { + group = vim.api.nvim_create_augroup("webDev", {}), + pattern = { "html", "css", "javascript", "javascriptreact" }, + callback = function() + vim.cmd [[setlocal tabstop=2 softtabstop=2 shiftwidth=2]] + vim.bo.formatprg = "npx prettier --stdin-filepath %" + + vim.api.nvim_create_autocmd("BufWritePre", { + command = "Format", + buffer = 0 + }) + end }) diff --git a/nvim/lua/keymaps.lua b/nvim/lua/keymaps.lua index 7fa04b9..998c198 100644 --- a/nvim/lua/keymaps.lua +++ b/nvim/lua/keymaps.lua @@ -39,7 +39,13 @@ window_map("j") window_map("k") window_map("l") -vim.api.nvim_create_user_command("Format", "normal mzgg=G'zzz", {}) +vim.api.nvim_create_user_command("Format", function() + if vim.bo.formatprg ~= "" then + vim.cmd [[normal mzgggqG'zzz]] + else + vim.cmd [[normal mzgg=G'zzz]] + end +end, {}) ------------------------------------------------------------------------------ -- Plugin Maps --