-- Non-plugin and global helper keymaps local map = vim.keymap.set local opts = { noremap = true, silent = true } -- LSP-aware helpers with graceful fallback to built-ins local function lsp_has(bufnr, capability) for _, client in pairs(vim.lsp.get_clients({ bufnr = bufnr })) do if client.server_capabilities[capability] then return true end end return false end local function lsp_or_builtin(bufnr, capability, lsp_fn, builtin_normal) if lsp_has(bufnr, capability) then lsp_fn() else vim.cmd("normal! " .. builtin_normal) end end -- Global LSP-keymaps with fallback so they work reliably map('n', 'gd', function() lsp_or_builtin(0, 'definitionProvider', vim.lsp.buf.definition, 'gd') end, { desc = 'Go to definition (LSP or built-in)' }) map('n', 'gr', function() if lsp_has(0, 'referencesProvider') then vim.lsp.buf.references() end end, { desc = 'References (LSP)', silent = true }) map('n', 'gD', function() lsp_or_builtin(0, 'declarationProvider', vim.lsp.buf.declaration, 'gD') end, { desc = 'Go to declaration (LSP or built-in)' }) map('n', 'gI', function() if lsp_has(0, 'implementationProvider') then vim.lsp.buf.implementation() end end, { desc = 'Go to implementation (LSP)', silent = true }) map('n', 'K', function() if lsp_has(0, 'hoverProvider') then vim.lsp.buf.hover() end end, { desc = 'Hover (LSP)', silent = true }) return {}