-
Notifications
You must be signed in to change notification settings - Fork 50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Telescope preview issue #183
Comments
I've added the echo statement in the local create_image = function(filepath, winid, bufnr)
image = image_api.from_file(filepath, { window = winid, buffer = bufnr })
if not image then
return
end
image:render()
vim.api.nvim_echo({
{ 'image.is_rendered: ' .. tostring(image.is_rendered), nil },
}, false, {})
is_image_preview = true
end And on the first try it prints But on the second try it returns the |
I'm not sure whether it should be the actual solution, but I've wrapped it with the local create_image = function(filepath, winid, bufnr)
image = image_api.from_file(filepath, { window = winid, buffer = bufnr })
if not image then
return
end
vim.defer_fn(function()
image:render()
end, 0)
is_image_preview = true
end |
@miszo I'd love to play around with this telescope previewer. Do you have a repo to install this as a telescope extension? |
@exosyphon, didn't go that deep to set up this as a telescope extension. Here's the code for:
|
Moved the code into a single file, based on example above, would be awesome if there was an extension for this tho: function telescope_image_preview()
local supported_images = { "svg", "png", "jpg", "jpeg", "gif", "webp", "avif" }
local from_entry = require("telescope.from_entry")
local Path = require("plenary.path")
local conf = require("telescope.config").values
local Previewers = require("telescope.previewers")
local previewers = require("telescope.previewers")
local image_api = require("image")
local is_image_preview = false
local image = nil
local last_file_path = ""
local is_supported_image = function(filepath)
local split_path = vim.split(filepath:lower(), ".", { plain = true })
local extension = split_path[#split_path]
return vim.tbl_contains(supported_images, extension)
end
local delete_image = function()
if not image then
return
end
image:clear()
is_image_preview = false
end
local create_image = function(filepath, winid, bufnr)
image = image_api.hijack_buffer(filepath, winid, bufnr)
if not image then
return
end
vim.schedule(function()
image:render()
end)
is_image_preview = true
end
local function defaulter(f, default_opts)
default_opts = default_opts or {}
return {
new = function(opts)
if conf.preview == false and not opts.preview then
return false
end
opts.preview = type(opts.preview) ~= "table" and {} or opts.preview
if type(conf.preview) == "table" then
for k, v in pairs(conf.preview) do
opts.preview[k] = vim.F.if_nil(opts.preview[k], v)
end
end
return f(opts)
end,
__call = function()
local ok, err = pcall(f(default_opts))
if not ok then
error(debug.traceback(err))
end
end,
}
end
-- NOTE: Add teardown to cat previewer to clear image when close Telescope
local file_previewer = defaulter(function(opts)
opts = opts or {}
local cwd = opts.cwd or vim.loop.cwd()
return Previewers.new_buffer_previewer({
title = "File Preview",
dyn_title = function(_, entry)
return Path:new(from_entry.path(entry, true)):normalize(cwd)
end,
get_buffer_by_name = function(_, entry)
return from_entry.path(entry, true)
end,
define_preview = function(self, entry, _)
local p = from_entry.path(entry, true)
if p == nil or p == "" then
return
end
conf.buffer_previewer_maker(p, self.state.bufnr, {
bufname = self.state.bufname,
winid = self.state.winid,
preview = opts.preview,
})
end,
teardown = function(_)
if is_image_preview then
delete_image()
end
end,
})
end, {})
local buffer_previewer_maker = function(filepath, bufnr, opts)
-- NOTE: Clear image when preview other file
if is_image_preview and last_file_path ~= filepath then
delete_image()
end
last_file_path = filepath
if is_supported_image(filepath) then
create_image(filepath, opts.winid, bufnr)
else
previewers.buffer_previewer_maker(filepath, bufnr, opts)
end
end
return { buffer_previewer_maker = buffer_previewer_maker, file_previewer = file_previewer.new }
end local image_preview = telescope_image_preview()
require("telescope").setup({
defaults = {
file_previewer = image_preview.file_previewer,
buffer_previewer_maker = image_preview.buffer_previewer_maker,
},
extensions = {
file_browser = { hijack_netrw = true },
},
}) |
Could be something going wrong with the decorations provider or auto-commands. |
Thanks for the reply, i turned my custom auto-commands and plugins off and nvim only crashes sometimes now in combination with Maybe it has something to do with: nvim-neotest/neotest#441, i'll wait for that |
But did image.nvim ever render an image in the session? I think we shouldn't even set up the handlers and providers until an image needs to be rendered, I'll looking into it and make sure we don't! |
No it did not, thanks! |
Is anyone else getting noticeable hang in the main loop when first selecting / rendering an image entry using this code? I tried removing all Image:render() calls and replacing create_image with just The freezing seems to happen in image.lua's from_file for me. Am I misunderstanding how plenary's async.void can be used? Hows/is it possible to create the image objects outside the main loop to prevent freezing neovim? @3rd @sand4rt |
https://www.lua.org/pil/9.4.html#:~:text=However%2C%20unlike%20%22real%22%20multithreading%2C%20coroutines%20are%20non%20preemptive Edit: this is again a limitation of working with the rock, we'll have better alternatives that non-blocking, but we can do it in Lua as well if the magick rock will survive http://lua-users.org/wiki/ThreadsTutorial |
Since if (opts.ft == nil or opts.ft == "") and possible_binary then
putils.set_preview_message(bufnr, opts.winid, "Binary cannot be previewed", opts.preview.msg_bg_fillchar)
return
end |
I'm trying to use your plugin to render previews in telescope. I've created a bufferpreviewer_maker like this:
And I have the problem when I try to preview the image for the first time – it doesn't render, it renders on the second attempt.
I think that the problem is in the create_image function.
Any ideas how to resolve the issue?
My env:
Plugin setup:
The text was updated successfully, but these errors were encountered: