Skip to content

Commit

Permalink
fix(builtin.colorscheme): preview colorscheme without previewer (#3097)
Browse files Browse the repository at this point in the history
* fix: enable_preview true

* fix: remove buflisted check and colorscheme command in the buffer previewer

---------

Co-authored-by: nobrain <moviemaker@alfonso>
  • Loading branch information
MovieMaker93 and nobrain committed May 17, 2024
1 parent ccaeeb5 commit 9661012
Showing 1 changed file with 44 additions and 39 deletions.
83 changes: 44 additions & 39 deletions lua/telescope/builtin/__internal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -999,45 +999,20 @@ internal.colorscheme = function(opts)
local bufnr = vim.api.nvim_get_current_buf()
local p = vim.api.nvim_buf_get_name(bufnr)

-- don't need previewer
if vim.fn.buflisted(bufnr) ~= 1 then
local deleted = false
local function del_win(win_id)
if win_id and vim.api.nvim_win_is_valid(win_id) then
utils.buf_delete(vim.api.nvim_win_get_buf(win_id))
pcall(vim.api.nvim_win_close, win_id, true)
-- show current buffer content in previewer
previewer = previewers.new_buffer_previewer {
get_buffer_by_name = function()
return p
end,
define_preview = function(self)
if vim.loop.fs_stat(p) then
conf.buffer_previewer_maker(p, self.state.bufnr, { bufname = self.state.bufname })
else
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
vim.api.nvim_buf_set_lines(self.state.bufnr, 0, -1, false, lines)
end
end

previewer = previewers.new {
preview_fn = function(_, entry, status)
if not deleted then
deleted = true
if status.layout.preview then
del_win(status.layout.preview.winid)
del_win(status.layout.preview.border.winid)
end
end
vim.cmd.colorscheme(entry.value)
end,
}
else
-- show current buffer content in previewer
previewer = previewers.new_buffer_previewer {
get_buffer_by_name = function()
return p
end,
define_preview = function(self, entry)
if vim.loop.fs_stat(p) then
conf.buffer_previewer_maker(p, self.state.bufnr, { bufname = self.state.bufname })
else
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
vim.api.nvim_buf_set_lines(self.state.bufnr, 0, -1, false, lines)
end
vim.cmd.colorscheme(entry.value)
end,
}
end
end,
}
end

local picker = pickers.new(opts, {
Expand All @@ -1059,9 +1034,39 @@ internal.colorscheme = function(opts)
need_restore = false
vim.cmd.colorscheme(selection.value)
end)

action_set.shift_selection:enhance {
post = function()
local selection = action_state.get_selected_entry()
if selection == nil then
utils.__warn_no_selection "builtin.colorscheme"
return
end
need_restore = true
if opts.enable_preview then
vim.cmd.colorscheme(selection.value)
end
end,
}
actions.close:enhance {
post = function()
if need_restore then
vim.cmd.colorscheme(before_color)
end
end,
}
return true
end,
on_complete = {
function()
local selection = action_state.get_selected_entry()
if selection == nil then
utils.__warn_no_selection "builtin.colorscheme"
return
end
need_restore = true
vim.cmd.colorscheme(selection.value)
end,
},
})

if opts.enable_preview then
Expand Down

0 comments on commit 9661012

Please sign in to comment.