diff options
| author | wongxy <xiyao.wong@foxmail.com> | 2021-07-17 00:45:31 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-07-16 18:45:31 +0200 |
| commit | 747396227d1e2283d4d59693ffec571a52195627 (patch) | |
| tree | fb72ce4de7932ba5a99d38279b9756e311960e58 /lua/telescope/builtin | |
| parent | 1866265feaebda2b615fadaa9230ec45958dd210 (diff) | |
feat: enable to preview themes (#980)
Diffstat (limited to 'lua/telescope/builtin')
| -rw-r--r-- | lua/telescope/builtin/init.lua | 1 | ||||
| -rw-r--r-- | lua/telescope/builtin/internal.lua | 91 |
2 files changed, 84 insertions, 8 deletions
diff --git a/lua/telescope/builtin/init.lua b/lua/telescope/builtin/init.lua index 8e7f5ec..0c2c461 100644 --- a/lua/telescope/builtin/init.lua +++ b/lua/telescope/builtin/init.lua @@ -254,6 +254,7 @@ builtin.buffers = require('telescope.builtin.internal').buffers --- Lists available colorschemes and applies them on `<cr>` ---@param opts table: options to pass to the picker +---@field enable_preview boolean: if true, will preview the selected color builtin.colorscheme = require('telescope.builtin.internal').colorscheme --- Lists vim marks and their value, jumps to the mark on `<cr>` diff --git a/lua/telescope/builtin/internal.lua b/lua/telescope/builtin/internal.lua index b157ee2..dc1b167 100644 --- a/lua/telescope/builtin/internal.lua +++ b/lua/telescope/builtin/internal.lua @@ -635,26 +635,101 @@ internal.buffers = function(opts) end internal.colorscheme = function(opts) - local colors = vim.list_extend(opts.colors or {}, vim.fn.getcompletion('', 'color')) + local before_color = vim.api.nvim_exec('colorscheme', true) + local need_restore = true - pickers.new(opts,{ - prompt = 'Change Colorscheme', + local colors = opts.colors or { before_color } + if not vim.tbl_contains(colors, before_color) then + table.insert(colors, 1, before_color) + end + + colors = vim.list_extend( + colors, + vim.tbl_filter(function(color) + return color ~= before_color + end, vim.fn.getcompletion( + '', + 'color' + )) + ) + + local previewer + if opts.enable_preview then + -- define previewer + 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) + end + end + + previewer = previewers.new { + preview_fn = function(_, entry, status) + if not deleted then + deleted = true + del_win(status.preview_win) + del_win(status.preview_border_win) + 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 + + local picker = pickers.new(opts, { + prompt_title = 'Change Colorscheme', finder = finders.new_table { - results = colors + results = colors, }, - -- TODO: better preview? sorter = conf.generic_sorter(opts), + previewer = previewer, attach_mappings = function(prompt_bufnr) actions.select_default:replace(function() local selection = action_state.get_selected_entry() - actions.close(prompt_bufnr) - vim.cmd("colorscheme " .. selection.value) + + need_restore = false + vim.cmd('colorscheme ' .. selection.value) end) return true + end, + }) + + if opts.enable_preview then + -- rewrite picker.close_windows. restore color if needed + local close_windows = picker.close_windows + picker.close_windows = function(status) + close_windows(status) + if need_restore then + vim.cmd('colorscheme ' .. before_color) + end end - }):find() + end + + picker:find() end internal.marks = function(opts) |
