diff --git a/lua/keymaps.lua b/lua/keymaps.lua index c017f72..fbbbcc5 100644 --- a/lua/keymaps.lua +++ b/lua/keymaps.lua @@ -1,42 +1,35 @@ -- Non-plugin and global helper keymaps +-- +-- NOTE: LSP keymaps are primarily handled in lua/plugins/lsp.lua via LspAttach. +-- Buffer-local keymaps set there take precedence over these global fallbacks. +-- +-- These global keymaps provide fallback behavior for buffers without LSP attached, +-- ensuring that common navigation keys still work with built-in Vim functionality +-- (e.g., ctags, included files, man pages). + 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 +-- Fallback navigation keymaps for non-LSP buffers +-- When LSP attaches, buffer-local keymaps in lsp.lua override these 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 }) + -- Built-in: jump to definition via tags or included files + vim.cmd("normal! gd") +end, { desc = 'Go to definition (built-in fallback)', 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)' }) + -- Built-in: jump to declaration (global) + vim.cmd("normal! gD") +end, { desc = 'Go to declaration (built-in fallback)', silent = true }) -map('n', 'gI', function() - if lsp_has(0, 'implementationProvider') then vim.lsp.buf.implementation() end -end, { desc = 'Go to implementation (LSP)', silent = true }) +-- LSP-only keymaps (no useful fallback for these in non-LSP contexts) +map('n', 'gr', '', { desc = 'References (requires LSP)', silent = true }) +map('n', 'gI', '', { desc = 'Implementation (requires LSP)', silent = true }) +-- K: Hover/Help with sensible fallback map('n', 'K', function() - if lsp_has(0, 'hoverProvider') then vim.lsp.buf.hover() end -end, { desc = 'Hover (LSP)', silent = true }) - -return {} + -- Built-in K uses 'keywordprg' (defaults to 'man' for shell scripts, etc.) + -- LSP buffers override this with vim.lsp.buf.hover() in lsp.lua + if vim.bo.keywordprg ~= '' then + vim.cmd("normal! K") + end +end, { desc = 'Hover/Help (LSP or keywordprg fallback)', silent = true })