# Copyright (c) 2024-2025 0xda157 { lib, pkgs, ... }: let inherit (lib) singleton; in { extraPackages = with pkgs; [ prettierd black stylua nixfmt rustfmt shfmt clang-tools gleam keep-sorted ]; keymaps = singleton { mode = [ "n" "v" ]; key = "l"; action = # lua '' function() conform.format({ lsp_fallback = true, async = true, }) end ''; }; plugins.conform-nvim = { enable = true; settings = { format_on_save = # lua '' function(bufnr) -- Disable "format_on_save lsp_fallback" for lanuages that don't -- have a well standardized coding style. You can add additional -- lanuages here or re-enable it for the disabled ones. local disable_filetypes = { c = true, cpp = true } if vim.g.disable_autoformat or disable_filetypes[vim.bo[bufnr].filetype] then return nil else return { timeout_ms = 500, lsp_format = "fallback", } end end ''; default_format_opts = { lsp_format = "fallback"; timeout_ms = 200; }; formatters_by_ft = let prettier = { __unkeyed-1 = "prettierd"; __unkeyed-2 = "prettier"; stop_after_first = true; }; in { # keep-sorted start block=true "*" = [ "keep-sorted" ]; "_" = [ "trim_whitespace" ]; bash = [ "shfmt" ]; c = [ "clangd" ]; cpp = [ "clangd" ]; css = prettier; # html = prettier; gleam = [ "gleam" ]; javascript = prettier; javascriptreact = prettier; lua = [ "stylua" ]; nix = [ "nixfmt" ]; # markdown = prettier; python = [ "black" ]; rust = [ "rustfmt" ]; sh = [ "shfmt" ]; typescript = prettier; typescriptreact = prettier; zsh = [ "shfmt" ]; # keep-sorted end }; }; luaConfig.post = '' 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", }) ''; }; }