From 96610122a40f0bb4ce2e452c6d2429bf093d6700 Mon Sep 17 00:00:00 2001 From: DevFortunato <57877156+MovieMaker93@users.noreply.github.com> Date: Fri, 17 May 2024 05:08:28 +0200 Subject: [PATCH] fix(builtin.colorscheme): preview colorscheme without previewer (#3097) * fix: enable_preview true * fix: remove buflisted check and colorscheme command in the buffer previewer --------- Co-authored-by: nobrain --- lua/telescope/builtin/__internal.lua | 83 +++++++++++++++------------- 1 file changed, 44 insertions(+), 39 deletions(-) diff --git a/lua/telescope/builtin/__internal.lua b/lua/telescope/builtin/__internal.lua index 21735a7..1b18496 100644 --- a/lua/telescope/builtin/__internal.lua +++ b/lua/telescope/builtin/__internal.lua @@ -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, { @@ -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