From 6e20108c4a8128782f534f5fe90c757b44212ef6 Mon Sep 17 00:00:00 2001 From: Aleksandr Vedeneev Date: Wed, 15 May 2024 19:25:12 +0400 Subject: [PATCH] feat(preview): Scroll Preview window command and bindings (#1470) --- doc/neo-tree.txt | 9 +++++++++ lua/neo-tree/defaults.lua | 2 ++ lua/neo-tree/sources/common/commands.lua | 4 ++++ lua/neo-tree/sources/common/preview.lua | 14 ++++++++++++++ 4 files changed, 29 insertions(+) diff --git a/doc/neo-tree.txt b/doc/neo-tree.txt index ccccb50b..18be245e 100644 --- a/doc/neo-tree.txt +++ b/doc/neo-tree.txt @@ -224,6 +224,12 @@ P = toggle_preview: Toggles "preview mode", see |neo-tree-preview-mode| l = focus_preview: Focus the active preview window + = scroll_preview: Scrolls preview window down (without focusing it) + see |neo-tree-preview-mode| for params + + = scroll_preview: Scrolls preview window up (without focusing it) + see |neo-tree-preview-mode| for params + = revert_preview: Ends "preview_mode" if it is enabled, and reverts any preview windows to what was being shown before preview mode began. @@ -404,6 +410,9 @@ an existing split by configuring the command like this: window = { mappings = { ["P"] = { "toggle_preview", config = { use_float = false, use_image_nvim = true } }, + ["l"] = "focus_preview", + [""] = { "scroll_preview", config = {direction = 10} }, + [""] = { "scroll_preview", config = {direction = -10} }, } } }) diff --git a/lua/neo-tree/defaults.lua b/lua/neo-tree/defaults.lua index 44736046..733dcc07 100644 --- a/lua/neo-tree/defaults.lua +++ b/lua/neo-tree/defaults.lua @@ -368,6 +368,8 @@ local config = { -- [""] = { "open", config = { expand_nested_files = true } }, -- expand nested file takes precedence [""] = "cancel", -- close preview or floating neo-tree window ["P"] = { "toggle_preview", config = { use_float = true, use_image_nvim = false } }, + [""] = { "scroll_preview", config = {direction = -10} }, + [""] = { "scroll_preview", config = {direction = 10} }, ["l"] = "focus_preview", ["S"] = "open_split", -- ["S"] = "split_with_window_picker", diff --git a/lua/neo-tree/sources/common/commands.lua b/lua/neo-tree/sources/common/commands.lua index 05a41533..5b668384 100644 --- a/lua/neo-tree/sources/common/commands.lua +++ b/lua/neo-tree/sources/common/commands.lua @@ -664,6 +664,10 @@ M.toggle_preview = function(state) Preview.toggle(state) end +M.scroll_preview = function(state) + Preview.scroll(state) +end + M.focus_preview = function() Preview.focus() end diff --git a/lua/neo-tree/sources/common/preview.lua b/lua/neo-tree/sources/common/preview.lua index a11f596c..153cd888 100644 --- a/lua/neo-tree/sources/common/preview.lua +++ b/lua/neo-tree/sources/common/preview.lua @@ -435,4 +435,18 @@ Preview.focus = function() end end +Preview.scroll = function(state) + local direction = state.config.direction + -- NOTE: Chars below are raw escape codes for / + local input = direction < 0 and [[]] or [[]] + local count = math.abs(direction) + + if Preview:is_active() then + vim.api.nvim_win_call(instance.winid, function() + vim.cmd([[normal! ]] .. count .. input) + end) + end + +end + return Preview