Skip to content

Commit e9296f1

Browse files
committed
Fixing contexts and adding auto repoMap
1 parent 47c9f34 commit e9296f1

File tree

5 files changed

+355
-27
lines changed

5 files changed

+355
-27
lines changed

lua/eca/api.lua

Lines changed: 119 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,19 @@ function M.add_file_context(file_path)
7575
content = content
7676
}
7777

78-
-- For now, store it for next message
79-
-- TODO: Implement context management
80-
Utils.debug("File context added: " .. file_path)
78+
-- Get current sidebar and add context
79+
local sidebar = eca.get()
80+
if not sidebar then
81+
Utils.info("Opening ECA sidebar to add context...")
82+
M.chat()
83+
sidebar = eca.get()
84+
end
85+
86+
if sidebar then
87+
sidebar:add_context(context)
88+
else
89+
Utils.error("Failed to create ECA sidebar")
90+
end
8191
end
8292

8393
---@param directory_path string
@@ -123,8 +133,112 @@ function M.add_selection_context()
123133
local lines = vim.api.nvim_buf_get_lines(0, start_pos[2] - 1, end_pos[2], false)
124134
if #lines > 0 then
125135
local selection_text = table.concat(lines, "\n")
126-
Utils.info("Adding selection context (" .. #lines .. " lines)")
127-
-- TODO: Send selection to ECA server as context
136+
local current_file = vim.api.nvim_buf_get_name(0)
137+
local context_path = current_file .. ":" .. start_pos[2] .. "-" .. end_pos[2]
138+
139+
-- Create context object
140+
local context = {
141+
type = "selection",
142+
path = context_path,
143+
content = selection_text,
144+
source_file = current_file,
145+
start_line = start_pos[2],
146+
end_line = end_pos[2]
147+
}
148+
149+
-- Get current sidebar and add context
150+
local eca = require("eca")
151+
local sidebar = eca.get()
152+
if not sidebar then
153+
Utils.info("Opening ECA sidebar to add context...")
154+
M.chat()
155+
sidebar = eca.get()
156+
end
157+
158+
if sidebar then
159+
sidebar:add_context(context)
160+
Utils.info("Added selection context (" .. #lines .. " lines)")
161+
else
162+
Utils.error("Failed to create ECA sidebar")
163+
end
164+
end
165+
end
166+
167+
function M.list_contexts()
168+
local eca = require("eca")
169+
local sidebar = eca.get()
170+
if not sidebar then
171+
Utils.warn("No active ECA sidebar")
172+
return
173+
end
174+
175+
local contexts = sidebar:get_contexts()
176+
if #contexts == 0 then
177+
Utils.info("No active contexts")
178+
return
179+
end
180+
181+
Utils.info("Active contexts (" .. #contexts .. "):")
182+
for i, context in ipairs(contexts) do
183+
local size_info = ""
184+
if context.content then
185+
local lines = vim.split(context.content, "\n")
186+
size_info = " (" .. #lines .. " lines)"
187+
end
188+
Utils.info(i .. ". " .. context.type .. ": " .. context.path .. size_info)
189+
end
190+
end
191+
192+
function M.clear_contexts()
193+
local eca = require("eca")
194+
local sidebar = eca.get()
195+
if not sidebar then
196+
Utils.warn("No active ECA sidebar")
197+
return
198+
end
199+
200+
sidebar:clear_contexts()
201+
end
202+
203+
function M.remove_context(path)
204+
local eca = require("eca")
205+
local sidebar = eca.get()
206+
if not sidebar then
207+
Utils.warn("No active ECA sidebar")
208+
return
209+
end
210+
211+
sidebar:remove_context(path)
212+
end
213+
214+
function M.add_repo_map_context()
215+
local eca = require("eca")
216+
local sidebar = eca.get()
217+
if not sidebar then
218+
Utils.info("Opening ECA sidebar to add repoMap context...")
219+
M.chat()
220+
sidebar = eca.get()
221+
end
222+
223+
if sidebar then
224+
-- Check if repoMap already exists
225+
local contexts = sidebar:get_contexts()
226+
for _, context in ipairs(contexts) do
227+
if context.type == "repoMap" then
228+
Utils.info("RepoMap context already added")
229+
return
230+
end
231+
end
232+
233+
-- Add repoMap context
234+
sidebar:add_context({
235+
type = "repoMap",
236+
path = "repoMap",
237+
content = "Repository structure and code mapping for better project understanding"
238+
})
239+
Utils.info("Added repoMap context")
240+
else
241+
Utils.error("Failed to create ECA sidebar")
128242
end
129243
end
130244

lua/eca/commands.lua

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,36 @@ function M.setup()
4747
desc = "Add current selection as context to ECA",
4848
})
4949

50+
vim.api.nvim_create_user_command("EcaListContexts", function()
51+
require("eca.api").list_contexts()
52+
end, {
53+
desc = "List active contexts in ECA",
54+
})
55+
56+
vim.api.nvim_create_user_command("EcaClearContexts", function()
57+
require("eca.api").clear_contexts()
58+
end, {
59+
desc = "Clear all contexts from ECA",
60+
})
61+
62+
vim.api.nvim_create_user_command("EcaRemoveContext", function(opts)
63+
if opts.args and opts.args ~= "" then
64+
require("eca.api").remove_context(opts.args)
65+
else
66+
Utils.warn("Please provide a file path to remove")
67+
end
68+
end, {
69+
desc = "Remove specific context from ECA",
70+
nargs = "+",
71+
complete = "file",
72+
})
73+
74+
vim.api.nvim_create_user_command("EcaAddRepoMap", function()
75+
require("eca.api").add_repo_map_context()
76+
end, {
77+
desc = "Add repository map context to ECA",
78+
})
79+
5080
vim.api.nvim_create_user_command("EcaServerStart", function()
5181
require("eca.api").start_server()
5282
end, {

lua/eca/config.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ M._defaults = {
2121
enable = true, -- Enable markview.nvim integration
2222
filetypes = { "markdown", "Eca" }, -- Filetypes to enable markview
2323
},
24+
context = {
25+
auto_repo_map = true, -- Automatically add repoMap context when starting new chat
26+
},
2427
mappings = {
2528
chat = "<leader>ec",
2629
focus = "<leader>ef",

lua/eca/path_finder.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ function M:_download_latest_server(server_path, version)
137137

138138
local download_path = self._cache_dir .. "/" .. artifact_name
139139

140-
Utils.info("Downloading ECA server from: " .. download_url)
140+
Utils.info("Downloading latest ECA server version from: " .. download_url)
141141

142142
-- Ensure cache directory exists
143143
vim.fn.mkdir(self._cache_dir, "p")

0 commit comments

Comments
 (0)