-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcommands.lua
More file actions
393 lines (342 loc) · 11.7 KB
/
Copy pathcommands.lua
File metadata and controls
393 lines (342 loc) · 11.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
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
local Utils = require("eca.utils")
local Logger = require("eca.logger")
local M = {}
--- Setup ECA commands
function M.setup()
-- Define ECA commands
vim.api.nvim_create_user_command("EcaChat", function(opts)
require("eca.api").chat(opts.args and { message = opts.args } or {})
end, {
desc = "Open ECA chat",
nargs = "?",
})
vim.api.nvim_create_user_command("EcaToggle", function()
require("eca.api").toggle()
end, {
desc = "Toggle ECA sidebar",
})
vim.api.nvim_create_user_command("EcaFocus", function()
require("eca.api").focus()
end, {
desc = "Focus ECA sidebar",
})
vim.api.nvim_create_user_command("EcaClose", function()
require("eca.api").close()
end, {
desc = "Close ECA sidebar",
})
vim.api.nvim_create_user_command("EcaAddFile", function(opts)
if opts.args and opts.args ~= "" then
require("eca.api").add_file_context(opts.args)
else
require("eca.api").add_current_file_context()
end
end, {
desc = "Add file as context to ECA",
nargs = "?",
complete = "file",
})
vim.api.nvim_create_user_command("EcaAddSelection", function()
-- Force exit visual mode and set marks
vim.cmd("normal! \\<Esc>")
vim.defer_fn(function()
require("eca.api").add_selection_context()
end, 50) -- Small delay to ensure marks are set
end, {
desc = "Add current selection as context to ECA",
range = true,
})
vim.api.nvim_create_user_command("EcaListContexts", function()
require("eca.api").list_contexts()
end, {
desc = "List active contexts in ECA",
})
vim.api.nvim_create_user_command("EcaClearContexts", function()
require("eca.api").clear_contexts()
end, {
desc = "Clear all contexts from ECA",
})
vim.api.nvim_create_user_command("EcaRemoveContext", function(opts)
if opts.args and opts.args ~= "" then
require("eca.api").remove_context(opts.args)
else
Logger.notify("Please provide a file path to remove", vim.log.levels.WARN)
end
end, {
desc = "Remove specific context from ECA",
nargs = "+",
complete = "file",
})
vim.api.nvim_create_user_command("EcaAddRepoMap", function()
require("eca.api").add_repo_map_context()
end, {
desc = "Add repository map context to ECA",
})
-- ===== Selected Code Commands =====
vim.api.nvim_create_user_command("EcaShowSelection", function()
require("eca.api").show_selected_code()
end, {
desc = "Show currently selected code in ECA",
})
vim.api.nvim_create_user_command("EcaClearSelection", function()
require("eca.api").clear_selected_code()
end, {
desc = "Clear selected code from ECA",
})
-- ===== TODOs Commands =====
vim.api.nvim_create_user_command("EcaAddTodo", function(opts)
if opts.args and opts.args ~= "" then
require("eca.api").add_todo(opts.args)
else
Logger.notify("Please provide TODO content", vim.log.levels.WARN)
end
end, {
desc = "Add a new TODO to ECA",
nargs = "+",
})
vim.api.nvim_create_user_command("EcaListTodos", function()
require("eca.api").list_todos()
end, {
desc = "List active TODOs in ECA",
})
vim.api.nvim_create_user_command("EcaToggleTodo", function(opts)
if opts.args and opts.args ~= "" then
local index = tonumber(opts.args)
if index then
require("eca.api").toggle_todo(index)
else
Logger.notify("Please provide a valid TODO index", vim.log.levels.WARN)
end
else
Logger.notify("Please provide TODO index to toggle", vim.log.levels.WARN)
end
end, {
desc = "Toggle TODO completion status",
nargs = 1,
})
vim.api.nvim_create_user_command("EcaClearTodos", function()
require("eca.api").clear_todos()
end, {
desc = "Clear all TODOs from ECA",
})
vim.api.nvim_create_user_command("EcaServerStart", function()
require("eca.api").start_server()
end, {
desc = "Start ECA server",
})
vim.api.nvim_create_user_command("EcaServerStop", function()
require("eca.api").stop_server()
end, {
desc = "Stop ECA server",
})
vim.api.nvim_create_user_command("EcaServerRestart", function()
require("eca.api").restart_server()
end, {
desc = "Restart ECA server",
})
vim.api.nvim_create_user_command("EcaServerMessages", function()
local has_snacks, snacks = pcall(require, "snacks")
if not has_snacks then
Logger.notify("snacks.nvim is not available", vim.log.levels.ERROR)
return
end
snacks.picker(
---@type snacks.picker.Config
{
source = "eca messages",
finder = function(opts, ctx)
---@type snacks.picker.finder.Item[]
local items = {}
local eca = require("eca")
if not eca or not eca.server then
Logger.notify("ECA plugin is not available", vim.log.levels.ERROR)
return items
end
for msg in vim.iter(eca.server.messages) do
local decoded = vim.json.decode(msg.content)
table.insert(items, {
text = decoded.method,
idx = decoded.id,
preview = {
text = vim.inspect(decoded),
ft = "lua",
},
})
end
return items
end,
preview = "preview",
format = "text",
confirm = function(self, item, _)
vim.fn.setreg("", item.preview.text)
self:close()
end,
}
)
end, {
desc = "Display Messages Sent to and Received by ECA server",
})
vim.api.nvim_create_user_command("EcaLogs", function(opts)
local Api = require("eca.api")
local subcommand = opts.args and opts.args:match("%S+") or "show"
if subcommand == "show" then
Api.show_logs()
elseif subcommand == "log_path" then
local log_path = Logger.get_log_path()
Logger.notify("ECA log file: " .. log_path, vim.log.levels.INFO)
elseif subcommand == "clear" then
if Logger.clear_log() then
Logger.notify("ECA log file cleared", vim.log.levels.INFO)
else
Logger.notify("Failed to clear log file", vim.log.levels.WARN)
end
elseif subcommand == "stats" then
local stats = Logger.get_log_stats()
if stats then
Logger.notify(string.format("Log file: %s", stats.path), vim.log.levels.INFO)
Logger.notify(string.format("Size: %.2fMB (%d bytes)", stats.size_mb, stats.size), vim.log.levels.INFO)
Logger.notify(string.format("Last modified: %s", stats.modified), vim.log.levels.INFO)
else
Logger.notify("No log file stats available", vim.log.levels.INFO)
end
else
Logger.notify("Unknown EcaLogs subcommand: " .. subcommand, vim.log.levels.WARN)
Logger.notify("Available subcommands: show, log_path, clear, stats", vim.log.levels.INFO)
end
end, {
desc = "ECA logging commands (show, log_path, clear, stats)",
nargs = "?",
complete = function(ArgLead, CmdLine, CursorPos)
local subcommands = { "show", "log_path", "clear", "stats" }
return vim.tbl_filter(function(cmd)
return cmd:match("^" .. ArgLead)
end, subcommands)
end,
})
vim.api.nvim_create_user_command("EcaSend", function(opts)
if opts.args and opts.args ~= "" then
require("eca.api").send_message(opts.args)
else
Logger.notify("Please provide a message to send", vim.log.levels.WARN)
end
end, {
desc = "Send a message to ECA",
nargs = "+",
})
vim.api.nvim_create_user_command("EcaDebugWidth", function()
local Config = require("eca.config")
local width = Config.get_window_width()
local columns = vim.o.columns
local percentage = Config.options.windows.width
Logger.notify(
string.format("Width: %d columns (%.1f%% of %d total columns)", width, percentage, columns),
vim.log.levels.INFO
)
end, {
desc = "Debug window width calculation",
})
vim.api.nvim_create_user_command("EcaRedownload", function()
local eca = require("eca")
local Utils = require("eca.utils")
if eca.server and eca.server:is_running() then
Logger.notify("Stopping server before re-download...", vim.log.levels.INFO)
eca.server:stop()
end
-- Remove existing version file to force re-download
local cache_dir = Utils.get_cache_dir()
local version_file = cache_dir .. "/eca-version"
os.remove(version_file)
-- Remove existing binary
local server_binary = cache_dir .. "/eca"
os.remove(server_binary)
Logger.notify("Removed cached ECA server. Will re-download on next start.", vim.log.levels.INFO)
-- Restart server
vim.defer_fn(function()
if eca.server then
eca.server:start()
end
end, 1000)
end, {
desc = "Force re-download of ECA server",
})
vim.api.nvim_create_user_command("EcaStopResponse", function()
local eca = require("eca")
local Utils = require("eca.utils")
-- Force stop any ongoing streaming response
if eca.sidebar then
eca.sidebar:_finalize_streaming_response()
Logger.notify("Forced stop of streaming response", vim.log.levels.INFO)
else
Logger.notify("No active sidebar to stop", vim.log.levels.WARN)
end
end, {
desc = "Emergency stop for infinite loops or runaway responses",
})
vim.api.nvim_create_user_command("EcaFixTreesitter", function()
local Utils = require("eca.utils")
-- Emergency treesitter fix for chat buffer
vim.schedule(function()
local eca = require("eca")
if eca.sidebar and eca.sidebar.containers and eca.sidebar.containers.chat then
local bufnr = eca.sidebar.containers.chat.bufnr
if bufnr and vim.api.nvim_buf_is_valid(bufnr) then
-- Disable all highlighting for this buffer
pcall(vim.api.nvim_set_option_value, "syntax", "off", { buf = bufnr })
-- Destroy treesitter highlighter if it exists
pcall(function()
if vim.treesitter.highlighter.active[bufnr] then
vim.treesitter.highlighter.active[bufnr]:destroy()
vim.treesitter.highlighter.active[bufnr] = nil
end
end)
Logger.notify("Disabled treesitter highlighting for ECA chat buffer", vim.log.levels.INFO)
Logger.notify("Buffer " .. bufnr .. " highlighting disabled", vim.log.levels.INFO)
else
Logger.notify("No valid chat buffer found", vim.log.levels.WARN)
end
else
Logger.notify("No active ECA sidebar found", vim.log.levels.WARN)
end
end)
end, {
desc = "Emergency fix for treesitter issues in ECA chat",
})
vim.api.nvim_create_user_command("EcaChatSelectModel", function()
local eca = require("eca")
if not eca or not eca.current or not eca.current.sidebar then
Logger.notify("No active ECA sidebar found", vim.log.levels.WARN)
return
end
local chat = eca.current.sidebar
local models = chat.mediator:models()
vim.ui.select(models, {
prompt = "Select ECA Chat Model:",
}, function(choice)
if choice then
chat.mediator:update_selected_model(choice)
end
end)
end, {
desc = "Select current ECA Chat model",
})
vim.api.nvim_create_user_command("EcaChatSelectBehavior", function()
local eca = require("eca")
if not eca or not eca.current or not eca.current.sidebar then
Logger.notify("No active ECA sidebar found", vim.log.levels.WARN)
return
end
local chat = eca.current.sidebar
local behaviors = chat.mediator:behaviors()
vim.ui.select(behaviors, {
prompt = "Select ECA Chat Behavior:",
}, function(choice)
if choice then
chat.mediator:update_selected_behavior(choice)
end
end)
end, {
desc = "Select current ECA Chat behavior",
})
Logger.debug("ECA commands registered")
end
return M