Skip to content

feat(preview): add force parameter to toggle_preview #1777

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 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
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ return {
["<2-LeftMouse>"] = "open",
["<cr>"] = "open",
["<esc>"] = "cancel", -- close preview or floating neo-tree window
["P"] = { "toggle_preview", config = { use_float = true, use_image_nvim = true } },
["P"] = { "toggle_preview", config = { use_float = true, use_image_nvim = true, force = nil } },
-- Read `# Preview Mode` for more information
["l"] = "focus_preview",
["S"] = "open_split",
Expand All @@ -311,7 +311,6 @@ return {
-- ["<cr>"] = "open_drop",
-- ["t"] = "open_tab_drop",
["w"] = "open_with_window_picker",
--["P"] = "toggle_preview", -- enter preview mode, which shows the current node without focusing
["C"] = "close_node",
-- ['C'] = 'close_all_subnodes',
["z"] = "close_all_nodes",
Expand Down Expand Up @@ -792,6 +791,7 @@ require("neo-tree").setup({
config = {
use_float = false,
-- use_image_nvim = true,
-- force = nil,
-- title = 'Neo-tree Preview',
},
},
Expand All @@ -804,6 +804,24 @@ 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.

The `toggle_preview` command also takes a `force` option which will force the
preview to open/close if set to true/false, respectively. This can be used for
"auto-preview"-like behavior like:

```lua
require('neo-tree').setup({
event_handlers = {
{
event = 'after_render',
handler = function(state)
state.config = { use_float = true, force = true }
state.commands.toggle_preview(state)
end
}
}
})
```

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
Expand Down
23 changes: 22 additions & 1 deletion doc/neo-tree.txt
Original file line number Diff line number Diff line change
Expand Up @@ -430,18 +430,39 @@ 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, force = nil } },
["l"] = "focus_preview",
["<C-b>"] = { "scroll_preview", config = {direction = 10} },
["<C-f>"] = { "scroll_preview", config = {direction = -10} },
}
}
})
<

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.

The `toggle_preview` command also takes a `force` option which will force the
preview to open/close if set to true/false, respectively. This can be used for
"auto-preview"-like behavior like:

>lua
require('neo-tree').setup({
event_handlers = {
{
event = 'after_render',
handler = function(state)
state.config = { use_float = true, force = true }
state.commands.toggle_preview(state)
end
}
}
})
<

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
1 change: 1 addition & 0 deletions lua/neo-tree/defaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@ local config = {
["P"] = { "toggle_preview", config = {
use_float = true,
use_image_nvim = false,
-- force = nil,
-- title = "Neo-tree Preview", -- You can define a custom title for the preview floating window.
} },
["<C-f>"] = { "scroll_preview", config = {direction = -10} },
Expand Down
15 changes: 9 additions & 6 deletions lua/neo-tree/sources/common/preview.lua
Original file line number Diff line number Diff line change
Expand Up @@ -413,10 +413,10 @@ function Preview:clearHighlight()
end
end

local toggle_state = false
local showing = false

Preview.hide = function()
toggle_state = false
showing = false
if instance then
instance:revert()
end
Expand Down Expand Up @@ -448,12 +448,15 @@ Preview.show = function(state)
end

Preview.toggle = function(state)
if toggle_state then
state.config = state.config or {}
local should_hide = state.config.force and (state.config.force == false) or showing
local should_show = state.config.force and (state.config.force == true) or not showing
if should_hide then
Preview.hide()
else
elseif should_show then
Preview.show(state)
if instance and instance.active then
toggle_state = true
showing = true
else
Preview.hide()
return
Expand All @@ -464,7 +467,7 @@ Preview.toggle = function(state)
event = events.VIM_CURSOR_MOVED,
handler = function()
local did_enter_preview = vim.api.nvim_get_current_win() == instance.winid
if not toggle_state or (did_enter_preview and instance.config.use_float) then
if not showing or (did_enter_preview and instance.config.use_float) then
return
end
if vim.api.nvim_get_current_win() == winid then
Expand Down
Loading