diff --git a/config/default.nix b/config/default.nix index eb3d6ed..c1e9e7a 100644 --- a/config/default.nix +++ b/config/default.nix @@ -22,6 +22,8 @@ ./languages/nvim-jdtls.nix ./languages/nvim-lint.nix + ./lsp/conform.nix + ./dap/dap.nix ./filetrees/neo-tree.nix diff --git a/config/lsp/conform.nix b/config/lsp/conform.nix new file mode 100644 index 0000000..2ed9708 --- /dev/null +++ b/config/lsp/conform.nix @@ -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 = "uf"; + action = ":FormatToggle"; + options = { + desc = "Toggle Format"; + silent = true; + }; + } + { + mode = "n"; + key = "cf"; + action = "lua require('conform').format()"; + options = { + silent = true; + desc = "Format Buffer"; + }; + } + + { + mode = "v"; + key = "cF"; + action = "lua require('conform').format()"; + 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, + }) + ''; +} +