From 495c0201632e03f3f68b9068179c39619ad28184 Mon Sep 17 00:00:00 2001 From: bxshan Date: Tue, 27 Oct 2026 07:34:25 +0000 Subject: [PATCH] Fix scroll keymaps undoing the scroll in the terminal The / terminal-mode mappings ended with `i`, which re-enters terminal mode right after scrolling. Entering terminal mode moves the view back to the terminal cursor, so the scroll was undone immediately and the keys appeared to do nothing. Drop the trailing `i` so the mappings leave the window in normal mode, scrolled, as the README and help already describe ("press the `i` key to re-enter insert mode"). Add a spec covering the mapped rhs. Fixes #58 --- lua/claude-code/keymaps.lua | 6 ++++-- tests/spec/keymaps_spec.lua | 40 +++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/lua/claude-code/keymaps.lua b/lua/claude-code/keymaps.lua index 5441bd1f..ed74def4 100644 --- a/lua/claude-code/keymaps.lua +++ b/lua/claude-code/keymaps.lua @@ -172,19 +172,21 @@ function M.setup_terminal_navigation(claude_code, config) end -- Add scrolling keymaps + -- These leave terminal mode on purpose: re-entering it (e.g. with a trailing `i`) + -- jumps the view back to the terminal cursor and undoes the scroll. Press `i` to resume. if config.keymaps.scrolling then vim.api.nvim_buf_set_keymap( buf, 't', '', - [[i]], + [[]], { noremap = true, silent = true, desc = 'Scroll full page down' } ) vim.api.nvim_buf_set_keymap( buf, 't', '', - [[i]], + [[]], { noremap = true, silent = true, desc = 'Scroll full page up' } ) end diff --git a/tests/spec/keymaps_spec.lua b/tests/spec/keymaps_spec.lua index 13772cf8..95d42bbf 100644 --- a/tests/spec/keymaps_spec.lua +++ b/tests/spec/keymaps_spec.lua @@ -190,5 +190,45 @@ describe('keymaps', function() assert.is_false(window_navigation_found, "Window navigation keybindings should not be registered when disabled") end) + + it('should leave terminal mode after scrolling so the scroll is kept', function() + -- Setup claude_code table with an active instance buffer + claude_code.claude_code = { current_instance = 'test', instances = { test = 42 } } + + config.keymaps.window_navigation = false + config.keymaps.scrolling = true + + -- Reset mapped_keys + mapped_keys = {} + + -- Mock buf_set_keymap + _G.vim.api.nvim_buf_set_keymap = function(bufnr, mode, lhs, rhs, opts) + table.insert(mapped_keys, { + bufnr = bufnr, + mode = mode, + lhs = lhs, + rhs = rhs, + opts = opts + }) + end + + -- Mock buf_is_valid + _G.vim.api.nvim_buf_is_valid = function(bufnr) + return bufnr == 42 + end + + keymaps.setup_terminal_navigation(claude_code, config) + + local scroll_rhs = {} + for _, mapping in ipairs(mapped_keys) do + if mapping.mode == 't' and (mapping.lhs == '' or mapping.lhs == '') then + scroll_rhs[mapping.lhs] = mapping.rhs + end + end + + -- Re-entering terminal mode would snap the view back to the cursor + assert.are.equal([[]], scroll_rhs['']) + assert.are.equal([[]], scroll_rhs['']) + end) end) end) \ No newline at end of file