Skip to content

Commit

Permalink
feat: add close to fuzzy_finder_mappings
Browse files Browse the repository at this point in the history
Useful for users who wants to map something else than `<esc>` (e.g.
`<C-e>` or `<C-c>)` to close the fuzzy finder input window
  • Loading branch information
agoodshort committed Sep 23, 2024
1 parent a77af2e commit 7240d7e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 20 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ use {
["<C-n>"] = "move_cursor_down",
["<up>"] = "move_cursor_up",
["<C-p>"] = "move_cursor_up",
["<esc>"] = "close",
-- ['<key>'] = function(state, scroll_padding) ... end,
},
},
Expand Down
1 change: 1 addition & 0 deletions lua/neo-tree/defaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ local config = {
["<C-n>"] = "move_cursor_down",
["<up>"] = "move_cursor_up",
["<C-p>"] = "move_cursor_up",
["<esc>"] = "close"
},
},
async_directory_scan = "auto", -- "auto" means refreshes are async, but it's synchronous when called from the Neotree commands.
Expand Down
50 changes: 30 additions & 20 deletions lua/neo-tree/sources/filesystem/lib/filter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ M.show_filter = function(state, search_as_you_type, fuzzy_finder_mode, use_fzy)
local a_score = result_scores[a.path]
local b_score = result_scores[b.path]
if a_score == nil or b_score == nil then
log.debug(string.format([[Fzy: failed to compare %s: %s, %s: %s]], a.path, a_score, b.path, b_score))
log.debug(
string.format([[Fzy: failed to compare %s: %s, %s: %s]], a.path, a_score, b.path, b_score)
)
local config = require("neo-tree").config
if config.sort_function ~= nil then
return config.sort_function(a, b)
Expand Down Expand Up @@ -194,18 +196,7 @@ M.show_filter = function(state, search_as_you_type, fuzzy_finder_mode, use_fzy)
vim.api.nvim_win_set_height(winid, height)
end
end)
input:map("i", "<esc>", function(bufnr)
vim.cmd("stopinsert")
input:unmount()
if fuzzy_finder_mode and utils.truthy(state.search_pattern) then
fs.reset_search(state, true)
end
restore_height()
end, { noremap = true })

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

input:on({ event.BufLeave, event.BufDelete }, function()
local close_input = function()
vim.cmd("stopinsert")
input:unmount()
-- If this was closed due to submit, that function will handle the reset_search
Expand All @@ -215,22 +206,41 @@ M.show_filter = function(state, search_as_you_type, fuzzy_finder_mode, use_fzy)
end
end, 100)
restore_height()
end, { once = true })
end

input:on({ event.BufLeave, event.BufDelete }, close_input, { once = true })

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

if fuzzy_finder_mode then
local config = require("neo-tree").config
for lhs, cmd_name in pairs(config.filesystem.window.fuzzy_finder_mappings) do
local t = type(cmd_name)
if t == "string" then
local cmd = cmds[cmd_name]
if cmd then
input:map("i", lhs, create_input_mapping_handle(cmd, state, scroll_padding), { noremap = true })
if cmd_name == "close" then
input:map("i", lhs, close_input, { noremap = true })
else
log.warn(string.format("Invalid command in fuzzy_finder_mappings: %s = %s", lhs, cmd_name))
local cmd = cmds[cmd_name]
if cmd then
input:map(
"i",
lhs,
create_input_mapping_handle(cmd, state, scroll_padding),
{ noremap = true }
)
else
log.warn(
string.format("Invalid command in fuzzy_finder_mappings: %s = %s", lhs, cmd_name)
)
end
end
elseif t == "function" then
input:map("i", lhs, create_input_mapping_handle(cmd_name, state, scroll_padding),
{ noremap = true })
input:map(
"i",
lhs,
create_input_mapping_handle(cmd_name, state, scroll_padding),
{ noremap = true }
)
else
log.warn(string.format("Invalid command in fuzzy_finder_mappings: %s = %s", lhs, cmd_name))
end
Expand Down

0 comments on commit 7240d7e

Please sign in to comment.