-
Notifications
You must be signed in to change notification settings - Fork 189
Expand file tree
/
Copy pathsnacks.lua
More file actions
294 lines (265 loc) · 9.76 KB
/
snacks.lua
File metadata and controls
294 lines (265 loc) · 9.76 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
---Snacks.nvim terminal provider for Claude Code.
---@module 'claudecode.terminal.snacks'
local M = {}
local snacks_available, Snacks = pcall(require, "snacks")
local utils = require("claudecode.utils")
local terminal = nil
--- @return boolean
local function is_available()
return snacks_available and Snacks and Snacks.terminal ~= nil
end
---Setup event handlers for terminal instance
---@param term_instance table The Snacks terminal instance
---@param config table Configuration options
local function setup_terminal_events(term_instance, config)
local logger = require("claudecode.logger")
-- Handle command completion/exit - only if auto_close is enabled
if config.auto_close then
term_instance:on("TermClose", function()
if vim.v.event.status ~= 0 then
logger.error("terminal", "Claude exited with code " .. vim.v.event.status .. ".\nCheck for any errors.")
end
-- Clean up
terminal = nil
vim.schedule(function()
term_instance:close({ buf = true })
vim.cmd.checktime()
end)
end, { buf = true })
end
-- Handle buffer deletion
term_instance:on("BufWipeout", function()
logger.debug("terminal", "Terminal buffer wiped")
terminal = nil
end, { buf = true })
end
---Builds Snacks terminal options with focus control
---@param config ClaudeCodeTerminalConfig Terminal configuration
---@param env_table table Environment variables to set for the terminal process
---@param focus boolean|nil Whether to focus the terminal when opened (defaults to true)
---@return snacks.terminal.Opts opts Snacks terminal options with start_insert/auto_insert controlled by focus parameter
local function build_opts(config, env_table, focus)
focus = utils.normalize_focus(focus)
return {
env = env_table,
cwd = config.cwd,
start_insert = focus,
auto_insert = focus,
auto_close = false,
win = vim.tbl_deep_extend("force", {
position = config.split_side,
width = config.split_width_percentage,
height = 0,
relative = "editor",
keys = {
claude_new_line = {
"<S-CR>",
function()
vim.api.nvim_feedkeys("\\", "t", true)
vim.defer_fn(function()
vim.api.nvim_feedkeys("\r", "t", true)
end, 10)
end,
mode = "t",
desc = "New line",
},
},
} --[[@as snacks.win.Config]], config.snacks_win_opts or {}),
} --[[@as snacks.terminal.Opts]]
end
function M.setup()
-- No specific setup needed for Snacks provider
end
---Open a terminal using Snacks.nvim
---@param cmd_string string
---@param env_table table
---@param config ClaudeCodeTerminalConfig
---@param focus boolean?
function M.open(cmd_string, env_table, config, focus)
if not is_available() then
vim.notify("Snacks.nvim terminal provider selected but Snacks.terminal not available.", vim.log.levels.ERROR)
return
end
focus = utils.normalize_focus(focus)
if terminal and terminal:buf_valid() then
-- Check if terminal exists but is hidden (no window)
if not terminal.win or not vim.api.nvim_win_is_valid(terminal.win) then
-- Terminal is hidden, show it using snacks toggle
terminal:toggle()
if focus then
terminal:focus()
local term_buf_id = terminal.buf
if term_buf_id and vim.api.nvim_buf_get_option(term_buf_id, "buftype") == "terminal" then
if terminal.win and vim.api.nvim_win_is_valid(terminal.win) then
vim.api.nvim_win_call(terminal.win, function()
vim.cmd("startinsert")
end)
end
end
end
else
-- Terminal is already visible
if focus then
terminal:focus()
local term_buf_id = terminal.buf
if term_buf_id and vim.api.nvim_buf_get_option(term_buf_id, "buftype") == "terminal" then
-- Check if window is valid before calling nvim_win_call
if terminal.win and vim.api.nvim_win_is_valid(terminal.win) then
vim.api.nvim_win_call(terminal.win, function()
vim.cmd("startinsert")
end)
end
end
end
end
return
end
local opts = build_opts(config, env_table, focus)
local term_instance = Snacks.terminal.open(cmd_string, opts)
if term_instance and term_instance:buf_valid() then
setup_terminal_events(term_instance, config)
terminal = term_instance
else
terminal = nil
local logger = require("claudecode.logger")
local error_details = {}
if not term_instance then
table.insert(error_details, "Snacks.terminal.open() returned nil")
elseif not term_instance:buf_valid() then
table.insert(error_details, "terminal instance is invalid")
if term_instance.buf and not vim.api.nvim_buf_is_valid(term_instance.buf) then
table.insert(error_details, "buffer is invalid")
end
if term_instance.win and not vim.api.nvim_win_is_valid(term_instance.win) then
table.insert(error_details, "window is invalid")
end
end
local context = string.format("cmd='%s', opts=%s", cmd_string, vim.inspect(opts))
local error_msg = string.format(
"Failed to open Claude terminal using Snacks. Details: %s. Context: %s",
table.concat(error_details, ", "),
context
)
vim.notify(error_msg, vim.log.levels.ERROR)
logger.debug("terminal", error_msg)
end
end
---Close the terminal
function M.close()
if not is_available() then
return
end
if terminal and terminal:buf_valid() then
terminal:close()
end
end
---Simple toggle: always show/hide terminal regardless of focus
---@param cmd_string string
---@param env_table table
---@param config table
---@param force_new boolean? If true, close any existing terminal and open a new one with cmd_string
function M.simple_toggle(cmd_string, env_table, config, force_new)
if not is_available() then
vim.notify("Snacks.nvim terminal provider selected but Snacks.terminal not available.", vim.log.levels.ERROR)
return
end
local logger = require("claudecode.logger")
-- If args like --resume or --continue were passed, force a new session
if force_new and terminal and terminal:buf_valid() then
logger.debug("terminal", "Simple toggle: force_new=true, closing existing terminal to start new session")
M.close()
M.open(cmd_string, env_table, config)
return
end
-- Check if terminal exists and is visible
if terminal and terminal:buf_valid() and terminal:win_valid() then
-- Terminal is visible, hide it
logger.debug("terminal", "Simple toggle: hiding visible terminal")
terminal:toggle()
elseif terminal and terminal:buf_valid() and not terminal:win_valid() then
-- Terminal exists but not visible, show it
logger.debug("terminal", "Simple toggle: showing hidden terminal")
terminal:toggle()
else
-- No terminal exists, create new one
logger.debug("terminal", "Simple toggle: creating new terminal")
M.open(cmd_string, env_table, config)
end
end
---Smart focus toggle: switches to terminal if not focused, hides if currently focused
---@param cmd_string string
---@param env_table table
---@param config table
---@param force_new boolean? If true, close any existing terminal and open a new one with cmd_string
function M.focus_toggle(cmd_string, env_table, config, force_new)
if not is_available() then
vim.notify("Snacks.nvim terminal provider selected but Snacks.terminal not available.", vim.log.levels.ERROR)
return
end
local logger = require("claudecode.logger")
-- If args like --resume or --continue were passed, force a new session
if force_new and terminal and terminal:buf_valid() then
logger.debug("terminal", "Focus toggle: force_new=true, closing existing terminal to start new session")
M.close()
M.open(cmd_string, env_table, config)
return
end
-- Terminal exists, is valid, but not visible
if terminal and terminal:buf_valid() and not terminal:win_valid() then
logger.debug("terminal", "Focus toggle: showing hidden terminal")
terminal:toggle()
-- Terminal exists, is valid, and is visible
elseif terminal and terminal:buf_valid() and terminal:win_valid() then
local claude_term_neovim_win_id = terminal.win
local current_neovim_win_id = vim.api.nvim_get_current_win()
-- you're IN it
if claude_term_neovim_win_id == current_neovim_win_id then
logger.debug("terminal", "Focus toggle: hiding terminal (currently focused)")
terminal:toggle()
-- you're NOT in it
else
logger.debug("terminal", "Focus toggle: focusing terminal")
vim.api.nvim_set_current_win(claude_term_neovim_win_id)
if terminal.buf and vim.api.nvim_buf_is_valid(terminal.buf) then
if vim.api.nvim_buf_get_option(terminal.buf, "buftype") == "terminal" then
vim.api.nvim_win_call(claude_term_neovim_win_id, function()
vim.cmd("startinsert")
end)
end
end
end
-- No terminal exists
else
logger.debug("terminal", "Focus toggle: creating new terminal")
M.open(cmd_string, env_table, config)
end
end
---Legacy toggle function for backward compatibility (defaults to simple_toggle)
---@param cmd_string string
---@param env_table table
---@param config table
function M.toggle(cmd_string, env_table, config)
M.simple_toggle(cmd_string, env_table, config)
end
---Get the active terminal buffer number
---@return number?
function M.get_active_bufnr()
if terminal and terminal:buf_valid() and terminal.buf then
if vim.api.nvim_buf_is_valid(terminal.buf) then
return terminal.buf
end
end
return nil
end
---Is the terminal provider available?
---@return boolean
function M.is_available()
return is_available()
end
---For testing purposes
---@return table? terminal The terminal instance, or nil
function M._get_terminal_for_test()
return terminal
end
---@type ClaudeCodeTerminalProvider
return M