Skip to content

Commit 6b180f8

Browse files
author
Bassam Data
committed
refactor(namu): refactor custom keymaps
1 parent e610385 commit 6b180f8

File tree

3 files changed

+124
-144
lines changed

3 files changed

+124
-144
lines changed

README.md

+66-98
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ Using [lazy.nvim](https://github.com/folke/lazy.nvim):
6262
options = {}, -- here you can configure namu
6363
},
6464
-- Optional: Enable other modules if needed
65+
ui_select = { enable = false }, -- vim.ui.select() wrapper
6566
colorscheme = {
6667
enable = false,
6768
options = {
@@ -70,11 +71,8 @@ Using [lazy.nvim](https://github.com/folke/lazy.nvim):
7071
write_shada = false, -- If you open multiple nvim instances, then probably you need to enable this
7172
},
7273
},
73-
ui_select = { enable = false }, -- vim.ui.select() wrapper
7474
})
7575
-- === Suggested Keymaps: ===
76-
local namu = require("namu.namu_symbols")
77-
local colorscheme = require("namu.colorscheme")
7876
vim.keymap.set("n", "<leader>ss",":Namu symbols<cr>" , {
7977
desc = "Jump to LSP symbol",
8078
silent = true,
@@ -107,7 +105,7 @@ Using [lazy.nvim](https://github.com/folke/lazy.nvim):
107105

108106
</details>
109107

110-
### Default Keymaps
108+
### Keymaps
111109

112110
<details>
113111
<summary>Default keymaps are:</summary>
@@ -123,16 +121,6 @@ Using [lazy.nvim](https://github.com/folke/lazy.nvim):
123121
| `<Up>` | Previous item |
124122
| `q` | Close help |
125123

126-
to change the movement keys to `C-j` and `C-k`:
127-
```lua
128-
-- in namu_symbols.options
129-
movement = {
130-
next = "<C-j>",
131-
previous = "<C-k>",
132-
alternative_next = "<DOWN>",
133-
alternative_previous = "<UP>",
134-
},
135-
```
136124

137125

138126
#### Multiselect
@@ -154,8 +142,57 @@ movement = {
154142

155143
</details>
156144

145+
### Change Keymaps:
146+
147+
<details>
148+
<summary>change the default keymaps:</summary>
149+
150+
```lua
151+
-- in namu_symbols.options
152+
movement = {
153+
next = { "<C-n>", "<DOWN>" }, -- Support multiple keys
154+
previous = { "<C-p>", "<UP>" }, -- Support multiple keys
155+
close = { "<ESC>" }, -- close mapping
156+
select = { "<CR>" }, -- select mapping
157+
delete_word = {}, -- delete word mapping
158+
clear_line = {}, -- clear line mapping
159+
},
160+
multiselect = {
161+
enabled = false,
162+
indicator = "", -- or "✓"◉
163+
keymaps = {
164+
toggle = "<Tab>",
165+
select_all = "<C-a>",
166+
clear_all = "<C-l>",
167+
untoggle = "<S-Tab>",
168+
},
169+
max_items = nil, -- No limit by default
170+
},
171+
custom_keymaps = {
172+
yank = {
173+
keys = { "<C-y>" }, -- yank symbol text
174+
},
175+
delete = {
176+
keys = { "<C-d>" }, -- delete symbol text
177+
},
178+
vertical_split = {
179+
keys = { "<C-v>" }, -- open in vertical split
180+
},
181+
horizontal_split = {
182+
keys = { "<C-h>" }, -- open in horizontal split
183+
},
184+
codecompanion = {
185+
keys = "<C-o>", -- Add symbols to CodeCompanion
186+
},
187+
avante = {
188+
keys = "<C-t>", -- Add symbol to Avante
189+
},
190+
},
191+
```
192+
193+
</details>
157194

158-
## Available Commands
195+
## Commands
159196

160197
The `Namu` command provides several subcommands with autocomplete:
161198

@@ -326,97 +363,28 @@ M.config = {
326363
close_on_yank = false, -- Whether to close picker after yanking
327364
close_on_delete = true, -- Whether to close picker after deleting
328365
},
329-
keymaps = {
330-
{
331-
key = "<C-y>",
332-
handler = function(items_or_item, state)
333-
local success = M.yank_symbol_text(items_or_item, state)
334-
-- Only close if yanking was successful and config says to close
335-
if success and M.config.actions.close_on_yank then
336-
M.clear_preview_highlight()
337-
return false -- This should close the picker
338-
end
339-
end,
366+
custom_keymaps = {
367+
yank = {
368+
keys = { "<C-y>" },
340369
desc = "Yank symbol text",
341370
},
342-
{
343-
key = "<C-d>",
344-
handler = function(items_or_item, state)
345-
local deleted = M.delete_symbol_text(items_or_item, state)
346-
-- Only close if deletion was successful and config says to close
347-
if deleted and M.config.actions.close_on_delete then
348-
M.clear_preview_highlight()
349-
return false
350-
end
351-
end,
352-
desc = "Delete symbol text",
371+
delete = {
372+
keys = { "<C-d>" },
353373
},
354-
{
355-
key = "<C-v>",
356-
handler = function(item, selecta_state)
357-
if not state.original_buf then
358-
vim.notify("No original buffer available", vim.log.levels.ERROR)
359-
return
360-
end
361-
362-
local new_win = selecta.open_in_split(selecta_state, item, "vertical", state)
363-
if new_win then
364-
local symbol = item.value
365-
if symbol and symbol.lnum and symbol.col then
366-
-- Set cursor to symbol position
367-
pcall(vim.api.nvim_win_set_cursor, new_win, { symbol.lnum, symbol.col - 1 })
368-
vim.cmd("normal! zz")
369-
end
370-
M.clear_preview_highlight()
371-
return false
372-
end
373-
end,
374+
vertical_split = {
375+
keys = { "<C-v>" },
374376
desc = "Open in vertical split",
375377
},
376-
{
377-
key = "<C-h>",
378-
handler = function(item, selecta_state)
379-
if not state.original_buf then
380-
vim.notify("No original buffer available", vim.log.levels.ERROR)
381-
return
382-
end
383-
384-
local new_win = selecta.open_in_split(selecta_state, item, "horizontal", state)
385-
if new_win then
386-
local symbol = item.value
387-
if symbol and symbol.lnum and symbol.col then
388-
-- Set cursor to symbol position
389-
pcall(vim.api.nvim_win_set_cursor, new_win, { symbol.lnum, symbol.col - 1 })
390-
vim.cmd("normal! zz")
391-
end
392-
M.clear_preview_highlight()
393-
return false
394-
end
395-
end,
378+
horizontal_split = {
379+
keys = { "<C-h>" },
396380
desc = "Open in horizontal split",
397381
},
398-
{
399-
key = "<C-o>",
400-
handler = function(items_or_item)
401-
if type(items_or_item) == "table" and items_or_item[1] then
402-
M.add_symbol_to_codecompanion(items_or_item, state.original_buf)
403-
else
404-
-- Single item case
405-
M.add_symbol_to_codecompanion({ items_or_item }, state.original_buf)
406-
end
407-
end,
382+
codecompanion = {
383+
keys = "<C-o>",
408384
desc = "Add symbol to CodeCompanion",
409385
},
410-
{
411-
key = "<C-t>",
412-
handler = function(items_or_item)
413-
if type(items_or_item) == "table" and items_or_item[1] then
414-
M.add_symbol_to_avante(items_or_item, state.original_buf)
415-
else
416-
-- Single item case
417-
M.add_symbol_to_avante({ items_or_item }, state.original_buf)
418-
end
419-
end,
386+
avante = {
387+
keys = "<C-t>",
420388
desc = "Add symbol to Avante",
421389
},
422390
},
@@ -484,5 +452,5 @@ Any suggestions to improve and integrate with other plugins are also welcome.
484452
- [Mini.pick](https://github.com/echasnovski/mini.nvim) @echasnovski for the idea of `getchar()`, without which this plugin wouldn't exist.
485453
- Magnet module (couldn’t find it anymore on GitHub, sorry!), which intrigued me a lot.
486454
- @folke for handling multiple versions of Neovim LSP requests in [Snacks.nvim](https://github.com/folke/snacks.nvim).
487-
- tests structure, thanks to @Oli [CodeCompanion](https://github.com/olimorris/codecompanion.nvim)
455+
- tests and ci structure, thanks to @Oli [CodeCompanion](https://github.com/olimorris/codecompanion.nvim)
488456
- A simple mechanism to persist the colorscheme, thanks to this [Reddit comment](https://www.reddit.com/r/neovim/comments/1edwhk8/comment/lfb1m2f/?utm_source=share&utm_medium=web3x&utm_name=web3xcss&utm_term=1&utm_content=share_button).

lua/namu/namu_symbols/init.lua

+16-18
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ local M = {}
6262
---@field auto_select boolean Auto-select single matches
6363
---@field row_position "center"|"top10"|"top10_right"|"center_right"|"bottom" Window position preset
6464
---@field multiselect table Multiselect configuration
65-
---@field keymaps table Keymap configuration
65+
---@field custom_keymaps table Keymap configuration
6666

6767
---@class NamuState
6868
---@field original_win number|nil Original window
@@ -249,33 +249,31 @@ M.config = {
249249
alternative_next = "<DOWN>",
250250
alternative_previous = "<UP>",
251251
},
252-
keymaps = {
253-
{
254-
key = "<C-y>",
252+
custom_keymaps = {
253+
yank = {
254+
keys = { "<C-y>" },
255255
handler = function(items_or_item, state)
256256
local success = M.yank_symbol_text(items_or_item, state)
257-
-- Only close if yanking was successful and config says to close
258257
if success and M.config.actions.close_on_yank then
259258
M.clear_preview_highlight()
260-
return false -- This should close the picker
259+
return false
261260
end
262261
end,
263262
desc = "Yank symbol text",
264263
},
265-
{
266-
key = "<C-d>",
264+
delete = {
265+
keys = { "<C-d>" },
267266
handler = function(items_or_item, state)
268267
local deleted = M.delete_symbol_text(items_or_item, state)
269-
-- Only close if deletion was successful and config says to close
270268
if deleted and M.config.actions.close_on_delete then
271269
M.clear_preview_highlight()
272270
return false
273271
end
274272
end,
275273
desc = "Delete symbol text",
276274
},
277-
{
278-
key = "<C-v>",
275+
vertical_split = {
276+
keys = { "<C-v>" },
279277
handler = function(item, selecta_state)
280278
if not state.original_buf then
281279
vim.notify("No original buffer available", vim.log.levels.ERROR)
@@ -296,8 +294,8 @@ M.config = {
296294
end,
297295
desc = "Open in vertical split",
298296
},
299-
{
300-
key = "<C-h>",
297+
horizontal_split = {
298+
keys = { "<C-h>" },
301299
handler = function(item, selecta_state)
302300
if not state.original_buf then
303301
vim.notify("No original buffer available", vim.log.levels.ERROR)
@@ -318,8 +316,8 @@ M.config = {
318316
end,
319317
desc = "Open in horizontal split",
320318
},
321-
{
322-
key = "<C-o>",
319+
codecompanion = {
320+
keys = "<C-o>",
323321
handler = function(items_or_item)
324322
if type(items_or_item) == "table" and items_or_item[1] then
325323
M.add_symbol_to_codecompanion(items_or_item, state.original_buf)
@@ -330,8 +328,8 @@ M.config = {
330328
end,
331329
desc = "Add symbol to CodeCompanion",
332330
},
333-
{
334-
key = "<C-t>",
331+
avante = {
332+
keys = "<C-t>",
335333
handler = function(items_or_item)
336334
if type(items_or_item) == "table" and items_or_item[1] then
337335
M.add_symbol_to_avante(items_or_item, state.original_buf)
@@ -1053,7 +1051,7 @@ local function show_picker(selectaItems, notify_opts)
10531051
end
10541052
end,
10551053
},
1056-
keymaps = M.config.keymaps,
1054+
custom_keymaps = M.config.custom_keymaps,
10571055
-- TODO: Enable multiselect if configured
10581056
multiselect = {
10591057
enabled = M.config.multiselect.enabled,

0 commit comments

Comments
 (0)