From 64217f33c5ec32daff0733c79e5d146bbb1752ca Mon Sep 17 00:00:00 2001 From: Myles Mo Date: Wed, 8 Jan 2025 18:30:11 +0800 Subject: [PATCH 1/3] fix(preview): scroll_preview fallback default when preview is't active --- lua/neo-tree/sources/common/commands.lua | 4 ++-- lua/neo-tree/sources/common/preview.lua | 14 +++++++++----- lua/neo-tree/ui/renderer.lua | 2 +- lua/neo-tree/utils/init.lua | 5 +++++ 4 files changed, 17 insertions(+), 8 deletions(-) diff --git a/lua/neo-tree/sources/common/commands.lua b/lua/neo-tree/sources/common/commands.lua index 4caf1e68..5fd75ae3 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, cmd) + Preview.scroll(state, cmd) 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..617df377 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) +Preview.scroll = function(state, cmd) local direction = state.config.direction - -- NOTE: Chars below are raw escape codes for / - local input = direction < 0 and [[]] or [[]] + local input = direction < 0 and utils.keycode("") or utils.keycode("") local count = math.abs(direction) + local keycode = utils.keycode(cmd) + 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(keycode)) end) end - end return Preview diff --git a/lua/neo-tree/ui/renderer.lua b/lua/neo-tree/ui/renderer.lua index a67dd70f..cd05c40c 100644 --- a/lua/neo-tree/ui/renderer.lua +++ b/lua/neo-tree/ui/renderer.lua @@ -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..0d6ffa35 100644 --- a/lua/neo-tree/utils/init.lua +++ b/lua/neo-tree/utils/init.lua @@ -1293,4 +1293,9 @@ M.index_by_path = function(tbl, key) return value end +---@param str string +function M.keycode(str) + return vim.api.nvim_replace_termcodes(str, true, true, true) +end + return M From a4da2f8839189b38b7a8f798c9c145b7105e1e15 Mon Sep 17 00:00:00 2001 From: pynappo Date: Thu, 23 Jan 2025 01:11:32 -0800 Subject: [PATCH 2/3] slight refactor --- lua/neo-tree/sources/common/preview.lua | 10 +++++----- lua/neo-tree/ui/renderer.lua | 2 +- lua/neo-tree/utils/init.lua | 6 +++++- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/lua/neo-tree/sources/common/preview.lua b/lua/neo-tree/sources/common/preview.lua index 617df377..c9cd2e9a 100644 --- a/lua/neo-tree/sources/common/preview.lua +++ b/lua/neo-tree/sources/common/preview.lua @@ -435,20 +435,20 @@ Preview.focus = function() end end -Preview.scroll = function(state, cmd) +local CTRL_E = utils.replace_termcodes("") +local CTRL_Y = utils.replace_termcodes("") +Preview.scroll = function(state, fallback) local direction = state.config.direction - local input = direction < 0 and utils.keycode("") or utils.keycode("") + local input = direction < 0 and CTRL_E or CTRL_Y local count = math.abs(direction) - local keycode = utils.keycode(cmd) - if Preview:is_active() then vim.api.nvim_win_call(instance.winid, function() vim.cmd(("normal! %s%s"):format(count, input)) end) else vim.api.nvim_buf_call(state.bufnr, function() - vim.cmd(("normal! %s"):format(keycode)) + vim.cmd(("normal! %s"):format(utils.replace_termcodes(fallback))) end) end end diff --git a/lua/neo-tree/ui/renderer.lua b/lua/neo-tree/ui/renderer.lua index cd05c40c..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 diff --git a/lua/neo-tree/utils/init.lua b/lua/neo-tree/utils/init.lua index 0d6ffa35..4efae4d2 100644 --- a/lua/neo-tree/utils/init.lua +++ b/lua/neo-tree/utils/init.lua @@ -1293,8 +1293,12 @@ 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 -function M.keycode(str) +---@return string representation Internal representation of the keycodes +function M.replace_termcodes(str) return vim.api.nvim_replace_termcodes(str, true, true, true) end From ba8ad9d507b15f4dabe58c3a07ce6905fe6b5c79 Mon Sep 17 00:00:00 2001 From: pynappo Date: Thu, 23 Jan 2025 01:23:54 -0800 Subject: [PATCH 3/3] rename --- lua/neo-tree/sources/common/commands.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lua/neo-tree/sources/common/commands.lua b/lua/neo-tree/sources/common/commands.lua index 5fd75ae3..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, cmd) - Preview.scroll(state, cmd) +M.scroll_preview = function(state, fallback) + Preview.scroll(state, fallback) end M.focus_preview = function()