Skip to content

feat(fuzzy_finder): normal mode navigation, normalize mappings #1788

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions lua/neo-tree/setup/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,26 @@ local hijack_cursor = require("neo-tree.sources.common.hijack_cursor")

local M = {}

---@param config neotree.Config.Base
local normalize_mappings = function(config)
if config == nil then
return false
---@param source_config neotree.Config.Source
local normalize_mappings = function(source_config)
if source_config == nil then
return
end
local mappings = vim.tbl_get(config, { "window", "mappings" })
local mappings = vim.tbl_get(source_config, { "window", "mappings" })
if mappings then
local fixed = mapping_helper.normalize_map(mappings)
config.window.mappings = fixed
return true
else
return false
source_config.window.mappings = fixed
end
end

local normalize_fuzzy_mappings = function(source_config)
if source_config == nil then
return
end
local mappings = vim.tbl_get(source_config, { "window", "fuzzy_finder_mappings" })
if mappings then
local fixed = mapping_helper.normalize_map(mappings)
source_config.window.fuzzy_finder_mappings = fixed
end
end

Expand Down Expand Up @@ -562,6 +570,11 @@ M.merge_config = function(user_config)
log.debug("Sources to load: ", vim.inspect(all_sources))
require("neo-tree.command.parser").setup(all_source_names)

normalize_fuzzy_mappings(default_config.filesystem)
normalize_fuzzy_mappings(user_config.filesystem)
if user_config.use_default_mappings == false then
default_config.filesystem.window.fuzzy_finder_mappings = {}
end
-- setup the default values for all sources
normalize_mappings(default_config)
normalize_mappings(user_config)
Expand Down Expand Up @@ -613,7 +626,6 @@ M.merge_config = function(user_config)
user_config[source_name].window.position = "left"
end
end
--print(vim.inspect(default_config.filesystem))

-- local orig_sources = user_config.sources and user_config.sources or {}

Expand Down
4 changes: 2 additions & 2 deletions lua/neo-tree/sources/common/components.lua
Original file line number Diff line number Diff line change
Expand Up @@ -679,13 +679,13 @@ end

---@class (exact) neotree.Component.Common.SymlinkTarget : neotree.Component
---@field [1] "symlink_target"?
---@field text_format string
---@field text_format string?

---@param config neotree.Component.Common.SymlinkTarget
M.symlink_target = function(config, node, _)
if node.is_link then
return {
text = string.format(config.text_format, node.link_to),
text = string.format(config.text_format or "-> %s", node.link_to),
highlight = config.highlight or highlights.SYMBOLIC_LINK_TARGET,
}
else
Expand Down
33 changes: 23 additions & 10 deletions lua/neo-tree/sources/common/filters/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -211,25 +211,38 @@ M.show_filter = function(state, search_as_you_type, keep_filter_on_submit)
end
restore_height()
end,

noop = nil,
none = nil,
}

-- create mappings and autocmd
input:map("i", "<C-w>", "<C-S-w>", { noremap = true })
if config.use_default_mappings then
input:map("i", "<C-w>", "<C-S-w>", { noremap = true })
input:map(
"n",
"j",
utils.wrap(cmds.move_cursor_down, state, scroll_padding),
{ noremap = true }
)
input:map("n", "k", utils.wrap(cmds.move_cursor_up, state, scroll_padding), { noremap = true })
end

local config = require("neo-tree").config
for lhs, cmd_name in pairs(config.filesystem.window.fuzzy_finder_mappings) do
local t = type(cmd_name)
local falsy_mappings = { "noop", "none" }
for lhs, cmd in pairs(config.filesystem.window.fuzzy_finder_mappings) do
local t = type(cmd)
if t == "string" then
local cmd = cmds[cmd_name]
if cmd then
input:map("i", lhs, utils.wrap(cmd, state, scroll_padding), { noremap = true })
else
log.warn(string.format("Invalid command in fuzzy_finder_mappings: %s = %s", lhs, cmd_name))
local command = cmds[cmd]
if command then
input:map("i", lhs, utils.wrap(command, state, scroll_padding), { noremap = true })
elseif not vim.tbl_contains(falsy_mappings, cmds) then
log.warn(string.format("Invalid command in fuzzy_finder_mappings: %s = %s", lhs, command))
end
elseif t == "function" then
input:map("i", lhs, utils.wrap(cmd_name, state, scroll_padding), { noremap = true })
input:map("i", lhs, utils.wrap(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))
end
end
end
Expand Down
41 changes: 21 additions & 20 deletions lua/neo-tree/sources/filesystem/lib/filter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -215,35 +215,36 @@ M.show_filter = function(state, search_as_you_type, fuzzy_finder_mode, use_fzy)

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

