diff --git a/README.md b/README.md index 3ade0996..bbf60ed6 100644 --- a/README.md +++ b/README.md @@ -714,7 +714,14 @@ an existing split by configuring the command like this: require("neo-tree").setup({ window = { mappings = { - ["P"] = { "toggle_preview", config = { use_float = false, use_image_nvim = true } }, + ["P"] = { + "toggle_preview", + config = { + use_float = false, + use_image_nvim = true, + title = 'Neo-tree Preview', + }, + }, } } }) @@ -724,6 +731,8 @@ Anything that causes Neo-tree to lose focus will end preview mode. When `use_float = false`, the window that was taken over by preview mode will revert back to whatever was shown in that window before preview mode began. +You can choose a custom title for the floating window by setting the `title` option in its config. + If you want to work with the floating preview mode window in autocmds or other custom code, the window will have the `neo-tree-preview` filetype. diff --git a/lua/neo-tree/defaults.lua b/lua/neo-tree/defaults.lua index cbd30c77..f7ba73d4 100644 --- a/lua/neo-tree/defaults.lua +++ b/lua/neo-tree/defaults.lua @@ -378,7 +378,11 @@ local config = { [""] = "open", -- [""] = { "open", config = { expand_nested_files = true } }, -- expand nested file takes precedence [""] = "cancel", -- close preview or floating neo-tree window - ["P"] = { "toggle_preview", config = { use_float = true, use_image_nvim = false } }, + ["P"] = { "toggle_preview", config = { + use_float = true, + use_image_nvim = false, + title = "Neo-tree Preview", -- You can define a custom title for the preview floating window. + } }, [""] = { "scroll_preview", config = {direction = -10} }, [""] = { "scroll_preview", config = {direction = 10} }, ["l"] = "focus_preview", diff --git a/lua/neo-tree/sources/common/preview.lua b/lua/neo-tree/sources/common/preview.lua index 153cd888..eba37a0c 100644 --- a/lua/neo-tree/sources/common/preview.lua +++ b/lua/neo-tree/sources/common/preview.lua @@ -12,6 +12,9 @@ local function create_floating_preview_window(state) local default_position = utils.resolve_config_option(state, "window.position", "left") state.current_position = state.current_position or default_position + local toggle_preview_mapping = utils.table_find_by_value(state.window.mappings, "toggle_preview") + + local title = toggle_preview_mapping and toggle_preview_mapping.config.title or "Neo-tree Preview" local winwidth = vim.api.nvim_win_get_width(state.winid) local winheight = vim.api.nvim_win_get_height(state.winid) local height = vim.o.lines - 4 @@ -58,7 +61,7 @@ local function create_floating_preview_window(state) end local popups = require("neo-tree.ui.popups") - local options = popups.popup_options("Neo-tree Preview", width, { + local options = popups.popup_options(title, width, { ns_id = highlights.ns_id, size = { height = height, width = width }, relative = "editor", @@ -446,7 +449,6 @@ Preview.scroll = function(state) vim.cmd([[normal! ]] .. count .. input) end) end - end return Preview diff --git a/lua/neo-tree/utils/init.lua b/lua/neo-tree/utils/init.lua index 5ff54a0e..1a43d672 100644 --- a/lua/neo-tree/utils/init.lua +++ b/lua/neo-tree/utils/init.lua @@ -1009,6 +1009,53 @@ M.table_merge = function(base_table, override_table) return table_merge_internal(merged_table, override_table) end +---Find the parent table to a target value inside a nested table structure. +--- +---@param tbl table The table to search. +---@param target_value any The value to search for. +---@param max_depth number? Maximum depth to search (default: 5). +---@param max_items number? Maximum number of items to check (default: 555). +---@return table? parent_table The table containing the value, or nil if not found. +M.table_find_by_value = function(tbl, target_value, max_depth, max_items) + max_depth = max_depth or 5 + max_items = max_items or 555 + + local stack = { { tbl, 0 } } + local items_checked = 0 + + while #stack > 0 and items_checked <= max_items do + local current, depth = unpack(table.remove(stack)) + + if depth > max_depth then + goto continue + end + + if type(current) ~= "table" then + goto continue + end + + for _, v in pairs(current) do + items_checked = items_checked + 1 + + if v == target_value then + return current + end + + if type(v) == "table" then + table.insert(stack, { v, depth + 1 }) + end + + if items_checked > max_items then + break + end + end + + ::continue:: + end + + return nil +end + ---Evaluate the truthiness of a value, according to js/python rules. ---@param value any ---@return boolean