Skip to content

Commit 3220b6a

Browse files
committed
add tests for contexts area
1 parent f5f38bd commit 3220b6a

File tree

2 files changed

+408
-13
lines changed

2 files changed

+408
-13
lines changed

lua/eca/sidebar.lua

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ local Split = require("nui.split")
88
---@class eca.Sidebar
99
---@field public id integer The tab ID
1010
---@field public containers table<string, NuiSplit> The nui containers
11+
---@field public extmarks table The extmarks for various UI elements
1112
---@field mediator eca.Mediator mediator to send server requests to
1213
---@field private _initialized boolean Whether the sidebar has been initialized
1314
---@field private _current_response_buffer string Buffer for accumulating streaming response
@@ -411,19 +412,32 @@ function M:_setup_input_events(container)
411412
return
412413
end
413414

414-
local prefix_row = unpack(vim.api.nvim_buf_get_extmark_by_id(buf, prefix_ns, prefix_id, {}))
415+
local prefix_mark = vim.api.nvim_buf_get_extmark_by_id(buf, prefix_ns, prefix_id, {})
416+
local prefix_row = unpack(prefix_mark)
415417
local contexts_row = 0
416418

417-
-- If both are in the same row, contexts_row was deleted
419+
local prefix_line = lines[prefix_row + 1] or nil
420+
local contexts_line = lines[contexts_row + 1] or nil
421+
local contexts_placeholder_line = self._contexts_placeholder_line or ""
422+
423+
-- Logger.test(lines)
424+
-- Logger.test("first line changed: " .. first)
425+
-- Logger.test(prefix_mark)
426+
418427
if prefix_row == contexts_row then
428+
-- prefix line missing, restore
429+
if contexts_line == contexts_placeholder_line then
430+
-- Logger.test("prefix line missing, restore")
431+
self:_update_input_display()
432+
return
433+
end
434+
435+
-- we can consider that contexts were deleted
436+
-- Logger.test("contexts cleared")
419437
self.mediator:clear_contexts()
420438
return
421439
end
422440

423-
local prefix_line = lines[prefix_row + 1] or nil
424-
local contexts_line = lines[contexts_row + 1] or nil
425-
local contexts_placeholder_line = self._contexts_placeholder_line or ""
426-
427441
-- prefix line missing, restore
428442
if not prefix_line and contexts_line == contexts_placeholder_line then
429443
self:_update_input_display()
@@ -669,14 +683,11 @@ function M:_update_input_display(opts)
669683
end
670684
end
671685

672-
local old_contexts_placeholder_line = self._contexts_placeholder_line
673-
674686
self._contexts_placeholder_line = "@"
675687
for _ = 1, #contexts_name do
676688
self._contexts_placeholder_line = self._contexts_placeholder_line .. "@"
677689
end
678690

679-
680691
local prefix_extmark = self.extmarks.prefix or nil
681692
local prefix_ns = prefix_extmark and prefix_extmark._ns or nil
682693
local prefix_id = prefix_extmark and prefix_extmark._id and prefix_extmark._id[1] or nil
@@ -702,6 +713,8 @@ function M:_update_input_display(opts)
702713
self.extmarks.contexts._id = {}
703714
end
704715

716+
vim.api.nvim_buf_clear_namespace(input.bufnr, self.extmarks.contexts._ns, 0, -1)
717+
705718
for i, context_name in ipairs(contexts_name) do
706719
self.extmarks.contexts._id[i] = vim.api.nvim_buf_set_extmark(
707720
input.bufnr,
@@ -738,10 +751,6 @@ function M:_update_input_display(opts)
738751
vim.tbl_extend("force", { virt_text = { { prefix, "Normal" } }, virt_text_pos = "inline", right_gravity = false }, { id = self.extmarks.prefix._id[1] })
739752
)
740753

741-
-- local prefix_mark = vim.api.nvim_buf_get_extmark_by_id(input.bufnr, self.extmarks.prefix._ns, self.extmarks.prefix._id[1], { details = true })
742-
-- local prefix_mark = opts and opts.prefix_mark or {}
743-
-- vim.notify(vim.inspect(prefix_mark), vim.log.levels.DEBUG)
744-
745754
-- Set cursor to end of input line
746755
if vim.api.nvim_win_is_valid(input.winid) then
747756
local row = 1 + (not clear and existing_lines and #existing_lines > 0 and #existing_lines or 1)

0 commit comments

Comments
 (0)