From 4f858eb004c557991a99c6daeb82ae18b025d482 Mon Sep 17 00:00:00 2001 From: Matt Sturgeon Date: Tue, 30 Sep 2025 13:52:00 +0100 Subject: [PATCH] tests/modules/lsp: test lsp keymaps Assert that the expected code is added to an autoCmd. --- tests/test-sources/modules/lsp.nix | 117 +++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) diff --git a/tests/test-sources/modules/lsp.nix b/tests/test-sources/modules/lsp.nix index 299e1d16..05ffb7df 100644 --- a/tests/test-sources/modules/lsp.nix +++ b/tests/test-sources/modules/lsp.nix @@ -29,6 +29,123 @@ }; }; + keymaps = + { + lib, + pkgs, + config, + ... + }: + let + autoCmds = config.autoCmd; + autoCmd = builtins.head autoCmds; + + print = lib.generators.toPretty { }; + expect = name: expected: actual: { + assertion = expected == actual; + message = "Expected ${name} to be ${print expected}, but found ${print actual}"; + }; + in + { + lsp.keymaps = [ + { + key = "gd"; + lspBufAction = "definition"; + } + { + key = "K"; + lspBufAction = "hover"; + } + { + key = "k"; + action = lib.nixvim.mkRaw "function() vim.diagnostic.jump({ count=-1, float=true }) end"; + } + { + key = "j"; + action = lib.nixvim.mkRaw "function() vim.diagnostic.jump({ count=1, float=true }) end"; + } + { + key = "lx"; + action = "LspStop"; + } + ]; + + assertions = [ + (expect "number of autocmds" 1 (builtins.length autoCmds)) + (expect "event" "LspAttach" autoCmd.event) + (expect "group" "nixvim_lsp_binds" autoCmd.group) + ]; + + test.extraInputs = [ + (pkgs.testers.testEqualContents { + assertion = "lsp keymaps autocmd callback"; + actual = pkgs.writeText "actual.lua" (autoCmd.callback.__raw or ""); + expected = pkgs.writeText "expected.lua" '' + function(args) + local __keymaps = { { action = vim.lsp.buf["definition"], key = "gd", lspBufAction = "definition", mode = "" }, { action = vim.lsp.buf["hover"], key = "K", lspBufAction = "hover", mode = "" }, { action = function() vim.diagnostic.jump({ count=-1, float=true }) end, key = "k", mode = "" }, { action = function() vim.diagnostic.jump({ count=1, float=true }) end, key = "j", mode = "" }, { action = "LspStop", key = "lx", mode = "" } } + + for _, keymap in ipairs(__keymaps) do + local options = vim.tbl_extend( + "keep", + keymap.options or {}, + { buffer = args.buf } + ) + vim.keymap.set(keymap.mode, keymap.key, keymap.action, options) + end + end + ''; + }) + ]; + + # Test that keymaps are registered after LspAttach + extraConfigLuaPost = '' + -- Assert keymaps not registered + local keymaps_pre_attach = vim.api.nvim_buf_get_keymap(0, "") + if not vim.tbl_isempty(keymaps_pre_attach) then + print("Unexpected keymaps registered before LspAttach:") + vim.print(keymaps_pre_attach) + end + + -- Trigger the LspAttach autocmd + vim.api.nvim_exec_autocmds("LspAttach", { + group = "nixvim_lsp_binds", + buffer = 0, + modeline = false, + data = { + client_id = "stub_id", + }, + }) + + -- Assert keymaps are registered + local keymaps_post_attach = vim.api.nvim_buf_get_keymap(0, "") + + local keymaps_post_attach_len = vim.tbl_count(keymaps_post_attach) + if keymaps_post_attach_len ~= 5 then + print("Expected 5 keymaps to be registered after LspAttach, but found", keymaps_post_attach_len) + vim.print(keymaps_post_attach) + end + + for _, expected in + ipairs({ + "gd", + "K", + "\\k", + "\\j", + "\\lx", + }) + do + local has_keymap = vim.tbl_contains( + keymaps_post_attach, + function(keymap) return keymap.lhsraw == expected end, + { predicate = true } + ) + if not has_keymap then + print("keymap", expected, "was not registered") + end + end + ''; + }; + package-fallback = { lib, config, ... }: {