nixvim/config/plugins/lsp/conform.nix
Wael Nasreddine 43f2ee52b8
config/plugins/lsp: Stop after first formatter (#69)
NeoVim was stuck in an infinite loop saving an HTML file, by stopping
after the first successful formatter between `prettierd` and `prettier`
the inifinite loop stopped.

(cherry picked from commit
7e6eb2b645)
2025-01-08 09:02:07 +01:00

169 lines
4.6 KiB
Nix

{
lib,
pkgs,
...
}:
{
config = {
extraConfigLuaPre =
# lua
''
local slow_format_filetypes = {}
vim.api.nvim_create_user_command("FormatDisable", function(args)
if args.bang then
-- FormatDisable! will disable formatting just for this buffer
vim.b.disable_autoformat = true
else
vim.g.disable_autoformat = true
end
end, {
desc = "Disable autoformat-on-save",
bang = true,
})
vim.api.nvim_create_user_command("FormatEnable", function()
vim.b.disable_autoformat = false
vim.g.disable_autoformat = false
end, {
desc = "Re-enable autoformat-on-save",
})
vim.api.nvim_create_user_command("FormatToggle", function(args)
if args.bang then
-- Toggle formatting for current buffer
vim.b.disable_autoformat = not vim.b.disable_autoformat
else
-- Toggle formatting globally
vim.g.disable_autoformat = not vim.g.disable_autoformat
end
end, {
desc = "Toggle autoformat-on-save",
bang = true,
})
'';
plugins.conform-nvim = {
enable = true;
settings = {
format_on_save = ''
function(bufnr)
if vim.g.disable_autoformat or vim.b[bufnr].disable_autoformat then
return
end
if slow_format_filetypes[vim.bo[bufnr].filetype] then
return
end
local function on_format(err)
if err and err:match("timeout$") then
slow_format_filetypes[vim.bo[bufnr].filetype] = true
end
end
return { timeout_ms = 200, lsp_fallback = true }, on_format
end
'';
format_after_save = ''
function(bufnr)
if vim.g.disable_autoformat or vim.b[bufnr].disable_autoformat then
return
end
if not slow_format_filetypes[vim.bo[bufnr].filetype] then
return
end
return { lsp_fallback = true }
end
'';
notify_on_error = true;
formatters_by_ft = {
html = {
__unkeyed-1 = "prettierd";
__unkeyed-2 = "prettier";
stop_after_first = true;
};
css = {
__unkeyed-1 = "prettierd";
__unkeyed-2 = "prettier";
stop_after_first = true;
};
javascript = {
__unkeyed-1 = "prettierd";
__unkeyed-2 = "prettier";
stop_after_first = true;
};
typescript = {
__unkeyed-1 = "prettierd";
__unkeyed-2 = "prettier";
stop_after_first = true;
};
python = [
"black"
"isort"
];
lua = [ "stylua" ];
nix = [ "nixfmt-rfc-style" ];
markdown = {
__unkeyed-1 = "prettierd";
__unkeyed-2 = "prettier";
stop_after_first = true;
};
yaml = {
__unkeyed-1 = "prettierd";
__unkeyed-2 = "prettier";
stop_after_first = true;
};
terraform = [ "terraform_fmt" ];
bicep = [ "bicep" ];
bash = [
"shellcheck"
"shellharden"
"shfmt"
];
json = [ "jq" ];
"_" = [ "trim_whitespace" ];
};
formatters = {
black = {
command = "${lib.getExe pkgs.black}";
};
isort = {
command = "${lib.getExe pkgs.isort}";
};
nixfmt-rfc-style = {
command = "${lib.getExe pkgs.nixfmt-rfc-style}";
};
alejandra = {
command = "${lib.getExe pkgs.alejandra}";
};
jq = {
command = "${lib.getExe pkgs.jq}";
};
prettierd = {
command = "${lib.getExe pkgs.prettierd}";
};
stylua = {
command = "${lib.getExe pkgs.stylua}";
};
shellcheck = {
command = "${lib.getExe pkgs.shellcheck}";
};
shfmt = {
command = "${lib.getExe pkgs.shfmt}";
};
shellharden = {
command = "${lib.getExe pkgs.shellharden}";
};
bicep = {
command = "${lib.getExe pkgs.bicep}";
};
#yamlfmt = {
# command = "${lib.getExe pkgs.yamlfmt}";
#};
};
};
};
};
}