add conform.nix

This commit is contained in:
Aditya 2024-03-06 17:48:40 +05:30
parent 2b78342829
commit 8d51966364
Signed by: aditya
SSH key fingerprint: SHA256:jL1IvWsjjlPtw6HvDIHfXfhO9IkIokNEyIfuFhSdoyU
2 changed files with 95 additions and 0 deletions

View file

@ -22,6 +22,8 @@
./languages/nvim-jdtls.nix
./languages/nvim-lint.nix
./lsp/conform.nix
./dap/dap.nix
./filetrees/neo-tree.nix

93
config/lsp/conform.nix Normal file
View file

@ -0,0 +1,93 @@
{
plugins.conform-nvim = {
enable = true;
notifyOnError = true;
formattersByFt = {
html = [["prettierd" "prettier"]];
css = [["prettierd" "prettier"]];
javascript = [["prettierd" "prettier"]];
javascriptreact = [["prettierd" "prettier"]];
typescript = [["prettierd" "prettier"]];
typescriptreact = [["prettierd" "prettier"]];
java = ["google-java-format"];
python = ["black"];
lua = ["stylua"];
nix = ["alejandra"];
markdown = [["prettierd" "prettier"]];
rust = ["rustfmt"];
};
};
keymaps = [
{
mode = "n";
key = "<leader>uf";
action = ":FormatToggle<CR>";
options = {
desc = "Toggle Format";
silent = true;
};
}
{
mode = "n";
key = "<leader>cf";
action = "<cmd>lua require('conform').format()<cr>";
options = {
silent = true;
desc = "Format Buffer";
};
}
{
mode = "v";
key = "<leader>cF";
action = "<cmd>lua require('conform').format()<cr>";
options = {
silent = true;
desc = "Format Lines";
};
}
];
extraConfigLua = ''
local conform = require("conform")
local notify = require("notify")
conform.setup({
format_on_save = function(bufnr)
-- Disable with a global or buffer-local variable
if vim.g.disable_autoformat or vim.b[bufnr].disable_autoformat then
return
end
return { timeout_ms = 500, lsp_fallback = true }
end,
})
local function show_notification(message, level)
notify(message, level, { title = "conform.nvim" })
end
vim.api.nvim_create_user_command("FormatToggle", function(args)
local is_global = not args.bang
if is_global then
vim.g.disable_autoformat = not vim.g.disable_autoformat
if vim.g.disable_autoformat then
show_notification("Autoformat-on-save disabled globally", "info")
else
show_notification("Autoformat-on-save enabled globally", "info")
end
else
vim.b.disable_autoformat = not vim.b.disable_autoformat
if vim.b.disable_autoformat then
show_notification("Autoformat-on-save disabled for this buffer", "info")
else
show_notification("Autoformat-on-save enabled for this buffer", "info")
end
end
end, {
desc = "Toggle autoformat-on-save",
bang = true,
})
'';
}