Skip to content
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

feat(preview): custom title #1563

Open
wants to merge 2 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
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
},
}
}
})
Expand All @@ -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.

Expand Down
6 changes: 5 additions & 1 deletion lua/neo-tree/defaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,11 @@ local config = {
["<cr>"] = "open",
-- ["<cr>"] = { "open", config = { expand_nested_files = true } }, -- expand nested file takes precedence
["<esc>"] = "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.
} },
["<C-f>"] = { "scroll_preview", config = {direction = -10} },
["<C-b>"] = { "scroll_preview", config = {direction = 10} },
["l"] = "focus_preview",
Expand Down
6 changes: 4 additions & 2 deletions lua/neo-tree/sources/common/preview.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -446,7 +449,6 @@ Preview.scroll = function(state)
vim.cmd([[normal! ]] .. count .. input)
end)
end

end

return Preview
47 changes: 47 additions & 0 deletions lua/neo-tree/utils/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down