From eea5afaa7455f858b4443c382fc331c5546ef2ac Mon Sep 17 00:00:00 2001 From: Walter Jenkins Date: Wed, 26 Nov 2025 09:46:14 -0600 Subject: [PATCH] spectre and lsp keybindings --- .session.vim | 59 +++++++++ init.lua | 5 +- lua/plugins/lsp-keymaps.lua | 247 +++++++----------------------------- lua/plugins/snack.lua | 4 +- lua/plugins/spectre.lua | 24 ++++ 5 files changed, 134 insertions(+), 205 deletions(-) create mode 100644 .session.vim create mode 100644 lua/plugins/spectre.lua diff --git a/.session.vim b/.session.vim new file mode 100644 index 00000000..a338201f --- /dev/null +++ b/.session.vim @@ -0,0 +1,59 @@ +let SessionLoad = 1 +let s:so_save = &g:so | let s:siso_save = &g:siso | setg so=0 siso=0 | setl so=-1 siso=-1 +let v:this_session=expand(":p") +silent only +silent tabonly +cd ~/.config/nvim +if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == '' + let s:wipebuf = bufnr('%') +endif +let s:shortmess_save = &shortmess +if &shortmess =~ 'A' + set shortmess=aoOA +else + set shortmess=aoO +endif +badd +1 doc/kickstart.txt +argglobal +%argdel +edit doc/kickstart.txt +wincmd t +let s:save_winminheight = &winminheight +let s:save_winminwidth = &winminwidth +set winminheight=0 +set winheight=1 +set winminwidth=0 +set winwidth=1 +argglobal +setlocal foldmethod=expr +setlocal foldexpr=nvim_treesitter#foldexpr() +setlocal foldmarker={{{,}}} +setlocal foldignore=# +setlocal foldlevel=99 +setlocal foldminlines=1 +setlocal foldnestmax=20 +setlocal foldenable +let s:l = 1 - ((0 * winheight(0) + 24) / 48) +if s:l < 1 | let s:l = 1 | endif +keepjumps exe s:l +normal! zt +keepjumps 1 +normal! 0 +tabnext 1 +if exists('s:wipebuf') && len(win_findbuf(s:wipebuf)) == 0 && getbufvar(s:wipebuf, '&buftype') isnot# 'terminal' + silent exe 'bwipe ' . s:wipebuf +endif +unlet! s:wipebuf +set winheight=1 winwidth=20 +let &shortmess = s:shortmess_save +let &winminheight = s:save_winminheight +let &winminwidth = s:save_winminwidth +let s:sx = expand(":p:r")."x.vim" +if filereadable(s:sx) + exe "source " . fnameescape(s:sx) +endif +let &g:so = s:so_save | let &g:siso = s:siso_save +nohlsearch +doautoall SessionLoadPost +unlet SessionLoad +" vim: set ft=vim : diff --git a/init.lua b/init.lua index 2e6d9d33..7705533a 100644 --- a/init.lua +++ b/init.lua @@ -33,7 +33,6 @@ local theme_module = themes[env_var_nvim_theme] or themes[default_color_scheme] require('lazy').setup({ require(theme_module), require 'core.ui', - -- Load mason early so tools are ready for LSP configs require 'plugins.mason', @@ -54,6 +53,7 @@ require('lazy').setup({ require 'plugins.lazygit', require 'plugins.aerial', require 'plugins.misc', + require 'plugins.snack', -- LSP & companions require 'plugins.autocompletion', @@ -68,6 +68,9 @@ require('lazy').setup({ -- Debugging / DB (as you had) require 'plugins.debug', require 'plugins.database', + + require 'plugins.lsp-keymaps', + require 'plugins.spectre', }, { ui = { icons = vim.g.have_nerd_font and {} or { diff --git a/lua/plugins/lsp-keymaps.lua b/lua/plugins/lsp-keymaps.lua index bb2213a3..c0b67895 100644 --- a/lua/plugins/lsp-keymaps.lua +++ b/lua/plugins/lsp-keymaps.lua @@ -1,208 +1,51 @@ -local M = {} +-- lua/plugins/lsp-keymaps.lua +return { + -- Attach to the existing LSP config plugin + 'neovim/nvim-lspconfig', -M._keys = nil + -- We just need this to run once; inside we create an LspAttach autocmd + event = 'VeryLazy', ----@return table -function M.get() - if M._keys then - return M._keys - end + config = function() + -- Only define the autocmd once + vim.api.nvim_create_augroup('UserLspKeymaps', { clear = true }) - -- Standard LSP Keybindings (No LazyVim) - M._keys = { - { 'cl', 'LspInfo', desc = 'LSP Info' }, - { 'gd', vim.lsp.buf.definition, desc = 'Go to Definition' }, - { 'gr', vim.lsp.buf.references, desc = 'References' }, - { 'gI', vim.lsp.buf.implementation, desc = 'Go to Implementation' }, - { 'gy', vim.lsp.buf.type_definition, desc = 'Go to Type Definition' }, - { 'gD', vim.lsp.buf.declaration, desc = 'Go to Declaration' }, - { 'gK', vim.lsp.buf.signature_help, desc = 'Signature Help' }, - { '', vim.lsp.buf.signature_help, mode = 'i', desc = 'Signature Help' }, - { 'ca', vim.lsp.buf.code_action, desc = 'Code Action', mode = { 'n', 'v' } }, - { 'cr', vim.lsp.buf.rename, desc = 'Rename' }, - { - 'cR', - function() - if require 'snacks.rename' then - require('snacks.rename').rename_file() - else - print 'Snacks rename not available' + vim.api.nvim_create_autocmd('LspAttach', { + group = 'UserLspKeymaps', + callback = function(ev) + local bufnr = ev.buf + local opts = function(extra) + return vim.tbl_extend('force', { buffer = bufnr, silent = true, noremap = true }, extra or {}) end + + -- Core LSP maps + vim.keymap.set('n', 'cl', 'LspInfo', opts { desc = 'LSP Info' }) + + vim.keymap.set('n', 'gd', vim.lsp.buf.definition, opts { desc = 'Go to Definition' }) + vim.keymap.set('n', 'gr', vim.lsp.buf.references, opts { desc = 'References' }) + vim.keymap.set('n', 'gI', vim.lsp.buf.implementation, opts { desc = 'Go to Implementation' }) + vim.keymap.set('n', 'gy', vim.lsp.buf.type_definition, opts { desc = 'Go to Type Definition' }) + vim.keymap.set('n', 'gD', vim.lsp.buf.declaration, opts { desc = 'Go to Declaration' }) + vim.keymap.set('n', 'gK', vim.lsp.buf.signature_help, opts { desc = 'Signature Help' }) + + vim.keymap.set('i', '', vim.lsp.buf.signature_help, opts { desc = 'Signature Help' }) + + vim.keymap.set({ 'n', 'v' }, 'ca', vim.lsp.buf.code_action, opts { desc = 'Code Action' }) + + -- Rename symbol + vim.keymap.set('n', 'cr', vim.lsp.buf.rename, opts { desc = 'Rename' }) + + -- Rename file via Snacks, but **safely** + vim.keymap.set('n', 'cR', function() + local ok, snacks = pcall(require, 'snacks') + if ok and snacks.rename and snacks.rename.rename_file then + snacks.rename.rename_file() + else + print 'Snacks rename not available' + end + end, opts { desc = 'Rename File' }) end, - desc = 'Rename File', - mode = { 'n' }, - has = { 'workspace/didRenameFiles', 'workspace/willRenameFiles' }, - }, - { 'cr', vim.lsp.buf.rename, desc = 'Rename', has = 'rename' }, - } + }) + end, +} - return M._keys -end - ----@param buffer number -function M.on_attach(_, buffer) - for _, key in pairs(M.get()) do - vim.keymap.set(key.mode or 'n', key[1], key[2], { buffer = buffer, desc = key.desc, silent = true }) - end -end - -return M --- local M = {} --- --- ---@type LazyKeysLspSpec[]|nil --- M._keys = nil --- --- ---@alias LazyKeysLspSpec LazyKeysSpec|{has?:string|string[], cond?:fun():boolean} --- ---@alias LazyKeysLsp LazyKeys|{has?:string|string[], cond?:fun():boolean} --- --- ---@return LazyKeysLspSpec[] --- function M.get() --- if M._keys then --- return M._keys --- end --- -- stylua: ignore --- M._keys = { --- { "cl", "LspInfo", desc = "LSP Info" }, --- { "gd", vim.lsp.buf.definition, desc = "Go to Definition", has = "definition" }, --- { "gr", vim.lsp.buf.references, desc = "References", nowait = true }, --- { "gI", vim.lsp.buf.implementation, desc = "Go to Implementation" }, --- { "gy", vim.lsp.buf.type_definition, desc = "Go to Type Definition" }, --- { "gD", vim.lsp.buf.declaration, desc = "Go to Declaration" }, --- { "K", function() return vim.lsp.buf.hover() end, desc = "Hover" }, --- { "gK", function() return vim.lsp.buf.signature_help() end, desc = "Signature Help", has = "signatureHelp" }, --- { "", function() return vim.lsp.buf.signature_help() end, mode = "i", desc = "Signature Help", has = "signatureHelp" }, --- { "ca", vim.lsp.buf.code_action, desc = "Code Action", mode = { "n", "v" }, has = "codeAction" }, --- { "cc", vim.lsp.codelens.run, desc = "Run CodeLens", mode = { "n", "v" }, has = "codeLens" }, --- { "cC", vim.lsp.codelens.refresh, desc = "Refresh & Display CodeLens", mode = { "n" }, has = "codeLens" }, --- { --- "cR", --- function() --- if require("snacks.rename") then --- require("snacks.rename").rename_file() --- else --- print("Snacks rename not available") --- end --- end, --- desc = "Rename File", --- mode = { "n" }, --- has = { "workspace/didRenameFiles", "workspace/willRenameFiles" } --- }, --- { "cr", vim.lsp.buf.rename, desc = "Rename", has = "rename" }, --- { --- "cA", --- function() --- if require("lazyvim.lsp.action") then --- require("lazyvim.lsp.action").source() --- else --- print("LazyVim LSP action not available") --- end --- end, --- desc = "Source Action", --- has = "codeAction" --- }, --- { --- "]]", --- function() --- if require("snacks.words") then --- require("snacks.words").jump(vim.v.count1) --- end --- end, --- has = "documentHighlight", --- desc = "Next Reference", --- cond = function() return require("snacks.words").is_enabled() end --- }, --- { --- "[[", --- function() --- if require("snacks.words") then --- require("snacks.words").jump(-vim.v.count1) --- end --- end, --- has = "documentHighlight", --- desc = "Previous Reference", --- cond = function() return require("snacks.words").is_enabled() end --- }, --- { --- "", --- function() --- if require("snacks.words") then --- require("snacks.words").jump(vim.v.count1, true) --- end --- end, --- has = "documentHighlight", --- desc = "Next Reference", --- cond = function() return require("snacks.words").is_enabled() end --- }, --- { --- "", --- function() --- if require("snacks.words") then --- require("snacks.words").jump(-vim.v.count1, true) --- end --- end, --- has = "documentHighlight", --- desc = "Previous Reference", --- cond = function() return require("snacks.words").is_enabled() end --- }, --- } --- --- return M._keys --- end --- --- ---@param buffer number --- ---@param method string|string[] --- function M.has(buffer, method) --- if type(method) == 'table' then --- for _, m in ipairs(method) do --- if M.has(buffer, m) then --- return true --- end --- end --- return false --- end --- method = method:find '/' and method or 'textDocument/' .. method --- local clients = vim.lsp.get_active_clients { bufnr = buffer } --- for _, client in ipairs(clients) do --- if client.supports_method(method) then --- return true --- end --- end --- return false --- end --- --- ---@return LazyKeysLsp[] --- function M.resolve(buffer) --- local Keys = require 'lazy.core.handler.keys' --- if not Keys.resolve then --- return {} --- end --- local spec = vim.tbl_extend('force', {}, M.get()) --- local opts = require 'nvim-lspconfig' --- local clients = vim.lsp.get_active_clients { bufnr = buffer } --- for _, client in ipairs(clients) do --- local maps = opts.servers and opts.servers[client.name] and opts.servers[client.name].keys or {} --- vim.list_extend(spec, maps) --- end --- return Keys.resolve(spec) --- end --- --- function M.on_attach(_, buffer) --- local Keys = require 'lazy.core.handler.keys' --- local keymaps = M.resolve(buffer) --- --- for _, keys in pairs(keymaps) do --- local has = not keys.has or M.has(buffer, keys.has) --- local cond = not (keys.cond == false or ((type(keys.cond) == 'function') and not keys.cond())) --- --- if has and cond then --- local opts = Keys.opts(keys) --- opts.cond = nil --- opts.has = nil --- opts.silent = opts.silent ~= false --- opts.buffer = buffer --- vim.keymap.set(keys.mode or 'n', keys.lhs, keys.rhs, opts) --- end --- end --- end --- --- return M diff --git a/lua/plugins/snack.lua b/lua/plugins/snack.lua index cec88415..b703a963 100644 --- a/lua/plugins/snack.lua +++ b/lua/plugins/snack.lua @@ -20,9 +20,9 @@ return { image = { enabled = true, -- force enable even if terminal isn't fully supported backend = "magick", -- fallback backend for images - diagnostics = true, -- keep this on so you can see setup issues + diagnostics = false, -- keep this on so you can see setup issues auto_preview = false, -- if you don’t want inline previews on hover - view = "image", -- can be "image", "ascii", or "none" + view = "ascii", -- can be "image", "ascii", or "none" max_height = 30, -- in rows max_width = 50, -- in columns use_dither = false, -- optional: enable dithered rendering diff --git a/lua/plugins/spectre.lua b/lua/plugins/spectre.lua new file mode 100644 index 00000000..bb51208d --- /dev/null +++ b/lua/plugins/spectre.lua @@ -0,0 +1,24 @@ +return { + -- other plugins … + + -- nvim-spectre + { + "nvim-pack/nvim-spectre", + dependencies = { "nvim-lua/plenary.nvim" }, + -- optionally configure lazy loading: + -- lazy = true, -- default + -- or set events/commands/ft if desired + + -- you can also provide setup/options here if you want: + config = function() + require("spectre").setup({ + -- your config overrides, or just omit for defaults + }) + -- example keymap + vim.keymap.set('n', 'S', 'lua require("spectre").toggle()', { desc = "Toggle Spectre" }) + end, + }, + + -- other plugins … +} +