Skip to content
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

fix(preview): scroll_preview fallback default when preview is't active #1651

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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 @@ -664,8 +664,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
14 changes: 9 additions & 5 deletions lua/neo-tree/sources/common/preview.lua
Original file line number Diff line number Diff line change
Expand Up @@ -435,18 +435,22 @@ Preview.focus = function()
end
end

Preview.scroll = function(state)
local CTRL_E = utils.replace_termcodes("<c-e>")
local CTRL_Y = utils.replace_termcodes("<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.replace_termcodes(fallback)))
end)
end

end

return Preview
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.replace_termcodes("<ESC>")
local default_popup_size = { width = 60, height = "80%" }
local draw, create_tree, render_tree

Expand Down Expand Up @@ -851,7 +851,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
9 changes: 9 additions & 0 deletions lua/neo-tree/utils/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1293,4 +1293,13 @@ M.index_by_path = function(tbl, key)
return value
end

---Replaces string of terminal/key-codes with Neovim's representation
---Alias for vim.api.nvim_replace_termcodes(str, true, true, true)
---@see vim.api.nvim_replace_termcodes
---@param str string
---@return string representation Internal representation of the keycodes
function M.replace_termcodes(str)
return vim.api.nvim_replace_termcodes(str, true, true, true)
end

return M