Skip to content

Commit 3bb24bb

Browse files
authored
fix(builtin.colorscheme): improve enable_preview behavior (#3244)
* fix(builtin.colorscheme): don't preview colors when enable_preview=false * fix(builtin.colorscheme): update preview on mouse click
1 parent 3b1600d commit 3bb24bb

File tree

1 file changed

+20
-24
lines changed

1 file changed

+20
-24
lines changed

lua/telescope/builtin/__internal.lua

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -985,7 +985,7 @@ end
985985
internal.colorscheme = function(opts)
986986
local before_background = vim.o.background
987987
local before_color = vim.api.nvim_exec2("colorscheme", { output = true }).output
988-
local need_restore = true
988+
local need_restore = not not opts.enable_preview
989989

990990
local colors = opts.colors or { before_color }
991991
if not vim.tbl_contains(colors, before_color) then
@@ -1049,30 +1049,10 @@ internal.colorscheme = function(opts)
10491049
return
10501050
end
10511051

1052-
actions.close(prompt_bufnr)
10531052
need_restore = false
1053+
actions.close(prompt_bufnr)
10541054
vim.cmd.colorscheme(selection.value)
10551055
end)
1056-
action_set.shift_selection:enhance {
1057-
post = function()
1058-
local selection = action_state.get_selected_entry()
1059-
if selection == nil then
1060-
utils.__warn_no_selection "builtin.colorscheme"
1061-
return
1062-
end
1063-
need_restore = true
1064-
if opts.enable_preview then
1065-
vim.cmd.colorscheme(selection.value)
1066-
end
1067-
end,
1068-
}
1069-
actions.close:enhance {
1070-
post = function()
1071-
if need_restore then
1072-
vim.cmd.colorscheme(before_color)
1073-
end
1074-
end,
1075-
}
10761056
return true
10771057
end,
10781058
on_complete = {
@@ -1082,8 +1062,9 @@ internal.colorscheme = function(opts)
10821062
utils.__warn_no_selection "builtin.colorscheme"
10831063
return
10841064
end
1085-
need_restore = true
1086-
vim.cmd.colorscheme(selection.value)
1065+
if opts.enable_preview then
1066+
vim.cmd.colorscheme(selection.value)
1067+
end
10871068
end,
10881069
},
10891070
})
@@ -1098,6 +1079,21 @@ internal.colorscheme = function(opts)
10981079
vim.cmd.colorscheme(before_color)
10991080
end
11001081
end
1082+
1083+
-- rewrite picker.set_selection so that color schemes can be previewed when the current
1084+
-- selection is shifted using the keyboard or if an item is clicked with the mouse
1085+
local set_selection = picker.set_selection
1086+
picker.set_selection = function(self, row)
1087+
set_selection(self, row)
1088+
local selection = action_state.get_selected_entry()
1089+
if selection == nil then
1090+
utils.__warn_no_selection "builtin.colorscheme"
1091+
return
1092+
end
1093+
if opts.enable_preview then
1094+
vim.cmd.colorscheme(selection.value)
1095+
end
1096+
end
11011097
end
11021098

11031099
picker:find()

0 commit comments

Comments
 (0)