Skip to content

Commit 5f4783c

Browse files
authored
feat(preview): add use_float option for preview mode, closes #134 (#502)
1 parent 9f5119a commit 5f4783c

File tree

8 files changed

+230
-53
lines changed

8 files changed

+230
-53
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ use {
198198
},
199199
["<2-LeftMouse>"] = "open",
200200
["<cr>"] = "open",
201+
["<esc>"] = "revert_preview",
202+
["P"] = { "toggle_preview", config = { use_float = true } },
201203
["S"] = "open_split",
202204
["s"] = "open_vsplit",
203205
-- ["S"] = "split_with_window_picker",

doc/neo-tree.txt

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Configuration ............... |neo-tree-configuration|
1313
Setup ..................... |neo-tree-setup|
1414
Source Selector ........... |neo-tree-source-selector|
1515
Filtered Items ............ |neo-tree-filtered-items|
16+
Preview Mode .............. |neo-tree-preview-mode|
1617
Hijack Netrw Behavior ..... |neo-tree-netrw-hijack|
1718
Component Configs ......... |neo-tree-component-configs|
1819
Git Status ................ |neo-tree-git-status|
@@ -169,9 +170,9 @@ in a custom mapping.
169170

170171
Note: The "selected" item is the line the cursor is currently on.
171172

172-
< = prev_source Switches to the previous source.
173+
< = prev_source: Switches to the previous source.
173174

174-
> = next_source Switches to the next source.
175+
> = next_source: Switches to the next source.
175176

176177
<bs> = navigate_up: Moves the root directory up one level.
177178

@@ -192,6 +193,12 @@ z = close_all_nodes: Close all nodes in the tree.
192193

193194
expand_all_nodes: Expand all directory nodes in the tree recursively.
194195

196+
P = toggle_preview: Toggles "preview mode", see |neo-tree-preview-mode|
197+
198+
<esc> = revert_preview: Ends "preview_mode" if it is enabled, and reverts
199+
any preview windows to what was being shown before
200+
preview mode began.
201+
195202
S = open_split: Same as open, but opens in a new horizontal split.
196203

197204
s = open_vsplit: Same as open, but opens in a vertical split.
@@ -311,6 +318,34 @@ f = filter_on_submit: Same as above, but does not search until you hit
311318

312319
<C-x> = clear_filter: Removes the filter.
313320

321+
PREVIEW MODE *neo-tree-preview-mode*
322+
323+
Preview mode will temporarily show whatever file the cursor is on without
324+
switching focus from the Neo-tree window. By default, files will be previewed
325+
in a new floating window. This can also be configured to automatically choose
326+
an existing split by configuring the command like this:
327+
328+
>
329+
require("neo-tree").setup({
330+
window = {
331+
mappings = {
332+
["P"] = { "toggle_preview", config = { use_float = false } },
333+
}
334+
}
335+
})
336+
<
337+
Anything that causes Neo-tree to lose focus will end preview mode. When
338+
`use_float = false`, the window that was taken over by preview mode will revert
339+
back to whatever was shown in that window before preview mode began.
340+
341+
If you want to work with the floating preview mode window in autocmds or other
342+
custom code, the window will have the `neo-tree-preview` filetype.
343+
344+
When preview mode is not using floats, the window will have the window local
345+
variable `neo_tree_preview` set to `1` to indicate that it is being used as a
346+
preview window. You can refer to this in statusline and winbar configs to mark a
347+
window as being used as a preview.
348+
314349

315350
CUSTOM MAPPINGS *neo-tree-custom-mappings*
316351

lua/neo-tree/defaults.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,8 @@ local config = {
309309
},
310310
["<2-LeftMouse>"] = "open",
311311
["<cr>"] = "open",
312+
["<esc>"] = "revert_preview",
313+
["P"] = { "toggle_preview", config = { use_float = true } },
312314
["S"] = "open_split",
313315
-- ["S"] = "split_with_window_picker",
314316
["s"] = "open_vsplit",
@@ -317,7 +319,6 @@ local config = {
317319
-- ["<cr>"] = "open_drop",
318320
-- ["t"] = "open_tab_drop",
319321
["w"] = "open_with_window_picker",
320-
--["P"] = "toggle_preview",
321322
["C"] = "close_node",
322323
["z"] = "close_all_nodes",
323324
--["Z"] = "expand_all_nodes",

lua/neo-tree/sources/common/commands.lua

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -347,13 +347,14 @@ end
347347

348348
M.preview = function(state)
349349
local node = state.tree:get_node()
350-
if state.current_position == "current" or node.type == "directory" then
350+
if node.type == "directory" then
351351
return
352352
end
353353

354354
if not state.preview then
355355
state.preview = Preview:new(state)
356356
else
357+
state.preview.active = true
357358
state.preview:findWindow(state)
358359
end
359360

@@ -363,20 +364,23 @@ M.preview = function(state)
363364
local path = node.path or node:get_id()
364365
local bufnr = extra.bufnr or vim.fn.bufadd(path)
365366

366-
if bufnr and bufnr > 0 then
367-
state.preview:preview(bufnr, position, end_position)
367+
if bufnr and bufnr > 0 and state.preview then
368+
if renderer.is_window_valid(state.preview.winid) then
369+
state.preview:preview(bufnr, position, end_position)
370+
else
371+
log.warn("Preview window is not valid")
372+
Preview.dispose(state)
373+
end
368374
end
369375
end
370376

371377
M.revert_preview = function(state)
372-
if state.preview and state.preview.active then
373-
state.preview:revert()
374-
end
378+
Preview.dispose(state)
375379
end
376380

377381
M.toggle_preview = function(state)
378-
if state.preview and state.preview.active then
379-
state.preview:revert()
382+
if state.preview then
383+
M.revert_preview(state)
380384
else
381385
state.commands.preview(state)
382386
if not state.preview then
@@ -385,25 +389,20 @@ M.toggle_preview = function(state)
385389
local preview_event = {
386390
event = events.VIM_CURSOR_MOVED,
387391
handler = function()
392+
if not state.preview then
393+
return
394+
end
388395
if vim.api.nvim_get_current_win() == state.winid then
389-
state.commands.preview(state)
396+
if state.preview.active then
397+
state.commands.preview(state)
398+
end
399+
else
400+
Preview.dispose(state)
390401
end
391402
end,
392403
id = "preview-event",
393404
}
394-
local preview_buf_leave_event = {
395-
event = events.NEO_TREE_BUFFER_LEAVE,
396-
handler = function()
397-
local winid, bufnr = state.preview.winid, state.preview.bufnr
398-
state.preview:revert()
399-
if vim.api.nvim_get_current_win() == winid then
400-
vim.api.nvim_set_current_buf(bufnr)
401-
end
402-
end,
403-
id = "preview-buf-leave-event",
404-
}
405405
state.preview:subscribe(state.name, preview_event)
406-
state.preview:subscribe(state.name, preview_buf_leave_event)
407406
end
408407
end
409408

@@ -424,6 +423,7 @@ local open_with_cmd = function(state, open_cmd, toggle_directory, open_file)
424423
end
425424

426425
local function open()
426+
M.revert_preview(state)
427427
local path = node.path or node:get_id()
428428
if type(open_file) == "function" then
429429
open_file(state, path, open_cmd)

0 commit comments

Comments
 (0)