diff --git a/README.md b/README.md index 3ade0996..e8b7d161 100644 --- a/README.md +++ b/README.md @@ -368,6 +368,7 @@ use { [""] = "move_cursor_down", [""] = "move_cursor_up", [""] = "move_cursor_up", + [""] = "close", -- [''] = function(state, scroll_padding) ... end, }, }, diff --git a/lua/neo-tree/defaults.lua b/lua/neo-tree/defaults.lua index cbd30c77..2e67f12c 100644 --- a/lua/neo-tree/defaults.lua +++ b/lua/neo-tree/defaults.lua @@ -448,6 +448,7 @@ local config = { [""] = "move_cursor_down", [""] = "move_cursor_up", [""] = "move_cursor_up", + [""] = "close" }, }, async_directory_scan = "auto", -- "auto" means refreshes are async, but it's synchronous when called from the Neotree commands. diff --git a/lua/neo-tree/sources/filesystem/lib/filter.lua b/lua/neo-tree/sources/filesystem/lib/filter.lua index 90e7d456..146e92f0 100644 --- a/lua/neo-tree/sources/filesystem/lib/filter.lua +++ b/lua/neo-tree/sources/filesystem/lib/filter.lua @@ -12,17 +12,6 @@ local manager = require("neo-tree.sources.manager") local M = {} -local cmds = { - move_cursor_down = function(state, scroll_padding) - renderer.focus_node(state, nil, true, 1, scroll_padding) - end, - - move_cursor_up = function(state, scroll_padding) - renderer.focus_node(state, nil, true, -1, scroll_padding) - vim.cmd("redraw!") - end, -} - local function create_input_mapping_handle(cmd, state, scroll_padding) return function() cmd(state, scroll_padding) @@ -78,7 +67,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) @@ -194,18 +185,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", "", 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", "", "", { 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 @@ -215,22 +195,50 @@ 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", "", "", { 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 cmds = { + move_cursor_down = function(_state, _scroll_padding) + renderer.focus_node(_state, nil, true, 1, _scroll_padding) + end, + + move_cursor_up = function(_state, _scroll_padding) + renderer.focus_node(_state, nil, true, -1, _scroll_padding) + vim.cmd("redraw!") + end, + + close = function() + close_input() + end, + } + for lhs, cmd_name in pairs(require("neo-tree").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 }) + 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)) + log.warn( + string.format("Invalid command in fuzzy_finder_mappings: %s = %s", lhs, cmd_name) + ) 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