Skip to content

Commit dd25267

Browse files
committed
feat: feat: add fuzzy_finder_mappings to customize mappings for filter popup in fuzzy_finder_mode
related issue: #451 (comment)
1 parent 20c2f2f commit dd25267

File tree

3 files changed

+40
-14
lines changed

3 files changed

+40
-14
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,14 @@ use {
292292
["<c-x>"] = "clear_filter",
293293
["[g"] = "prev_git_modified",
294294
["]g"] = "next_git_modified",
295-
}
295+
},
296+
297+
fuzzy_finder_mappings = { -- define keymaps for filter popup window in fuzzy_finder_mode
298+
["<down>"] = "move_cursor_down",
299+
["<C-n>"] = "move_cursor_down",
300+
["<up>"] = "move_cursor_up",
301+
["<C-p>"] = "move_cursor_up",
302+
},
296303
}
297304
},
298305
buffers = {

lua/neo-tree/defaults.lua

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,13 @@ local config = {
369369
["."] = "set_root",
370370
["[g"] = "prev_git_modified",
371371
["]g"] = "next_git_modified",
372-
}
372+
},
373+
fuzzy_finder_mappings = { -- define keymaps for filter popup window in fuzzy_finder_mode
374+
["<down>"] = "move_cursor_down",
375+
["<C-n>"] = "move_cursor_down",
376+
["<up>"] = "move_cursor_up",
377+
["<C-p>"] = "move_cursor_up",
378+
},
373379
},
374380
async_directory_scan = "auto", -- "auto" means refreshes are async, but it's synchronous when called from the Neotree commands.
375381
-- "always" means directory scans are always async.

lua/neo-tree/sources/filesystem/lib/filter.lua

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,31 @@ local vim = vim
44
local Input = require("nui.input")
55
local event = require("nui.utils.autocmd").event
66
local fs = require("neo-tree.sources.filesystem")
7-
local inputs = require("neo-tree.ui.inputs")
87
local popups = require("neo-tree.ui.popups")
98
local renderer = require("neo-tree.ui.renderer")
109
local utils = require("neo-tree.utils")
1110
local log = require("neo-tree.log")
1211
local manager = require("neo-tree.sources.manager")
13-
local renderer = require("neo-tree.ui.renderer")
1412

1513
local M = {}
1614

15+
local cmds = {
16+
move_cursor_down = function(state, scroll_padding)
17+
renderer.focus_node(state, nil, true, 1, scroll_padding)
18+
end,
19+
20+
move_cursor_up = function(state, scroll_padding)
21+
renderer.focus_node(state, nil, true, -1, scroll_padding)
22+
vim.cmd("redraw!")
23+
end,
24+
}
25+
26+
local function create_input_mapping_handle(cmd, state, scroll_padding)
27+
return function()
28+
cmd(state, scroll_padding)
29+
end
30+
end
31+
1732
M.show_filter = function(state, search_as_you_type, fuzzy_finder_mode, use_fzy)
1833
local popup_options
1934
local winid = vim.api.nvim_get_current_win()
@@ -203,17 +218,15 @@ M.show_filter = function(state, search_as_you_type, fuzzy_finder_mode, use_fzy)
203218
end, { once = true })
204219

205220
if fuzzy_finder_mode then
206-
local move_cursor_down = function()
207-
renderer.focus_node(state, nil, true, 1, scroll_padding)
208-
end
209-
local move_cursor_up = function()
210-
renderer.focus_node(state, nil, true, -1, scroll_padding)
211-
vim.cmd("redraw!")
221+
local config = require("neo-tree").config
222+
for lhs, cmd_name in pairs(config.filesystem.window.fuzzy_finder_mappings) do
223+
local cmd = cmds[cmd_name]
224+
if cmd then
225+
input:map("i", lhs, create_input_mapping_handle(cmd, state, scroll_padding), { noremap = true })
226+
else
227+
log.warn(string.format('Invalid command in fuzzy_finder_mappings: %s = %s', lhs, cmd_name))
228+
end
212229
end
213-
input:map("i", "<down>", move_cursor_down, { noremap = true })
214-
input:map("i", "<C-n>", move_cursor_down, { noremap = true })
215-
input:map("i", "<up>", move_cursor_up, { noremap = true })
216-
input:map("i", "<C-p>", move_cursor_up, { noremap = true })
217230
end
218231
end
219232

0 commit comments

Comments
 (0)