This commit is contained in:
Ray Elliott 2025-12-07 20:02:17 +00:00
parent 8bde5cc4d3
commit 02df315571
1 changed files with 26 additions and 33 deletions

View File

@ -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', '<Nop>', { desc = 'References (requires LSP)', silent = true })
map('n', 'gI', '<Nop>', { 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 })