Skip to content

Commit

Permalink
chore: clear previewer if no item is selected
Browse files Browse the repository at this point in the history
  • Loading branch information
Conni2461 committed Jun 13, 2022
1 parent 53c8a7c commit 49ccfcb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
7 changes: 3 additions & 4 deletions lua/telescope/pickers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -965,6 +965,9 @@ function Picker:set_selection(row)
state.set_global_key("selected_entry", entry)

if not entry then
-- also refresh previewer when there is no entry selected, so the preview window is cleared
self._selection_entry = entry
self:refresh_previewer()
return
end

Expand Down Expand Up @@ -1068,10 +1071,6 @@ end
--- Refresh the previewer based on the current `status` of the picker
function Picker:refresh_previewer()
local status = state.get_status(self.prompt_bufnr)
if not self._selection_entry then
-- if selection_entry is nil there is nothing to be previewed
return
end
if self.previewer and status.preview_win and a.nvim_win_is_valid(status.preview_win) then
self:_increment "previewed"

Expand Down
13 changes: 13 additions & 0 deletions lua/telescope/previewers/previewer.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
local utils = require "telescope.utils"

local Previewer = {}
Previewer.__index = Previewer

Expand Down Expand Up @@ -25,11 +27,19 @@ function Previewer:new(opts)
_send_input = opts.send_input,
_scroll_fn = opts.scroll_fn,
preview_fn = opts.preview_fn,
_empty_bufnr = nil,
}, Previewer)
end

function Previewer:preview(entry, status)
if not entry then
if not self._empty_bufnr then
self._empty_bufnr = vim.api.nvim_create_buf(false, true)
end

if vim.api.nvim_buf_is_valid(self._empty_bufnr) then
vim.api.nvim_win_set_buf(status.preview_win, self._empty_bufnr)
end
return
end

Expand Down Expand Up @@ -57,6 +67,9 @@ function Previewer:title(entry, dynamic)
end

function Previewer:teardown()
if self._empty_bufnr then
utils.buf_delete(self._empty_bufnr)
end
if self._teardown_func then
self:_teardown_func()
end
Expand Down

0 comments on commit 49ccfcb

Please sign in to comment.