Skip to content

Commit

Permalink
fix(preview): setup image.nvim if needed (#1662)
Browse files Browse the repository at this point in the history
  • Loading branch information
pynappo authored Jan 24, 2025
1 parent c2c5f6f commit 7120b20
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/bug_report.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ body:
"nvim-lua/plenary.nvim",
"nvim-tree/nvim-web-devicons", -- not strictly required, but recommended
"MunifTanjim/nui.nvim",
-- "3rd/image.nvim", -- Optional image support
-- { "3rd/image.nvim", opts = {} }, -- Optional image support
},
opts = {
-- fill any relevant options here
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ so we can fix it.
"nvim-lua/plenary.nvim",
"nvim-tree/nvim-web-devicons", -- not strictly required, but recommended
"MunifTanjim/nui.nvim",
-- "3rd/image.nvim", -- Optional image support in preview window: See `# Preview Mode` for more information
-- {"3rd/image.nvim", opts = {}}, -- Optional image support in preview window: See `# Preview Mode` for more information
}
}
```
Expand Down
29 changes: 20 additions & 9 deletions lua/neo-tree/sources/common/preview.lua
Original file line number Diff line number Diff line change
Expand Up @@ -273,24 +273,35 @@ end

---@param winid number
---@param bufnr number
---@return Image[]|false result Images if the buffer was successfully hijacked, otherwise false
---@return boolean hijacked Whether the buffer was successfully hijacked.
local function try_load_image_nvim_buf(winid, bufnr)
-- notify only image.nvim to let it try and hijack
local image_augroup = vim.api.nvim_create_augroup("image.nvim", { clear = false })
if #vim.api.nvim_get_autocmds({ group = image_augroup }) == 0 then
local image_available, image = pcall(require, "image")
if not image_available then
local image_nvim_url = "https://github.com/3rd/image.nvim"
log.debug("You'll need to install image.nvim to use this command: " .. image_nvim_url)
return false
end
log.warn("image.nvim was not setup. Calling require('image').setup().")
image.setup()
image_augroup = vim.api.nvim_create_augroup("image.nvim", { clear = false })
end

vim.opt.eventignore:remove("BufWinEnter")
vim.api.nvim_win_call(winid, function()
vim.api.nvim_exec_autocmds("BufWinEnter", { group = "image.nvim", buffer = bufnr })
local ok = pcall(vim.api.nvim_win_call, winid, function()
vim.api.nvim_exec_autocmds("BufWinEnter", { group = image_augroup, buffer = bufnr })
end)
vim.opt.eventignore:append("BufWinEnter")
if vim.bo[bufnr].filetype ~= "image_nvim" then
if not ok then
log.debug("image.nvim doesn't have any file patterns to hijack.")
return false
end
local success, mod = pcall(require, "image")
if not success or not mod.hijack_buffer then
local image_nvim_url = "https://github.com/3rd/image.nvim"
log.debug("You'll need to install image.nvim to use this command: " .. image_nvim_url)
if vim.bo[bufnr].filetype ~= "image_nvim" then
return false
end
return mod.get_images({ buffer = bufnr, window = winid })
return true
end

---Set the buffer in the preview window without executing BufEnter or BufWinEnter autocommands.
Expand Down

0 comments on commit 7120b20

Please sign in to comment.