Skip to content

Commit d22bab0

Browse files
committed
fix: address PR review feedback for chat history preservation
1 parent 3243019 commit d22bab0

4 files changed

Lines changed: 58 additions & 8 deletions

File tree

lua/eca/commands.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ function M.setup()
550550
vim.api.nvim_create_user_command("EcaChatClear", function()
551551
local sidebar = require("eca").get()
552552
if sidebar then
553-
sidebar._welcome_message_applied = false
553+
sidebar._welcome_message_applied = true
554554
sidebar._force_welcome = false
555555
local chat = sidebar.containers and sidebar.containers.chat
556556
if chat and chat.bufnr and vim.api.nvim_buf_is_valid(chat.bufnr) then

lua/eca/config.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ M._defaults = {
1717
auto_start_server = false, -- Automatically start server on setup
1818
auto_download = true, -- Automatically download server if not found
1919
show_status_updates = true, -- Show status updates in notifications
20-
preserve_chat_history = false, -- When true, chat history is preserved after toggling/closing the chat window
20+
preserve_chat_history = false, -- When true, chat history is preserved across sidebar open/close cycles
2121
},
2222
context = {
2323
auto_repo_map = true, -- Automatically add repoMap context when starting new chat

lua/eca/sidebar.lua

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ function M:_close_windows_only()
161161

162162
for name, container in pairs(self.containers) do
163163
if container and container.winid and vim.api.nvim_win_is_valid(container.winid) then
164-
if preserve then
164+
if preserve and name == "chat" then
165165
-- Close only the window, keep the buffer alive
166166
pcall(vim.api.nvim_win_close, container.winid, true)
167167
container.winid = nil
@@ -654,12 +654,9 @@ function M:_setup_containers()
654654
end
655655

656656
function M:_refresh_container_content()
657-
local preserve = Config.behavior and Config.behavior.preserve_chat_history
658657
-- Refresh content without full setup
659658
if self.containers.chat then
660-
if not preserve then
661-
self:_set_welcome_content()
662-
end
659+
self:_set_welcome_content()
663660
end
664661

665662
if self.containers.config then

tests/test_chat_clear.lua

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ T["EcaChatClear"]["resets _welcome_message_applied and _force_welcome"] = functi
167167
child.cmd("EcaChatClear")
168168

169169
local flags = child.lua_get("_G.get_sidebar_flags()")
170-
eq(flags.welcome_message_applied, false)
170+
eq(flags.welcome_message_applied, true)
171171
eq(flags.force_welcome, false)
172172
end
173173

@@ -182,4 +182,57 @@ T["EcaChatClear"]["is idempotent when called twice"] = function()
182182
eq(child.lua_get("_G.get_chat_lines()"), { "" })
183183
end
184184

185+
-- preserve_chat_history toggle cycle -----------------------------------------
186+
187+
T["preserve_chat_history"] = MiniTest.new_set()
188+
189+
T["preserve_chat_history"]["reuses same bufnr and keeps content across close/open"] = function()
190+
setup_env(true)
191+
flush(200)
192+
193+
child.lua("_G.fill_chat()")
194+
local bufnr_before = child.lua_get("require('eca').get().containers.chat.bufnr")
195+
196+
child.lua([[require("eca").close_sidebar()]])
197+
flush(100)
198+
child.lua([[require("eca").open_sidebar({})]])
199+
flush(200)
200+
201+
local bufnr_after = child.lua_get("require('eca').get().containers.chat.bufnr")
202+
eq(bufnr_before, bufnr_after)
203+
eq(child.lua_get("_G.chat_has_old_content()"), true)
204+
end
205+
206+
T["preserve_chat_history"]["does not leak buffers across repeated toggles"] = function()
207+
setup_env(true)
208+
flush(200)
209+
210+
local buf_count_before = child.lua_get("#vim.api.nvim_list_bufs()")
211+
212+
for _ = 1, 5 do
213+
child.lua([[require("eca").close_sidebar()]])
214+
flush(100)
215+
child.lua([[require("eca").open_sidebar({})]])
216+
flush(200)
217+
end
218+
219+
local buf_count_after = child.lua_get("#vim.api.nvim_list_bufs()")
220+
-- Allow at most 1 extra buffer (nui internals), but definitely not 5+
221+
eq(buf_count_after - buf_count_before <= 1, true)
222+
end
223+
224+
T["preserve_chat_history"]["content is lost when preserve is disabled"] = function()
225+
setup_env(false)
226+
flush(200)
227+
228+
child.lua("_G.fill_chat()")
229+
230+
child.lua([[require("eca").close_sidebar()]])
231+
flush(100)
232+
child.lua([[require("eca").open_sidebar({})]])
233+
flush(200)
234+
235+
eq(child.lua_get("_G.chat_has_old_content()"), false)
236+
end
237+
185238
return T

0 commit comments

Comments
 (0)