Skip to content

Commit 7120b20

Browse files
authored
fix(preview): setup image.nvim if needed (#1662)
1 parent c2c5f6f commit 7120b20

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed

.github/ISSUE_TEMPLATE/bug_report.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ body:
7676
"nvim-lua/plenary.nvim",
7777
"nvim-tree/nvim-web-devicons", -- not strictly required, but recommended
7878
"MunifTanjim/nui.nvim",
79-
-- "3rd/image.nvim", -- Optional image support
79+
-- { "3rd/image.nvim", opts = {} }, -- Optional image support
8080
},
8181
opts = {
8282
-- fill any relevant options here

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ so we can fix it.
6565
"nvim-lua/plenary.nvim",
6666
"nvim-tree/nvim-web-devicons", -- not strictly required, but recommended
6767
"MunifTanjim/nui.nvim",
68-
-- "3rd/image.nvim", -- Optional image support in preview window: See `# Preview Mode` for more information
68+
-- {"3rd/image.nvim", opts = {}}, -- Optional image support in preview window: See `# Preview Mode` for more information
6969
}
7070
}
7171
```

lua/neo-tree/sources/common/preview.lua

+20-9
Original file line numberDiff line numberDiff line change
@@ -273,24 +273,35 @@ end
273273

274274
---@param winid number
275275
---@param bufnr number
276-
---@return Image[]|false result Images if the buffer was successfully hijacked, otherwise false
276+
---@return boolean hijacked Whether the buffer was successfully hijacked.
277277
local function try_load_image_nvim_buf(winid, bufnr)
278278
-- notify only image.nvim to let it try and hijack
279+
local image_augroup = vim.api.nvim_create_augroup("image.nvim", { clear = false })
280+
if #vim.api.nvim_get_autocmds({ group = image_augroup }) == 0 then
281+
local image_available, image = pcall(require, "image")
282+
if not image_available then
283+
local image_nvim_url = "https://github.com/3rd/image.nvim"
284+
log.debug("You'll need to install image.nvim to use this command: " .. image_nvim_url)
285+
return false
286+
end
287+
log.warn("image.nvim was not setup. Calling require('image').setup().")
288+
image.setup()
289+
image_augroup = vim.api.nvim_create_augroup("image.nvim", { clear = false })
290+
end
291+
279292
vim.opt.eventignore:remove("BufWinEnter")
280-
vim.api.nvim_win_call(winid, function()
281-
vim.api.nvim_exec_autocmds("BufWinEnter", { group = "image.nvim", buffer = bufnr })
293+
local ok = pcall(vim.api.nvim_win_call, winid, function()
294+
vim.api.nvim_exec_autocmds("BufWinEnter", { group = image_augroup, buffer = bufnr })
282295
end)
283296
vim.opt.eventignore:append("BufWinEnter")
284-
if vim.bo[bufnr].filetype ~= "image_nvim" then
297+
if not ok then
298+
log.debug("image.nvim doesn't have any file patterns to hijack.")
285299
return false
286300
end
287-
local success, mod = pcall(require, "image")
288-
if not success or not mod.hijack_buffer then
289-
local image_nvim_url = "https://github.com/3rd/image.nvim"
290-
log.debug("You'll need to install image.nvim to use this command: " .. image_nvim_url)
301+
if vim.bo[bufnr].filetype ~= "image_nvim" then
291302
return false
292303
end
293-
return mod.get_images({ buffer = bufnr, window = winid })
304+
return true
294305
end
295306

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

0 commit comments

Comments
 (0)