diff --git a/lua/claude-code/keymaps.lua b/lua/claude-code/keymaps.lua index 5441bd1..ed74def 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 13772cf..95d42bb 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