Skip to content

Commit 4435552

Browse files
committed
refact context commands
1 parent 97e0d75 commit 4435552

2 files changed

Lines changed: 93 additions & 208 deletions

File tree

lua/eca/api.lua

Lines changed: 30 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,36 @@ function M.add_file_context(file_path)
9797
Logger.info("File context added: " .. vim.inspect(context))
9898
end
9999

100+
function M.remove_file_context(path)
101+
local eca = require("eca")
102+
local chat = eca.get()
103+
104+
if not chat or not chat.mediator then
105+
Logger.notify("No active ECA Chat", vim.log.levels.WARN)
106+
return
107+
end
108+
109+
-- Create context object
110+
local context = {
111+
type = "file",
112+
data = {
113+
path = path,
114+
}
115+
}
116+
117+
Logger.info("Removing context: " .. vim.inspect(context))
118+
chat.mediator:remove_context(context)
119+
end
120+
121+
function M.remove_current_file_context()
122+
local current_file = vim.api.nvim_buf_get_name(0)
123+
if current_file and current_file ~= "" then
124+
M.remove_file_context(current_file)
125+
else
126+
Logger.notify("No current file to remove as context", vim.log.levels.WARN)
127+
end
128+
end
129+
100130
---@param directory_path string
101131
function M.add_directory_context(directory_path)
102132
Logger.info("Adding directory context: " .. directory_path)
@@ -218,50 +248,6 @@ function M.clear_contexts()
218248
Logger.info("Cleared all contexts")
219249
end
220250

221-
function M.remove_context(path)
222-
local eca = require("eca")
223-
local chat = eca.get()
224-
225-
if not chat or not chat.mediator then
226-
Logger.notify("No active ECA Chat", vim.log.levels.WARN)
227-
return
228-
end
229-
230-
chat.mediator:remove_context(path)
231-
Logger.info("Context removed: " .. path)
232-
end
233-
234-
function M.add_repo_map_context()
235-
local eca = require("eca")
236-
local sidebar = eca.get()
237-
if not sidebar then
238-
Logger.info("Opening ECA sidebar to add repoMap context...")
239-
M.chat()
240-
sidebar = eca.get()
241-
end
242-
243-
if sidebar then
244-
-- Check if repoMap already exists
245-
local contexts = sidebar:get_contexts()
246-
for _, context in ipairs(contexts) do
247-
if context.type == "repoMap" then
248-
Logger.notify("RepoMap context already added", vim.log.levels.INFO)
249-
return
250-
end
251-
end
252-
253-
-- Add repoMap context
254-
sidebar:add_context({
255-
type = "repoMap",
256-
path = "repoMap",
257-
content = "Repository structure and code mapping for better project understanding",
258-
})
259-
Logger.info("Added repoMap context")
260-
else
261-
Logger.notify("Failed to create ECA sidebar", vim.log.levels.ERROR)
262-
end
263-
end
264-
265251
---@return boolean
266252
function M.is_server_running()
267253
local eca = require("eca")
@@ -300,105 +286,6 @@ function M.server_status()
300286
end
301287
end
302288

