Skip to content

Commit 3b399ce

Browse files
committed
refactor: make Preview a singleton
1 parent 75f9e36 commit 3b399ce

File tree

2 files changed

+68
-64
lines changed

2 files changed

+68
-64
lines changed

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

Lines changed: 5 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -346,64 +346,15 @@ M.delete_visual = function(state, selected_nodes, callback)
346346
end
347347

348348
M.preview = function(state)
349-
local node = state.tree:get_node()
350-
if node.type == "directory" then
351-
return
352-
end
353-
354-
if not state.preview then
355-
state.preview = Preview:new(state)
356-
else
357-
state.preview.active = true
358-
state.preview:findWindow(state)
359-
end
360-
361-
local extra = node.extra or {}
362-
local position = extra.position
363-
local end_position = extra.end_position
364-
local path = node.path or node:get_id()
365-
local bufnr = extra.bufnr or vim.fn.bufadd(path)
366-
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
374-
end
349+
Preview.show(state)
375350
end
376351

377-
M.revert_preview = function(state)
378-
Preview.dispose(state)
352+
M.revert_preview = function()
353+
Preview.hide()
379354
end
380355

381356
M.toggle_preview = function(state)
382-
if state.preview then
383-
M.revert_preview(state)
384-
else
385-
state.commands.preview(state)
386-
if not state.preview then
387-
return
388-
end
389-
local preview_event = {
390-
event = events.VIM_CURSOR_MOVED,
391-
handler = function()
392-
if not state.preview then
393-
return
394-
end
395-
if vim.api.nvim_get_current_win() == state.winid then
396-
if state.preview.active then
397-
state.commands.preview(state)
398-
end
399-
else
400-
Preview.dispose(state)
401-
end
402-
end,
403-
id = "preview-event",
404-
}
405-
state.preview:subscribe(state.name, preview_event)
406-
end
357+
Preview.toggle(state)
407358
end
408359

409360
---Open file or directory
@@ -423,7 +374,7 @@ local open_with_cmd = function(state, open_cmd, toggle_directory, open_file)
423374
end
424375

425376
local function open()
426-
M.revert_preview(state)
377+
M.revert_preview()
427378
local path = node.path or node:get_id()
428379
if type(open_file) == "function" then
429380
open_file(state, path, open_cmd)

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

Lines changed: 63 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ local function create_floating_preview_window(state)
7878
win:mount()
7979
return win
8080
end
81-
Preview = {}
81+
82+
local Preview = {}
83+
local instance = nil
8284

8385
---Creates a new preview.
8486
---@param state table The state of the source.
@@ -107,6 +109,7 @@ end
107109
---@param start_pos table? The (0-indexed) starting position of the previewed text. May be absent.
108110
---@param end_pos table? The (0-indexed) ending position of the previewed text. May be absent
109111
function Preview:preview(bufnr, start_pos, end_pos)
112+
log.warn("Creating preview window")
110113
if self.is_neo_tree_window then
111114
log.error("Could not find appropriate window for preview")
112115
return
@@ -136,15 +139,6 @@ function Preview:preview(bufnr, start_pos, end_pos)
136139
self:highlight()
137140
end
138141

139-
function Preview.dispose(state)
140-
if state.preview and state.preview.active then
141-
if state.preview.active then
142-
state.preview:revert()
143-
end
144-
end
145-
state.preview = nil
146-
end
147-
148142
---Reverts the preview and inactivates it, restoring the preview window to its previous state.
149143
function Preview:revert()
150144
self.active = false
@@ -343,4 +337,63 @@ function Preview:clearHighlight()
343337
end
344338
end
345339

340+
Preview.hide = function()
341+
if instance then
342+
instance:revert()
343+
end
344+
instance = nil
345+
end
346+
347+
Preview.show = function(state)
348+
local node = state.tree:get_node()
349+
if node.type == "directory" then
350+
return
351+
end
352+
353+
if instance then
354+
instance:findWindow(state)
355+
if not renderer.is_window_valid(instance.winid) then
356+
log.warn("Preview window is not valid")
357+
Preview.hide()
358+
instance = Preview:new(state)
359+
end
360+
else
361+
instance = Preview:new(state)
362+
end
363+
364+
local extra = node.extra or {}
365+
local position = extra.position
366+
local end_position = extra.end_position
367+
local path = node.path or node:get_id()
368+
local bufnr = extra.bufnr or vim.fn.bufadd(path)
369+
370+
if bufnr and bufnr > 0 and instance then
371+
instance:preview(bufnr, position, end_position)
372+
end
373+
end
374+
375+
Preview.toggle = function(state)
376+
if instance then
377+
Preview.hide()
378+
else
379+
Preview.show(state)
380+
local preview_event = {
381+
event = events.VIM_CURSOR_MOVED,
382+
handler = function()
383+
if not instance.active then
384+
return
385+
end
386+
if vim.api.nvim_get_current_win() == state.winid then
387+
Preview.show(state)
388+
else
389+
log.debug("Neo-tree window lost focus, disposing preview")
390+
Preview.hide()
391+
end
392+
end,
393+
id = "preview-event",
394+
}
395+
instance:subscribe(state.name, preview_event)
396+
end
397+
end
398+
346399
return Preview

0 commit comments

Comments
 (0)