input:map("i", "<C-w>", "<C-S-w>", { noremap = true })
local config = require("neo-tree").config
if config.use_default_mappings then
input:map("i", "<C-w>", "<C-S-w>", { noremap = true })
input:map(
"n",
"j",
utils.wrap(cmds.move_cursor_down, state, scroll_padding),
{ noremap = true }
)
input:map("n", "k", utils.wrap(cmds.move_cursor_up, state, scroll_padding), { noremap = true })
end

if not fuzzy_finder_mode then
return
end

for lhs, cmd_name in pairs(require("neo-tree").config.filesystem.window.fuzzy_finder_mappings) do
local t = type(cmd_name)
local falsy_mappings = { "noop", "none" }
for lhs, cmd in pairs(config.filesystem.window.fuzzy_finder_mappings) do
local t = type(cmd)
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 }
)
else
log.warn(string.format("Invalid command in fuzzy_finder_mappings: %s = %s", lhs, cmd_name))
local command = cmds[cmd]
if command then
input:map("i", lhs, utils.wrap(command, state, scroll_padding), { noremap = true })
elseif not vim.tbl_contains(falsy_mappings, cmds) then
log.warn(string.format("Invalid command in fuzzy_finder_mappings: %s = %s", lhs, command))
end
elseif t == "function" then
input:map(
"i",
lhs,
create_input_mapping_handle(cmd_name, state, scroll_padding),
{ noremap = true }
)
input:map("i", lhs, utils.wrap(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))
end
end
end
Expand Down
10 changes: 4 additions & 6 deletions lua/neo-tree/types/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

---@class neotree.Config.Source
---@field window neotree.Config.Window?
---@field commands neotree.Config.Commands?
---@field renderers neotree.Component[]?

---@class neotree.Config.SourceSelector.Item
Expand Down Expand Up @@ -58,13 +59,13 @@
---@field width string|number?

---@class neotree.Config.Window.Popup
---@field title fun(state:table):string?
---@field title (fun(state:table):string)?
---@field size neotree.Config.Window.Size?
---@field border neotree.Config.BorderStyle?

---@alias neotree.Config.Window.Command string|function|neotree.Config.Window.Command.Configured

---@class (exact) neotree.Config.Window.Commands
---@class (exact) neotree.Config.Commands
---@field [string] function

---@class (exact) neotree.Config.Window.Mappings
Expand Down Expand Up @@ -101,7 +102,7 @@

---@alias neotree.Config.BorderStyle "NC"|"rounded"|"single"|"solid"|"double"|""

---@class (exact) neotree.Config.Base
---@class (exact) neotree.Config.Base : neotree.Config.Source
---@field sources string[]
---@field add_blank_line_at_top boolean
---@field auto_clean_after_session_restore boolean
Expand Down Expand Up @@ -131,10 +132,7 @@
---@field source_selector neotree.Config.SourceSelector
---@field event_handlers? neotree.Event.Handler[]
---@field default_component_configs neotree.Config.ComponentDefaults
---@field renderers neotree.Config.Renderers
---@field nesting_rules neotree.FileNesting.Rule[]
---@field commands table<string, fun()>
---@field window neotree.Config.Window
---
---@field filesystem neotree.Config.Filesystem
---@field buffers neotree.Config.Buffers
Expand Down
3 changes: 3 additions & 0 deletions lua/neo-tree/ui/popups.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ local M = {}
local winborder_option_exists = vim.fn.exists("&winborder") > 0
-- These borders will cause errors when trying to display border text with them
local invalid_borders = { "", "none", "shadow" }
---@param title string
---@param min_width integer?
---@param override_options table?
M.popup_options = function(title, min_width, override_options)
if string.len(title) ~= 0 then
title = " " .. title .. " "
Expand Down
Loading