303-
-- ===== Selected Code Management =====
304-
305-
function M.show_selected_code()
306-
local eca = require("eca")
307-
local sidebar = eca.get()
308-
if sidebar then
309-
local selected_code = sidebar._selected_code
310-
if selected_code then
311-
Logger.notify(
312-
"Selected code: "
313-
.. selected_code.filepath
314-
.. " (lines "
315-
.. (selected_code.start_line or "?")
316-
.. "-"
317-
.. (selected_code.end_line or "?")
318-
.. ")",
319-
vim.log.levels.INFO
320-
)
321-
else
322-
Logger.notify("No code currently selected", vim.log.levels.INFO)
323-
end
324-
else
325-
Logger.notify("ECA sidebar not available", vim.log.levels.WARN)
326-
end
327-
end
328-
329-
function M.clear_selected_code()
330-
local eca = require("eca")
331-
local sidebar = eca.get()
332-
if sidebar then
333-
sidebar:clear_selected_code()
334-
else
335-
Logger.notify("ECA sidebar not available", vim.log.levels.WARN)
336-
end
337-
end
338-
339-
-- ===== TODOs Management =====
340-
341-
function M.add_todo(content)
342-
local eca = require("eca")
343-
local sidebar = eca.get()
344-
if not sidebar then
345-
Logger.info("Opening ECA sidebar to add TODO...")
346-
M.chat()
347-
sidebar = eca.get()
348-
end
349-
350-
if sidebar then
351-
local todo = {
352-
content = content,
353-
status = "pending",
354-
}
355-
sidebar:add_todo(todo)
356-
else
357-
Logger.notify("Failed to create ECA sidebar", vim.log.levels.ERROR)
358-
end
359-
end
360-
361-
function M.list_todos()
362-
local eca = require("eca")
363-
local sidebar = eca.get()
364-
if sidebar then
365-
local todos = sidebar:get_todos()
366-
if #todos == 0 then
367-
Logger.notify("No active TODOs", vim.log.levels.INFO)
368-
return
369-
end
370-
371-
Logger.notify("Active TODOs:", vim.log.levels.INFO)
372-
for i, todo in ipairs(todos) do
373-
local status_icon = todo.status == "completed" and "" or ""
374-
Logger.notify(string.format("%d. %s %s", i, status_icon, todo.content), vim.log.levels.INFO)
375-
end
376-
else
377-
Logger.notify("ECA sidebar not available", vim.log.levels.WARN)
378-
end
379-
end
380-
381-
function M.toggle_todo(index)
382-
local eca = require("eca")
383-
local sidebar = eca.get()
384-
if sidebar then
385-
return sidebar:toggle_todo(index)
386-
else
387-
Logger.notify("ECA sidebar not available", vim.log.levels.WARN)
388-
return false
389-
end
390-
end
391-
392-
function M.clear_todos()
393-
local eca = require("eca")
394-
local sidebar = eca.get()
395-
if sidebar then
396-
sidebar:clear_todos()
397-
else
398-
Logger.notify("ECA sidebar not available", vim.log.levels.WARN)
399-
end
400-
end
401-
402289
-- Keep reference to logs popup globally to reuse it
403290
local logs_popup = nil
404291

lua/eca/commands.lua

Lines changed: 63 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@ function M.setup()
3232
})
3333

3434
vim.api.nvim_create_user_command("EcaAddFile", function(opts)
35-
if opts.args and opts.args ~= "" then
36-
require("eca.api").add_file_context(opts.args)
35+
Logger.notify("EcaAddFile is deprecated. Use EcaChatAddFile instead.", vim.log.levels.WARN)
36+
37+
if opts.args and opts.args ~= "" and type(opts.args) == "string" then
38+
require("eca.api").add_file_context(vim.fn.fnamemodify(opts.args, ":p"))
3739
else
3840
require("eca.api").add_current_file_context()
3941
end
@@ -43,102 +45,98 @@ function M.setup()
4345
complete = "file",
4446
})
4547

