Skip to content

fix(preview): make scroll_preview fallback when inactive #1651

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

Merged
merged 3 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lua/neo-tree/sources/common/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -669,8 +669,8 @@ M.toggle_preview = function(state)
Preview.toggle(state)
end

M.scroll_preview = function(state)
Preview.scroll(state)
M.scroll_preview = function(state, fallback)
Preview.scroll(state, fallback)
end

M.focus_preview = function()
Expand Down
13 changes: 9 additions & 4 deletions lua/neo-tree/sources/common/preview.lua
Original file line number Diff line number Diff line change
Expand Up @@ -489,15 +489,20 @@ Preview.focus = function()
end
end

Preview.scroll = function(state)
local CTRL_E = utils.keycode("<c-e>")
local CTRL_Y = utils.keycode("<c-y>")
Preview.scroll = function(state, fallback)
local direction = state.config.direction
-- NOTE: Chars below are raw escape codes for <Ctrl-E>/<Ctrl-Y>
local input = direction < 0 and [[]] or [[]]
local input = direction < 0 and CTRL_E or CTRL_Y
local count = math.abs(direction)

if Preview:is_active() then
vim.api.nvim_win_call(instance.winid, function()
vim.cmd([[normal! ]] .. count .. input)
vim.cmd(("normal! %s%s"):format(count, input))
end)
else
vim.api.nvim_buf_call(state.bufnr, function()
vim.cmd(("normal! %s"):format(utils.keycode(fallback)))
end)
end
end
Expand Down
4 changes: 2 additions & 2 deletions lua/neo-tree/ui/renderer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ local log = require("neo-tree.log")
local windows = require("neo-tree.ui.windows")

local M = { resize_timer_interval = 50 }
local ESC_KEY = vim.api.nvim_replace_termcodes("<ESC>", true, false, true)
local ESC_KEY = utils.keycode("<ESC>")
local default_popup_size = { width = 60, height = "80%" }
local draw, create_tree, render_tree

Expand Down Expand Up @@ -852,7 +852,7 @@ local set_buffer_mappings = function(state)
if type(func) == "function" then
resolved_mappings[cmd].handler = function()
state.config = config
return func(state)
return func(state, cmd)
end
keymap.set(state.bufnr, "n", cmd, resolved_mappings[cmd].handler, map_options)
if type(vfunc) == "function" then
Expand Down
8 changes: 8 additions & 0 deletions lua/neo-tree/utils/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1363,6 +1363,14 @@ M.index_by_path = function(tbl, key)
return value
end

---Backport of vim.keycode
---@see vim.keycode
---@param str string
---@return string representation Internal representation of the keycodes
function M.keycode(str)
return vim.api.nvim_replace_termcodes(str, true, true, true)
end

---Iterate through a table, sorted by its keys.
---Compared to vim.spairs, it also accepts a method that specifies how to sort the table by key.
---
Expand Down