nvim: [HACK] add command for non default formatters

This commit is contained in:
Moritz Böhme 2023-02-26 11:22:14 +01:00
parent c4bb776b75
commit 15dfb33122
Signed by: moritz
GPG key ID: 970C6E89EB0547A9
2 changed files with 22 additions and 0 deletions

View file

@ -48,6 +48,7 @@ in
withPython3 = true; withPython3 = true;
extraLuaConfig = builtins.readFile ./init.lua; extraLuaConfig = builtins.readFile ./init.lua;
extraPackages = with pkgs; [ extraPackages = with pkgs; [
alejandra
black black
isort isort
nil nil

View file

@ -211,6 +211,12 @@ require("formatter").setup({
yaml = { yaml = {
require("formatter.filetypes.yaml").yamlfmt, 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 -- Use the special "*" filetype for defining formatter configurations on
-- any filetype -- 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({ wk.register({
["="] = { "<cmd>Format<cr>", "format (formatter)" }, ["="] = { "<cmd>Format<cr>", "format (formatter)" },
}, { noremap = true, silent = true }) }, { noremap = true, silent = true })