-
-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathkeymaps.lua
More file actions
194 lines (180 loc) · 6.7 KB
/
keymaps.lua
File metadata and controls
194 lines (180 loc) · 6.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
---@mod claude-code.keymaps Keymap management for claude-code.nvim
---@brief [[
--- This module provides keymap registration and handling for claude-code.nvim.
--- It handles normal mode, terminal mode, and window navigation keymaps.
---@brief ]]
local M = {}
--- Register keymaps for claude-code.nvim
--- @param claude_code table The main plugin module
--- @param config table The plugin configuration
function M.register_keymaps(claude_code, config)
local map_opts = { noremap = true, silent = true }
-- Normal mode toggle keymaps
if config.keymaps.toggle.normal then
vim.api.nvim_set_keymap(
'n',
config.keymaps.toggle.normal,
[[<cmd>ClaudeCode<CR>]],
vim.tbl_extend('force', map_opts, { desc = 'Claude Code: Toggle' })
)
end
if config.keymaps.toggle.terminal then
-- Terminal mode toggle keymap
-- In terminal mode, special keys like Ctrl need different handling
-- We use a direct escape sequence approach for more reliable terminal mappings
vim.api.nvim_set_keymap(
't',
config.keymaps.toggle.terminal,
[[<C-\><C-n>:ClaudeCode<CR>]],
vim.tbl_extend('force', map_opts, { desc = 'Claude Code: Toggle' })
)
end
-- Register variant keymaps if configured
if config.keymaps.toggle.variants then
for variant_name, keymap in pairs(config.keymaps.toggle.variants) do
if keymap then
-- Convert variant name to PascalCase for command name (e.g., "continue" -> "Continue")
local capitalized_name = variant_name:gsub('^%l', string.upper)
local cmd_name = 'ClaudeCode' .. capitalized_name
vim.api.nvim_set_keymap(
'n',
keymap,
string.format([[<cmd>%s<CR>]], cmd_name),
vim.tbl_extend('force', map_opts, { desc = 'Claude Code: ' .. capitalized_name })
)
end
end
end
-- Register with which-key if it's available
vim.defer_fn(function()
local status_ok, which_key = pcall(require, 'which-key')
if status_ok then
if config.keymaps.toggle.normal then
which_key.add {
mode = 'n',
{ config.keymaps.toggle.normal, desc = 'Claude Code: Toggle', icon = '🤖' },
}
end
if config.keymaps.toggle.terminal then
which_key.add {
mode = 't',
{ config.keymaps.toggle.terminal, desc = 'Claude Code: Toggle', icon = '🤖' },
}
end
-- Register variant keymaps with which-key
if config.keymaps.toggle.variants then
for variant_name, keymap in pairs(config.keymaps.toggle.variants) do
if keymap then
local capitalized_name = variant_name:gsub('^%l', string.upper)
which_key.add {
mode = 'n',
{ keymap, desc = 'Claude Code: ' .. capitalized_name, icon = '🤖' },
}
end
end
end
end
end, 100)
end
--- Set up terminal-specific keymaps for window navigation
--- @param claude_code table The main plugin module
--- @param config table The plugin configuration
function M.setup_terminal_navigation(claude_code, config)
-- Get current active Claude instance buffer
local current_instance = claude_code.claude_code.current_instance
local buf = current_instance and claude_code.claude_code.instances[current_instance]
if buf and vim.api.nvim_buf_is_valid(buf) then
-- Create autocommand to enter insert mode when the terminal window gets focus
local augroup = vim.api.nvim_create_augroup('ClaudeCodeTerminalFocus_' .. buf, { clear = true })
-- Set up multiple events for more reliable focus detection
vim.api.nvim_create_autocmd(
{ 'WinEnter', 'BufEnter', 'WinLeave', 'FocusGained', 'CmdLineLeave' },
{
group = augroup,
callback = function()
vim.schedule(claude_code.force_insert_mode)
end,
desc = 'Auto-enter insert mode when focusing Claude Code terminal',
}
)
-- Window navigation keymaps
if config.keymaps.window_navigation.enabled then
-- Window navigation keymaps with special handling to force insert mode in the target window
vim.api.nvim_buf_set_keymap(
buf,
't',
config.keymaps.window_navigation.left,
[[<C-\><C-n><C-w>h:lua require("claude-code").force_insert_mode()<CR>]],
{ noremap = true, silent = true, desc = 'Window: move left' }
)
vim.api.nvim_buf_set_keymap(
buf,
't',
config.keymaps.window_navigation.down,
[[<C-\><C-n><C-w>j:lua require("claude-code").force_insert_mode()<CR>]],
{ noremap = true, silent = true, desc = 'Window: move down' }
)
vim.api.nvim_buf_set_keymap(
buf,
't',
config.keymaps.window_navigation.up,
[[<C-\><C-n><C-w>k:lua require("claude-code").force_insert_mode()<CR>]],
{ noremap = true, silent = true, desc = 'Window: move up' }
)
vim.api.nvim_buf_set_keymap(
buf,
't',
config.keymaps.window_navigation.right,
[[<C-\><C-n><C-w>l:lua require("claude-code").force_insert_mode()<CR>]],
{ noremap = true, silent = true, desc = 'Window: move right' }
)
-- Also add normal mode mappings for when user is in normal mode in the terminal
vim.api.nvim_buf_set_keymap(
buf,
'n',
config.keymaps.window_navigation.left,
[[<C-w>h:lua require("claude-code").force_insert_mode()<CR>]],
{ noremap = true, silent = true, desc = 'Window: move left' }
)
vim.api.nvim_buf_set_keymap(
buf,
'n',
config.keymaps.window_navigation.down,
[[<C-w>j:lua require("claude-code").force_insert_mode()<CR>]],
{ noremap = true, silent = true, desc = 'Window: move down' }
)
vim.api.nvim_buf_set_keymap(
buf,
'n',
config.keymaps.window_navigation.up,
[[<C-w>k:lua require("claude-code").force_insert_mode()<CR>]],
{ noremap = true, silent = true, desc = 'Window: move up' }
)
vim.api.nvim_buf_set_keymap(
buf,
'n',
config.keymaps.window_navigation.right,
[[<C-w>l:lua require("claude-code").force_insert_mode()<CR>]],
{ noremap = true, silent = true, desc = 'Window: move right' }
)
end
-- Add scrolling keymaps
if config.keymaps.scrolling.enabled then
vim.api.nvim_buf_set_keymap(
buf,
't',
config.keymaps.scrolling.page_down,
[[<C-\><C-n><C-f>i]],
{ noremap = true, silent = true, desc = 'Scroll full page down' }
)
vim.api.nvim_buf_set_keymap(
buf,
't',
config.keymaps.scrolling.page_up,
[[<C-\><C-n><C-b>i]],
{ noremap = true, silent = true, desc = 'Scroll full page up' }
)
end
end
end
return M