57 lines
1.9 KiB
Lua
57 lines
1.9 KiB
Lua
-- 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 })
|
|
|
|
-- Netrw file explorer keymaps
|
|
-- Open netrw in new tab at current file's directory
|
|
map('n', '<leader>te', function()
|
|
local current_file_dir = vim.fn.expand('%:p:h')
|
|
vim.cmd('tabnew')
|
|
vim.cmd('Explore ' .. vim.fn.fnameescape(current_file_dir))
|
|
end, { desc = 'Tab explore (current file dir)', silent = true })
|
|
|
|
-- Open netrw in new tab at project root (cwd)
|
|
map('n', '<leader>tE', function()
|
|
vim.cmd('tabnew')
|
|
vim.cmd('Explore ' .. vim.fn.fnameescape(vim.fn.getcwd()))
|
|
end, { desc = 'Tab explore (project root)', silent = true })
|
|
|
|
return {}
|