46-
vim.api.nvim_create_user_command("EcaAddSelection", function()
47-
-- Force exit visual mode and set marks
48-
vim.cmd("normal! \\<Esc>")
49-
vim.defer_fn(function()
50-
require("eca.api").add_selection_context()
51-
end, 50) -- Small delay to ensure marks are set
52-
end, {
53-
desc = "Add current selection as context to ECA",
54-
range = true,
55-
})
56-
57-
vim.api.nvim_create_user_command("EcaListContexts", function()
58-
require("eca.api").list_contexts()
59-
end, {
60-
desc = "List active contexts in ECA",
61-
})
62-
63-
vim.api.nvim_create_user_command("EcaClearContexts", function()
64-
require("eca.api").clear_contexts()
48+
vim.api.nvim_create_user_command("EcaChatAddFile", function(opts)
49+
if opts.args and opts.args ~= "" and type(opts.args) == "string" then
50+
require("eca.api").add_file_context(vim.fn.fnamemodify(opts.args, ":p"))
51+
else
52+
require("eca.api").add_current_file_context()
53+
end
6554
end, {
66-
desc = "Clear all contexts from ECA",
55+
desc = "Add file as context to ECA",
56+
nargs = "?",
57+
complete = "file",
6758
})
6859

6960
vim.api.nvim_create_user_command("EcaRemoveContext", function(opts)
70-
if opts.args and opts.args ~= "" then
71-
require("eca.api").remove_context(opts.args)
61+
Logger.notify("EcaRemoveContext is deprecated. Use EcaChatRemoveFile instead.", vim.log.levels.WARN)
62+
63+
if opts.args and opts.args ~= "" and type(opts.args) == "string" then
64+
require("eca.api").remove_file_context(vim.fn.fnamemodify(opts.args, ":p"))
7265
else
73-
Logger.notify("Please provide a file path to remove", vim.log.levels.WARN)
66+
require("eca.api").remove_current_file_context()
7467
end
7568
end, {
76-
desc = "Remove specific context from ECA",
77-
nargs = "+",
69+
desc = "Remove specific file context from ECA",
70+
nargs = "?",
7871
complete = "file",
7972
})
8073

81-
vim.api.nvim_create_user_command("EcaAddRepoMap", function()
82-
require("eca.api").add_repo_map_context()
74+
vim.api.nvim_create_user_command("EcaChatRemoveFile", function(opts)
75+
if opts.args and opts.args ~= "" and type(opts.args) == "string" then
76+
require("eca.api").remove_file_context(vim.fn.fnamemodify(opts.args, ":p"))
77+
else
78+
require("eca.api").remove_current_file_context()
79+
end
8380
end, {
84-
desc = "Add repository map context to ECA",
81+
desc = "Remove specific file context from ECA",
82+
nargs = "?",
83+
complete = "file",
8584
})
8685

87-
-- ===== Selected Code Commands =====
86+
vim.api.nvim_create_user_command("EcaAddSelection", function()
87+
Logger.notify("EcaAddSelection is deprecated. Use EcaChatAddSelection instead.", vim.log.levels.WARN)
8888

89-
vim.api.nvim_create_user_command("EcaShowSelection", function()
90-
require("eca.api").show_selected_code()
89+
-- Force exit visual mode and set marks
90+
vim.cmd("normal! \\<Esc>")
91+
vim.defer_fn(function()
92+
require("eca.api").add_selection_context()
93+
end, 50) -- Small delay to ensure marks are set
9194
end, {
92-
desc = "Show currently selected code in ECA",
95+
desc = "Add current selection as context to ECA",
96+
range = true,
9397
})
9498

95-
vim.api.nvim_create_user_command("EcaClearSelection", function()
96-
require("eca.api").clear_selected_code()
99+
vim.api.nvim_create_user_command("EcaChatAddSelection", function()
100+
-- Force exit visual mode and set marks
101+
vim.cmd("normal! \\<Esc>")
102+
vim.defer_fn(function()
103+
require("eca.api").add_selection_context()
104+
end, 50) -- Small delay to ensure marks are set
97105
end, {
98-
desc = "Clear selected code from ECA",
106+
desc = "Add current selection as context to ECA",
107+
range = true,
99108
})
100109

101-
-- ===== TODOs Commands =====
110+
vim.api.nvim_create_user_command("EcaListContexts", function()
111+
Logger.notify("EcaListContexts is deprecated. Use EcaChatListContexts instead.")
102112

103-
vim.api.nvim_create_user_command("EcaAddTodo", function(opts)
104-
if opts.args and opts.args ~= "" then
105-
require("eca.api").add_todo(opts.args)
106-
else
107-
Logger.notify("Please provide TODO content", vim.log.levels.WARN)
108-
end
113+
require("eca.api").list_contexts()
109114
end, {
110-
desc = "Add a new TODO to ECA",
111-
nargs = "+",
115+
desc = "List active contexts in ECA",
112116
})
113117

114-
vim.api.nvim_create_user_command("EcaListTodos", function()
115-
require("eca.api").list_todos()
118+
vim.api.nvim_create_user_command("EcaChatListContexts", function()
119+
require("eca.api").list_contexts()
116120
end, {
117-
desc = "List active TODOs in ECA",
121+
desc = "List active contexts in ECA",
118122
})
119123

120-
vim.api.nvim_create_user_command("EcaToggleTodo", function(opts)
121-
if opts.args and opts.args ~= "" then
122-
local index = tonumber(opts.args)
123-
if index then
124-
require("eca.api").toggle_todo(index)
125-
else
126-
Logger.notify("Please provide a valid TODO index", vim.log.levels.WARN)
127-
end
128-
else
129-
Logger.notify("Please provide TODO index to toggle", vim.log.levels.WARN)
130-
end
124+
vim.api.nvim_create_user_command("EcaClearContexts", function()
125+
Logger.notify("EcaClearContexts is deprecated. Use EcaChatClearContexts instead.")
126+
127+
require("eca.api").clear_contexts()
131128
end, {
132-
desc = "Toggle TODO completion status",
133-
nargs = 1,
129+
desc = "Clear all contexts from ECA",
134130
})
135131

136-
vim.api.nvim_create_user_command("EcaClearTodos", function()
137-
require("eca.api").clear_todos()
132+
vim.api.nvim_create_user_command("EcaChatClearContexts", function()
133+
require("eca.api").clear_contexts()
138134
end, {
139-
desc = "Clear all TODOs from ECA",
135+
desc = "Clear all contexts from ECA",
140136
})
141137

138+
-- ===== Server Commands =====
139+
142140
vim.api.nvim_create_user_command("EcaServerStart", function()
143141
require("eca.api").start_server()
144142
end, {

0 commit comments

Comments
 (0)