From 15dfb33122734cf99e5023f97289a7ab694780f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20B=C3=B6hme?= Date: Sun, 26 Feb 2023 11:22:14 +0100 Subject: [PATCH] nvim: [HACK] add command for non default formatters --- modules/programs/nvim/default.nix | 1 + modules/programs/nvim/init.lua | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/modules/programs/nvim/default.nix b/modules/programs/nvim/default.nix index dec09fb..1d29b71 100644 --- a/modules/programs/nvim/default.nix +++ b/modules/programs/nvim/default.nix @@ -48,6 +48,7 @@ in withPython3 = true; extraLuaConfig = builtins.readFile ./init.lua; extraPackages = with pkgs; [ + alejandra black isort nil diff --git a/modules/programs/nvim/init.lua b/modules/programs/nvim/init.lua index 5af67a5..a282e3e 100644 --- a/modules/programs/nvim/init.lua +++ b/modules/programs/nvim/init.lua @@ -211,6 +211,12 @@ require("formatter").setup({ yaml = { require("formatter.filetypes.yaml").yamlfmt, }, + + -- HACK to use specific formatters only when specified + alejandra = { + require("formatter.filetypes.nix").alejandra, + }, + -- Use the special "*" filetype for defining formatter configurations on -- any filetype ["*"] = { @@ -220,6 +226,21 @@ require("formatter").setup({ }, }, }) +vim.api.nvim_create_user_command("Fmt", function(opts) + local params = vim.split(opts.args, "%s+", { trimempty = true }) + local filetype = vim.bo.filetype + vim.cmd("set filetype=" .. params[1]) -- fake filetype + vim.cmd(":Format") + vim.cmd("set filetype=" .. filetype) -- restore original filetype +end, { + nargs = 1, + complete = function() + local languages = { + nix = { "alejandra" }, + } + return languages[vim.bo.filetype] or {} + end, +}) wk.register({ ["="] = { "Format", "format (formatter)" }, }, { noremap = true, silent = true })