Skip to content

Commit 73d6337

Browse files
authored
fix(fuzzy): add close command to all fuzzy finders (#1747)
1 parent 5da3489 commit 73d6337

File tree

3 files changed

+65
-66
lines changed

3 files changed

+65
-66
lines changed

lua/neo-tree/sources/common/filters/init.lua

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,6 @@ local fzy = require("neo-tree.sources.common.filters.filter_fzy")
1313

1414
local M = {}
1515

16-
---@enum (key) neotree.FuzzyFinder.Commands
17-
local cmds = {
18-
move_cursor_down = function(state, scroll_padding)
19-
renderer.focus_node(state, nil, true, 1, scroll_padding)
20-
end,
21-
22-
move_cursor_up = function(state, scroll_padding)
23-
renderer.focus_node(state, nil, true, -1, scroll_padding)
24-
vim.cmd("redraw!")
25-
end,
26-
}
27-
2816
---Reset the current filter to the empty string.
2917
---@param state any
3018
---@param refresh boolean? whether to refresh the source tree
@@ -204,16 +192,30 @@ M.show_filter = function(state, search_as_you_type, keep_filter_on_submit)
204192
end
205193
end)
206194

195+
---@enum (key) neotree.FuzzyFinder.Commands
196+
local cmds = {
197+
move_cursor_down = function(state_, scroll_padding_)
198+
renderer.focus_node(state_, nil, true, 1, scroll_padding_)
199+
end,
200+
201+
move_cursor_up = function(state_, scroll_padding_)
202+
renderer.focus_node(state_, nil, true, -1, scroll_padding_)
203+
vim.cmd("redraw!")
204+
end,
205+
206+
close = function()
207+
vim.cmd("stopinsert")
208+
input:unmount()
209+
if utils.truthy(state.search_pattern) then
210+
reset_filter(state, true)
211+
end
212+
restore_height()
213+
end,
214+
}
215+
207216
-- create mappings and autocmd
208217
input:map("i", "<C-w>", "<C-S-w>", { noremap = true })
209-
input:map("i", "<esc>", function()
210-
vim.cmd("stopinsert")
211-
input:unmount()
212-
if utils.truthy(state.search_pattern) then
213-
reset_filter(state, true)
214-
end
215-
restore_height()
216-
end, { noremap = true })
218+
input:map("i", "<esc>", cmds.close, { noremap = true })
217219

218220
local config = require("neo-tree").config
219221
for lhs, cmd_name in pairs(config.filesystem.window.fuzzy_finder_mappings) do

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

Lines changed: 42 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -190,63 +190,60 @@ M.show_filter = function(state, search_as_you_type, fuzzy_finder_mode, use_fzy)
190190
vim.api.nvim_win_set_height(winid, height)
191191
end
192192
end)
193-
local close_input = function()
194-
vim.cmd("stopinsert")
195-
input:unmount()
196-
-- If this was closed due to submit, that function will handle the reset_search
197-
vim.defer_fn(function()
198-
if fuzzy_finder_mode and utils.truthy(state.search_pattern) then
199-
fs.reset_search(state, true)
200-
end
201-
end, 100)
202-
restore_height()
203-
end
193+
local cmds = {
194+
move_cursor_down = function(_state, _scroll_padding)
195+
renderer.focus_node(_state, nil, true, 1, _scroll_padding)
196+
end,
197+
198+
move_cursor_up = function(_state, _scroll_padding)
199+
renderer.focus_node(_state, nil, true, -1, _scroll_padding)
200+
vim.cmd("redraw!")
201+
end,
202+
203+
close = function()
204+
vim.cmd("stopinsert")
205+
input:unmount()
206+
-- If this was closed due to submit, that function will handle the reset_search
207+
vim.defer_fn(function()
208+
if fuzzy_finder_mode and utils.truthy(state.search_pattern) then
209+
fs.reset_search(state, true)
210+
end
211+
end, 100)
212+
restore_height()
213+
end,
214+
}
204215

205-
input:on({ event.BufLeave, event.BufDelete }, close_input, { once = true })
216+
input:on({ event.BufLeave, event.BufDelete }, cmds.close, { once = true })
206217

207218
input:map("i", "<C-w>", "<C-S-w>", { noremap = true })
208219

209-
if fuzzy_finder_mode then
210-
local cmds = {
211-
move_cursor_down = function(_state, _scroll_padding)
212-
renderer.focus_node(_state, nil, true, 1, _scroll_padding)
213-
end,
214-
215-
move_cursor_up = function(_state, _scroll_padding)
216-
renderer.focus_node(_state, nil, true, -1, _scroll_padding)
217-
vim.cmd("redraw!")
218-
end,
219-
220-
close = function()
221-
close_input()
222-
end,
223-
}
224-
for lhs, cmd_name in pairs(require("neo-tree").config.filesystem.window.fuzzy_finder_mappings) do
225-
local t = type(cmd_name)
226-
if t == "string" then
227-
local cmd = cmds[cmd_name]
228-
if cmd then
229-
input:map(
230-
"i",
231-
lhs,
232-
create_input_mapping_handle(cmd, state, scroll_padding),
233-
{ noremap = true }
234-
)
235-
else
236-
log.warn(
237-
string.format("Invalid command in fuzzy_finder_mappings: %s = %s", lhs, cmd_name)
238-
)
239-
end
240-
elseif t == "function" then
220+
if not fuzzy_finder_mode then
221+
return
222+
end
223+
224+
for lhs, cmd_name in pairs(require("neo-tree").config.filesystem.window.fuzzy_finder_mappings) do
225+
local t = type(cmd_name)
226+
if t == "string" then
227+
local cmd = cmds[cmd_name]
228+
if cmd then
241229
input:map(
242230
"i",
243231
lhs,
244-
create_input_mapping_handle(cmd_name, state, scroll_padding),
232+
create_input_mapping_handle(cmd, state, scroll_padding),
245233
{ noremap = true }
246234
)
247235
else
248236
log.warn(string.format("Invalid command in fuzzy_finder_mappings: %s = %s", lhs, cmd_name))
249237
end
238+
elseif t == "function" then
239+
input:map(
240+
"i",
241+
lhs,
242+
create_input_mapping_handle(cmd_name, state, scroll_padding),
243+
{ noremap = true }
244+
)
245+
else
246+
log.warn(string.format("Invalid command in fuzzy_finder_mappings: %s = %s", lhs, cmd_name))
250247
end
251248
end
252249
end

lua/neo-tree/types/config/filesystem.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
---@class neotree.Config.Filesystem.Renderers : neotree.Config.Renderers
3535

3636
---@class neotree.Config.Filesystem.Window : neotree.Config.Source.Window
37-
---@field fuzzy_finder_mappings table<string, neotree.FuzzyFinder.Commands|"close">?
37+
---@field fuzzy_finder_mappings table<string, neotree.FuzzyFinder.Commands>?
3838

3939
---@class (exact) neotree.Config.Filesystem : neotree.Config.Source
4040
---@field async_directory_scan "auto"|"always"|"never"|nil

0 commit comments

Comments
 (0)