Skip to content

Commit fba6d1f

Browse files
authored
feat(preview): add snacks.image integration (#1815)
1 parent 51907ba commit fba6d1f

File tree

15 files changed

+210
-71
lines changed

15 files changed

+210
-71
lines changed

.github/workflows/.luarc-5.1.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"$PWD/.dependencies/pack/vendor/start/plenary.nvim",
1919
"$PWD/.dependencies/pack/vendor/start/nui.nvim",
2020
"$PWD/.dependencies/pack/vendor/start/nvim-web-devicons",
21+
"$PWD/.dependencies/pack/vendor/start/snacks.nvim",
2122
"${3rd}/luassert",
2223
"${3rd}/busted",
2324
"${3rd}/luv",

.github/workflows/.luarc-luajit-master.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"$PWD/.dependencies/pack/vendor/start/plenary.nvim",
1919
"$PWD/.dependencies/pack/vendor/start/nui.nvim",
2020
"$PWD/.dependencies/pack/vendor/start/nvim-web-devicons",
21+
"$PWD/.dependencies/pack/vendor/start/snacks.nvim",
2122
"${3rd}/luassert",
2223
"${3rd}/busted",
2324
"${3rd}/luv",

.lazy.lua

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
local root = vim.fs.find({ "neo-tree.nvim" }, { upward = true })[1]
2+
local deps_dir = root .. "/.dependencies/pack/vendor/start"
3+
return {
4+
{
5+
"folke/snacks.nvim",
6+
dir = deps_dir .. "/snacks.nvim",
7+
},
8+
{
9+
"MunifTanjim/nui.nvim",
10+
dir = deps_dir .. "/nui.nvim",
11+
},
12+
{
13+
"nvim-tree/nvim-web-devicons",
14+
dir = deps_dir .. "/nvim-web-devicons",
15+
},
16+
{
17+
"nvim-lua/plenary.nvim",
18+
dir = deps_dir .. "/plenary.nvim",
19+
},
20+
}

.luarc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"$PWD/.dependencies/pack/vendor/start/plenary.nvim",
1919
"$PWD/.dependencies/pack/vendor/start/nui.nvim",
2020
"$PWD/.dependencies/pack/vendor/start/nvim-web-devicons",
21+
"$PWD/.dependencies/pack/vendor/start/snacks.nvim",
2122
"${3rd}/luassert",
2223
"${3rd}/busted",
2324
"${3rd}/luv",

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ $(DEPS)/nvim-web-devicons: $(DEPS)
2727
$(DEPS)/plenary.nvim: $(DEPS)
2828
@test -d "$(DEPS)/plenary.nvim" || git clone https://github.com/nvim-lua/plenary.nvim "$(DEPS)/plenary.nvim"
2929

30-
setup: $(DEPS)/nui.nvim $(DEPS)/nvim-web-devicons $(DEPS)/plenary.nvim
30+
$(DEPS)/snacks.nvim: $(DEPS)
31+
@test -d "$(DEPS)/snacks.nvim" || git clone https://github.com/folke/snacks.nvim "$(DEPS)/snacks.nvim"
32+
33+
setup: $(DEPS)/nui.nvim $(DEPS)/nvim-web-devicons $(DEPS)/plenary.nvim $(DEPS)/snacks.nvim
3134
@echo "[setup] environment ready"
3235

3336
.PHONY: clean

README.md

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ so we can fix it.
6060
> You do not need to call `require('neo-tree').setup({ ... })` for Neo-tree to work. `setup()` is only used for
6161
> configuration.
6262
63-
#### Minimal Example for Lazy:
63+
#### Minimal Example for lazy.nvim:
6464
```lua
6565
{
6666
"nvim-neo-tree/neo-tree.nvim",
@@ -69,13 +69,16 @@ so we can fix it.
6969
"nvim-lua/plenary.nvim",
7070
"nvim-tree/nvim-web-devicons", -- not strictly required, but recommended
7171
"MunifTanjim/nui.nvim",
72-
-- {"3rd/image.nvim", opts = {}}, -- Optional image support in preview window: See `# Preview Mode` for more information
72+
-- Optional image support for file preview: See `# Preview Mode` for more information.
73+
-- {"3rd/image.nvim", opts = {}},
74+
-- OR use snacks.nvim's image module:
75+
-- "folke/snacks.nvim",
7376
},
7477
lazy = false, -- neo-tree will lazily load itself
7578
---@module "neo-tree"
7679
---@type neotree.Config?
7780
opts = {
78-
-- fill any relevant options here
81+
-- add options here
7982
},
8083
}
8184
```
@@ -89,7 +92,10 @@ use({
8992
"nvim-lua/plenary.nvim",
9093
"nvim-tree/nvim-web-devicons", -- not strictly required, but recommended
9194
"MunifTanjim/nui.nvim",
92-
-- "3rd/image.nvim", -- Optional image support in preview window: See `# Preview Mode` for more information
95+
-- Optional image support in preview window: See `# Preview Mode` for more information
96+
-- { "3rd/image.nvim", config = function() require('image').setup({}) end },
97+
-- OR use snacks.nvim's image module:
98+
-- "folke/snacks.nvim",
9399
}
94100
})
95101
```
@@ -153,10 +159,6 @@ return {
153159
},
154160
},
155161
lazy = false,
156-
-----Instead of using `config`, you can use `opts` instead, if you'd like:
157-
-----@module "neo-tree"
158-
-----@type neotree.Config
159-
--opts = {},
160162
config = function()
161163
-- If you want icons for diagnostic errors, you'll need to define them somewhere.
162164
-- In Neovim v0.10+, you can configure them in vim.diagnostic.config(), like:
@@ -178,6 +180,8 @@ return {
178180
-- vim.fn.sign_define("DiagnosticSignInfo", { text = " ", texthl = "DiagnosticSignInfo" })
179181
-- vim.fn.sign_define("DiagnosticSignHint", { text = "󰌵", texthl = "DiagnosticSignHint" })
180182

183+
vim.keymap.set("n", "<leader>e", "<Cmd>Neotree reveal<CR>")
184+
181185
require("neo-tree").setup({
182186
close_if_last_window = false, -- Close Neo-tree if it is the last window left in the tab
183187
popup_border_style = "NC", -- or "" to use 'winborder' on Neovim v0.11+
@@ -300,7 +304,14 @@ return {
300304
["<2-LeftMouse>"] = "open",
301305
["<cr>"] = "open",
302306
["<esc>"] = "cancel", -- close preview or floating neo-tree window
303-
["P"] = { "toggle_preview", config = { use_float = true, use_image_nvim = true } },
307+
["P"] = {
308+
"toggle_preview",
309+
config = {
310+
use_float = true,
311+
use_snacks_image = true,
312+
use_image_nvim = true,
313+
},
314+
},
304315
-- Read `# Preview Mode` for more information
305316
["l"] = "focus_preview",
306317
["S"] = "open_split",
@@ -507,8 +518,6 @@ return {
507518
},
508519
},
509520
})
510-
511-
vim.keymap.set("n", "<leader>e", "<Cmd>Neotree reveal<CR>")
512521
end,
513522
},
514523
}
@@ -807,6 +816,7 @@ require("neo-tree").setup({
807816
config = {
808817
use_float = false,
809818
-- use_image_nvim = true,
819+
-- use_snacks_image = true,
810820
-- title = 'Neo-tree Preview',
811821
},
812822
},
@@ -831,11 +841,13 @@ window as being used as a preview.
831841

832842
#### Image Support in Preview Mode
833843

834-
If you have [3rd/image.nvim](https://github.com/3rd/image.nvim) installed, preview
844+
If you have [folke/snacks.nvim](https://github.com/folke/snacks.nvim/blob/main/docs/image.md)
845+
or [3rd/image.nvim](https://github.com/3rd/image.nvim) installed, preview
835846
mode supports image rendering by default using kitty graphics protocol or ueberzug
836847
([Video](https://user-images.githubusercontent.com/41065736/277180763-b7152637-f310-43a5-b8c3-4bcba135629d.mp4)).
837-
However, if you do not want this feature, you can disable it by changing the option
838-
`use_image_nvim = false` in the mappings config mentioned above.
848+
849+
However, if you do not want this feature, you can disable it by setting
850+
`use_snacks_image = false` or `use_image_nvim = false` in the mappings config mentioned above.
839851

840852
## Configuration and Customization
841853

doc/neo-tree.txt

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,14 @@ an existing split by configuring the command like this:
430430
require("neo-tree").setup({
431431
window = {
432432
mappings = {
433-
["P"] = { "toggle_preview", config = { use_float = false, use_image_nvim = true } },
433+
["P"] = {
434+
"toggle_preview",
435+
config = {
436+
use_float = false,
437+
use_snacks_image = true,
438+
use_image_nvim = true
439+
}
440+
},
434441
["l"] = "focus_preview",
435442
["<C-b>"] = { "scroll_preview", config = {direction = 10} },
436443
["<C-f>"] = { "scroll_preview", config = {direction = -10} },
@@ -450,10 +457,15 @@ variable `neo_tree_preview` set to `1` to indicate that it is being used as a
450457
preview window. You can refer to this in statusline and winbar configs to mark a
451458
window as being used as a preview.
452459

453-
If you have [3rd/image.nvim](https://github.com/3rd/image.nvim) installed, preview
454-
mode supports image rendering by default using kitty graphics protocol or ueberzug.
455-
However, if you do not want this feature, you can disable it by changing the option
456-
`use_image_nvim = false` in the mappings config mentioned above.
460+
If you have folke/snacks.nvim or 3rd/image.nvim installed, preview mode supports
461+
image rendering by default using kitty graphics protocol or ueberzug.
462+
463+
- https://github.com/folke/snacks.nvim/blob/main/docs/image.md
464+
- https://github.com/3rd/image.nvim
465+
466+
However, if you do not want this feature, you can disable it by setting
467+
`use_snacks_image = false` or `use_image_nvim = false` in the mappings config
468+
mentioned above.
457469

458470
CUSTOM MAPPINGS *neo-tree-custom-mappings*
459471

lua/neo-tree/collections.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ function Queue:for_each(func)
9696
node_is_next = true
9797
self._list:remove_node(node_to_remove)
9898
elseif type(result) == "table" then
99-
if type(result.handled) == "boolean" and result.handled == true then
99+
if result.handled == true then
100100
log.trace(
101101
"Handler ",
102102
node.value.id,

lua/neo-tree/defaults.lua

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -393,11 +393,15 @@ local config = {
393393
["<cr>"] = "open",
394394
-- ["<cr>"] = { "open", config = { expand_nested_files = true } }, -- expand nested file takes precedence
395395
["<esc>"] = "cancel", -- close preview or floating neo-tree window
396-
["P"] = { "toggle_preview", config = {
397-
use_float = true,
398-
use_image_nvim = false,
399-
-- title = "Neo-tree Preview", -- You can define a custom title for the preview floating window.
400-
} },
396+
["P"] = {
397+
"toggle_preview",
398+
config = {
399+
use_float = true,
400+
use_snacks_image = true,
401+
use_image_nvim = true,
402+
-- title = "Neo-tree Preview", -- You can define a custom title for the preview floating window.
403+
}
404+
},
401405
["<C-f>"] = { "scroll_preview", config = {direction = -10} },
402406
["<C-b>"] = { "scroll_preview", config = {direction = 10} },
403407
["l"] = "focus_preview",

lua/neo-tree/events/init.lua

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ local q = require("neo-tree.events.queue")
22
local log = require("neo-tree.log")
33
local utils = require("neo-tree.utils")
44

5-
---@class neotree.Event.Functions
5+
---@class neotree.event.Functions
66
local M = {
77
-- Well known event names, you can make up your own
88
AFTER_RENDER = "after_render",
@@ -31,6 +31,7 @@ local M = {
3131
NEO_TREE_WINDOW_AFTER_OPEN = "neo_tree_window_after_open",
3232
NEO_TREE_WINDOW_BEFORE_CLOSE = "neo_tree_window_before_close",
3333
NEO_TREE_WINDOW_BEFORE_OPEN = "neo_tree_window_before_open",
34+
NEO_TREE_PREVIEW_BEFORE_RENDER = "neo_tree_preview_before_render",
3435
VIM_AFTER_SESSION_LOAD = "vim_after_session_load",
3536
VIM_BUFFER_ADDED = "vim_buffer_added",
3637
VIM_BUFFER_CHANGED = "vim_buffer_changed",
@@ -60,7 +61,7 @@ local parse_autocmd_string = function(autocmds)
6061
return parsed[1], parsed[2]
6162
end
6263

63-
---@param event_name neotree.Event|string
64+
---@param event_name neotree.EventName|string
6465
---@param autocmds string[]
6566
---@param debounce_frequency integer?
6667
---@param seed_fn function?
@@ -79,7 +80,7 @@ M.define_autocmd_event = function(event_name, autocmds, debounce_frequency, seed
7980
group = augroup,
8081
nested = nested,
8182
callback = function(args)
82-
---@class neotree.Event.Autocmd.CallbackArgs : neotree._vim.api.keyset.create_autocmd.callback_args
83+
---@class neotree.event.Autocmd.CallbackArgs : neotree._vim.api.keyset.create_autocmd.callback_args
8384
---@field afile string
8485
local event_args = args --[[@as neotree._vim.api.keyset.create_autocmd.callback_args]]
8586
event_args.afile = args.file or ""

0 commit comments

Comments
 (0)