telescope.lua 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. -- Function for image preview with image.nvim
  2. -- Still experimental and freeze:
  3. -- https://github.com/3rd/image.nvim/issues/183
  4. --
  5. -- function telescope_image_preview()
  6. -- local supported_images = { "svg", "png", "jpg", "jpeg", "gif", "webp", "avif" }
  7. -- local from_entry = require("telescope.from_entry")
  8. -- local Path = require("plenary.path")
  9. -- local conf = require("telescope.config").values
  10. -- local Previewers = require("telescope.previewers")
  11. --
  12. -- local previewers = require("telescope.previewers")
  13. -- local image_api = require("image")
  14. --
  15. -- local is_image_preview = false
  16. -- local image = nil
  17. -- local last_file_path = ""
  18. --
  19. -- local is_supported_image = function(filepath)
  20. -- local split_path = vim.split(filepath:lower(), ".", { plain = true })
  21. -- local extension = split_path[#split_path]
  22. -- return vim.tbl_contains(supported_images, extension)
  23. -- end
  24. --
  25. -- local delete_image = function()
  26. -- if not image then
  27. -- return
  28. -- end
  29. --
  30. -- image:clear()
  31. --
  32. -- is_image_preview = false
  33. -- end
  34. --
  35. -- local create_image = function(filepath, winid, bufnr)
  36. -- image = image_api.hijack_buffer(filepath, winid, bufnr)
  37. --
  38. -- if not image then
  39. -- return
  40. -- end
  41. --
  42. -- vim.schedule(function()
  43. -- image:render()
  44. -- end)
  45. --
  46. -- is_image_preview = true
  47. -- end
  48. --
  49. -- local function defaulter(f, default_opts)
  50. -- default_opts = default_opts or {}
  51. -- return {
  52. -- new = function(opts)
  53. -- if conf.preview == false and not opts.preview then
  54. -- return false
  55. -- end
  56. -- opts.preview = type(opts.preview) ~= "table" and {} or opts.preview
  57. -- if type(conf.preview) == "table" then
  58. -- for k, v in pairs(conf.preview) do
  59. -- opts.preview[k] = vim.F.if_nil(opts.preview[k], v)
  60. -- end
  61. -- end
  62. -- return f(opts)
  63. -- end,
  64. -- __call = function()
  65. -- local ok, err = pcall(f(default_opts))
  66. -- if not ok then
  67. -- error(debug.traceback(err))
  68. -- end
  69. -- end,
  70. -- }
  71. -- end
  72. --
  73. -- -- NOTE: Add teardown to cat previewer to clear image when close Telescope
  74. -- local file_previewer = defaulter(function(opts)
  75. -- opts = opts or {}
  76. -- local cwd = opts.cwd or vim.loop.cwd()
  77. -- return Previewers.new_buffer_previewer({
  78. -- title = "File Preview",
  79. -- dyn_title = function(_, entry)
  80. -- return Path:new(from_entry.path(entry, true)):normalize(cwd)
  81. -- end,
  82. --
  83. -- get_buffer_by_name = function(_, entry)
  84. -- return from_entry.path(entry, true)
  85. -- end,
  86. --
  87. -- define_preview = function(self, entry, _)
  88. -- local p = from_entry.path(entry, true)
  89. -- if p == nil or p == "" then
  90. -- return
  91. -- end
  92. --
  93. -- conf.buffer_previewer_maker(p, self.state.bufnr, {
  94. -- bufname = self.state.bufname,
  95. -- winid = self.state.winid,
  96. -- preview = opts.preview,
  97. -- })
  98. -- end,
  99. --
  100. -- teardown = function(_)
  101. -- if is_image_preview then
  102. -- delete_image()
  103. -- end
  104. -- end,
  105. -- })
  106. -- end, {})
  107. --
  108. -- local buffer_previewer_maker = function(filepath, bufnr, opts)
  109. -- -- NOTE: Clear image when preview other file
  110. -- if is_image_preview and last_file_path ~= filepath then
  111. -- delete_image()
  112. -- end
  113. --
  114. -- last_file_path = filepath
  115. --
  116. -- if is_supported_image(filepath) then
  117. -- create_image(filepath, opts.winid, bufnr)
  118. -- else
  119. -- previewers.buffer_previewer_maker(filepath, bufnr, opts)
  120. -- end
  121. -- end
  122. --
  123. -- return { buffer_previewer_maker = buffer_previewer_maker, file_previewer = file_previewer.new }
  124. -- end
  125. --
  126. -- local image_preview = telescope_image_preview()
  127. return {
  128. {
  129. "nvim-telescope/telescope.nvim",
  130. tag = "0.1.8",
  131. dependencies = { "nvim-lua/plenary.nvim" },
  132. config = function()
  133. require("telescope").setup({
  134. defaults = {
  135. -- file_previewer = image_preview.file_previewer,
  136. -- buffer_previewer_maker = image_preview.buffer_previewer_maker,
  137. -- Default configuration for telescope goes here:
  138. -- config_key = value,
  139. mappings = {
  140. i = {
  141. -- map actions.which_key to <C-h> (default: <C-/>)
  142. -- actions.which_key shows the mappings for your picker,
  143. -- e.g. git_{create, delete, ...}_branch for the git_branches picker
  144. ["<C-h>"] = "which_key",
  145. },
  146. },
  147. },
  148. pickers = {
  149. -- Default configuration for builtin pickers goes here:
  150. -- picker_name = {
  151. -- picker_config_key = value,
  152. -- ...
  153. -- }
  154. -- Now the picker_config_key will be applied every time you call this
  155. -- builtin picker
  156. },
  157. extensions = {
  158. -- file_browser = { hijack_netrw = true },
  159. -- Your extension configuration goes here:
  160. -- extension_name = {
  161. -- extension_config_key = value,
  162. -- }
  163. -- please take a look at the readme of the extension you want to configure
  164. },
  165. })
  166. end,
  167. },
  168. {
  169. "nvim-telescope/telescope-ui-select.nvim",
  170. config = function()
  171. -- This is your opts table
  172. require("telescope").setup({
  173. extensions = {
  174. ["ui-select"] = {
  175. require("telescope.themes").get_dropdown({
  176. -- even more opts
  177. }),
  178. -- pseudo code / specification for writing custom displays, like the one
  179. -- for "codeactions"
  180. -- specific_opts = {
  181. -- [kind] = {
  182. -- make_indexed = function(items) -> indexed_items, width,
  183. -- make_displayer = function(widths) -> displayer
  184. -- make_display = function(displayer) -> function(e)
  185. -- make_ordinal = function(e) -> string
  186. -- },
  187. -- -- for example to disable the custom builtin "codeactions" display
  188. -- do the following
  189. -- codeactions = false,
  190. -- }
  191. },
  192. },
  193. })
  194. -- To get ui-select loaded and working with telescope, you need to call
  195. -- load_extension, somewhere after setup function:
  196. require("telescope").load_extension("ui-select")
  197. end,
  198. },
  199. }