diff --git a/lua/neo-tree/sources/common/commands.lua b/lua/neo-tree/sources/common/commands.lua index 4caf1e68..2dba0880 100644 --- a/lua/neo-tree/sources/common/commands.lua +++ b/lua/neo-tree/sources/common/commands.lua @@ -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() diff --git a/lua/neo-tree/sources/common/preview.lua b/lua/neo-tree/sources/common/preview.lua index 153cd888..c9cd2e9a 100644 --- a/lua/neo-tree/sources/common/preview.lua +++ b/lua/neo-tree/sources/common/preview.lua @@ -435,18 +435,22 @@ Preview.focus = function() end end -Preview.scroll = function(state) +local CTRL_E = utils.replace_termcodes("") +local CTRL_Y = utils.replace_termcodes("") +Preview.scroll = function(state, fallback) local direction = state.config.direction - -- NOTE: Chars below are raw escape codes for / - 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 diff --git a/lua/neo-tree/ui/renderer.lua b/lua/neo-tree/ui/renderer.lua index a67dd70f..24e1f4f6 100644 --- a/lua/neo-tree/ui/renderer.lua +++ b/lua/neo-tree/ui/renderer.lua @@ -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("", true, false, true) +local ESC_KEY = utils.replace_termcodes("") local default_popup_size = { width = 60, height = "80%" } local draw, create_tree, render_tree @@ -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 diff --git a/lua/neo-tree/utils/init.lua b/lua/neo-tree/utils/init.lua index 5ff54a0e..4efae4d2 100644 --- a/lua/neo-tree/utils/init.lua +++ b/lua/neo-tree/utils/init.lua @@ -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