-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsidebar.lua
More file actions
1653 lines (1413 loc) · 49.9 KB
/
sidebar.lua
File metadata and controls
1653 lines (1413 loc) · 49.9 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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
local Utils = require("eca.utils")
local Logger = require("eca.logger")
local Config = require("eca.config")
-- Load nui.nvim components (required dependency)
local Split = require("nui.split")
---@class eca.Sidebar
---@field public id integer The tab ID
---@field public containers table<string, NuiSplit> The nui containers
---@field mediator eca.Mediator mediator to send server requests to
---@field private _initialized boolean Whether the sidebar has been initialized
---@field private _current_response_buffer string Buffer for accumulating streaming response
---@field private _is_streaming boolean Whether we're currently receiving a streaming response
---@field private _last_assistant_line integer Line number of the last assistant message
---@field private _usage_info string Current usage information
---@field private _last_user_message string Last user message to avoid duplicates
---@field private _current_tool_call table Current tool call being accumulated
---@field private _is_tool_call_streaming boolean Whether we're currently receiving a streaming tool call
---@field private _force_welcome boolean Whether to force show welcome content on next open
---@field private _contexts table Active contexts for this chat session
---@field private _selected_code table Current selected code for display
---@field private _todos table List of active todos
---@field private _current_status string Current processing status message
---@field private _augroup integer Autocmd group ID
---@field private _response_start_time number Timestamp when streaming started
---@field private _max_response_length number Maximum allowed response length
local M = {}
M.__index = M
-- Height calculation constants
local MIN_CHAT_HEIGHT = 10 -- Minimum lines for chat container to remain usable
local WINDOW_MARGIN = 3 -- Additional margin for window borders and spacing
local UI_ELEMENTS_HEIGHT = 2 -- Reserve space for statusline and tabline
local SAFETY_MARGIN = 2 -- Extra margin to prevent "Not enough room" errors
---@param id integer Tab ID
---@param mediator eca.Mediator
---@return eca.Sidebar
function M.new(id, mediator)
local instance = setmetatable({}, M)
instance.id = id
instance.mediator = mediator
instance.containers = {}
instance._initialized = false
instance._current_response_buffer = ""
instance._is_streaming = false
instance._last_assistant_line = 0
instance._usage_info = ""
instance._last_user_message = ""
instance._current_tool_call = nil
instance._is_tool_call_streaming = false
instance._force_welcome = false
instance._contexts = {}
instance._selected_code = nil
instance._todos = {}
instance._current_status = ""
instance._augroup = vim.api.nvim_create_augroup("eca_sidebar_" .. id, { clear = true })
instance._response_start_time = 0
instance._max_response_length = 50000 -- 50KB max response
require("eca.observer").subscribe(id, function(message)
instance:handle_chat_content(message)
end)
return instance
end
---@return boolean
function M:is_open()
return self.containers.chat and self.containers.chat.winid and vim.api.nvim_win_is_valid(self.containers.chat.winid)
end
---@param opts? table
function M:open(opts)
opts = opts or {}
if self:is_open() then
self:_focus_input()
return
end
-- Clean up any invalid containers
self:_cleanup_invalid_containers()
-- Create/recreate containers using nui.split
self:_create_containers()
-- Setup containers if not initialized or if we need to refresh content
if not self._initialized then
Logger.debug("Setting up containers (first time)")
self:_setup_containers()
else
Logger.debug("Reusing existing containers")
self:_refresh_container_content()
end
-- Always focus input when opening
self:_focus_input()
Logger.debug("ECA sidebar opened")
end
function M:close()
self:_close_windows_only()
end
function M:_close_windows_only()
for name, container in pairs(self.containers) do
if container and container.winid and vim.api.nvim_win_is_valid(container.winid) then
container:unmount()
-- Keep the container reference but mark window as invalid
container.winid = nil
end
end
Logger.debug("ECA sidebar windows closed")
end
function M:_close_and_cleanup()
for name, container in pairs(self.containers) do
if container then
if container.winid and vim.api.nvim_win_is_valid(container.winid) then
container:unmount()
end
-- Check if buffer is displayed elsewhere before deleting
if container.bufnr and vim.api.nvim_buf_is_valid(container.bufnr) then
local wins = vim.fn.win_findbuf(container.bufnr)
if #wins == 0 then
pcall(vim.api.nvim_buf_delete, container.bufnr, { force = true })
end
end
end
end
self.containers = {}
Logger.debug("ECA sidebar closed and cleaned up")
end
---@param opts? table
---@return boolean
function M:toggle(opts)
if self:is_open() then
self:close()
return false
else
self:open(opts)
return true
end
end
function M:focus()
if self:is_open() then
self:_focus_input()
else
self:open()
end
end
function M:resize()
if not self:is_open() then
return
end
-- Recalculate and update container sizes
self:_update_container_sizes()
end
function M:reset()
if self:is_open() then
self:_close_and_cleanup()
else
self:_close_and_cleanup()
end
-- Reset all state
self.containers = {}
self._initialized = false
self._is_streaming = false
self._current_response_buffer = ""
self._last_assistant_line = 0
self._usage_info = ""
self._last_user_message = ""
self._current_tool_call = nil
self._is_tool_call_streaming = false
self._force_welcome = false
self._contexts = {}
self._selected_code = nil
self._todos = {}
self._current_status = ""
end
function M:new_chat()
self:reset()
self._force_welcome = true
Logger.debug("New chat initiated - will show welcome content on next open")
end
---@private
function M:_cleanup_invalid_containers()
for name, container in pairs(self.containers) do
if container then
-- Check if window is still valid
if container.winid and not vim.api.nvim_win_is_valid(container.winid) then
container.winid = nil
end
-- Check if buffer is still valid
if container.bufnr and not vim.api.nvim_buf_is_valid(container.bufnr) then
container.bufnr = nil
end
end
end
end
---@private
function M:_create_containers()
local width = Config.get_window_width()
-- Calculate dynamic heights using existing methods
local input_height = Config.windows.input.height
local usage_height = 1
local status_height = 1
local contexts_height = self:get_contexts_height()
local selected_code_height = self:get_selected_code_height()
local todos_height = self:get_todos_height()
local original_chat_height = self:get_chat_height()
local chat_height = original_chat_height
-- Validate total height to prevent "Not enough room" error
local total_height = chat_height
+ selected_code_height
+ todos_height
+ status_height
+ contexts_height
+ input_height
+ usage_height
-- Always calculate from total screen minus UI elements (more accurate than current window)
local available_height = vim.o.lines - UI_ELEMENTS_HEIGHT
if total_height > available_height then
Logger.debug(
string.format(
"Total height (%d) exceeds available height (%d), adjusting chat height",
total_height,
available_height
)
)
local extra_height = total_height - (available_height - SAFETY_MARGIN)
chat_height = math.max(MIN_CHAT_HEIGHT, chat_height - extra_height)
Logger.debug(string.format("Adjusted chat height from %d to %d", original_chat_height, chat_height))
end
-- Base options for all containers
local base_buf_options = {
buftype = "nofile",
bufhidden = "hide",
swapfile = false,
}
local base_win_options = {
wrap = Config.windows.wrap,
number = false,
relativenumber = false,
signcolumn = "no",
foldcolumn = "0",
cursorline = false,
winfixheight = true,
winfixwidth = false,
}
-- 1. Create and mount main chat container first
self.containers.chat = Split({
relative = "editor",
position = "right",
size = {
width = width,
height = chat_height,
},
buf_options = vim.tbl_deep_extend("force", base_buf_options, {
modifiable = true,
filetype = "markdown",
}),
win_options = base_win_options,
})
self.containers.chat:mount()
self:_setup_container_events(self.containers.chat, "chat")
-- Track the current container for hierarchical mounting with proper space management
local current_winid = self.containers.chat.winid
Logger.debug("Mounted container: chat (winid: " .. current_winid .. ")")
-- 2. Create selected_code container (conditional)
if selected_code_height > 0 then
self.containers.selected_code = Split({
relative = {
type = "win",
winid = current_winid,
},
position = "bottom",
size = { height = selected_code_height },
buf_options = vim.tbl_deep_extend("force", base_buf_options, {
modifiable = false,
filetype = self._selected_code and self._selected_code.filetype or "text",
}),
win_options = vim.tbl_deep_extend("force", base_win_options, {
winhighlight = "Normal:Visual",
}),
})
self.containers.selected_code:mount()
self:_setup_container_events(self.containers.selected_code, "selected_code")
current_winid = self.containers.selected_code.winid
Logger.debug("Mounted container: selected_code (winid: " .. current_winid .. ")")
end
-- 3. Create todos container (conditional)
if todos_height > 0 then
self.containers.todos = Split({
relative = {
type = "win",
winid = current_winid,
},
position = "bottom",
size = { height = todos_height },
buf_options = vim.tbl_deep_extend("force", base_buf_options, {
modifiable = false,
}),
win_options = vim.tbl_deep_extend("force", base_win_options, {
winhighlight = "Normal:DiffAdd",
}),
})
self.containers.todos:mount()
self:_setup_container_events(self.containers.todos, "todos")
current_winid = self.containers.todos.winid
Logger.debug("Mounted container: todos (winid: " .. current_winid .. ")")
end
-- 4. Create status container (always present) - for processing messages
self.containers.status = Split({
relative = {
type = "win",
winid = current_winid,
},
position = "bottom",
size = { height = status_height },
buf_options = vim.tbl_deep_extend("force", base_buf_options, {
modifiable = false,
}),
win_options = vim.tbl_deep_extend("force", base_win_options, {
winhighlight = "Normal:WarningMsg",
}),
})
self.containers.status:mount()
self:_setup_container_events(self.containers.status, "status")
current_winid = self.containers.status.winid
Logger.debug("Mounted container: status (winid: " .. current_winid .. ")")
-- 5. Create contexts container between status and input
self.containers.contexts = Split({
relative = {
type = "win",
winid = current_winid,
},
position = "bottom",
size = { height = contexts_height },
buf_options = vim.tbl_deep_extend("force", base_buf_options, {
modifiable = false,
}),
win_options = vim.tbl_deep_extend("force", base_win_options, {
winhighlight = "Normal:Comment",
}),
})
self.containers.contexts:mount()
self:_setup_container_events(self.containers.contexts, "contexts")
current_winid = self.containers.contexts.winid
Logger.debug("Mounted container: contexts (winid: " .. current_winid .. ")")
-- 6. Create input container (always present)
self.containers.input = Split({
relative = {
type = "win",
winid = current_winid,
},
position = "bottom",
size = { height = input_height },
buf_options = vim.tbl_deep_extend("force", base_buf_options, {
modifiable = true,
filetype = "eca-input",
}),
win_options = vim.tbl_deep_extend("force", base_win_options, {
statusline = " ",
}),
})
self.containers.input:mount()
self:_setup_container_events(self.containers.input, "input")
current_winid = self.containers.input.winid
Logger.debug("Mounted container: input (winid: " .. current_winid .. ")")
-- 7. Create usage container (always present) - moved to bottom
self.containers.usage = Split({
relative = {
type = "win",
winid = current_winid,
},
position = "bottom",
size = { height = usage_height },
buf_options = vim.tbl_deep_extend("force", base_buf_options, {
modifiable = false,
}),
win_options = vim.tbl_deep_extend("force", base_win_options, {
winhighlight = "Normal:StatusLine",
statusline = " ",
}),
})
self.containers.usage:mount()
self:_setup_container_events(self.containers.usage, "usage")
Logger.debug("Mounted container: usage (winid: " .. self.containers.usage.winid .. ")")
Logger.debug(
string.format(
"Created containers: contexts=%d, chat=%d, selected_code=%s, todos=%s, status=%d, input=%d, usage=%d",
contexts_height,
chat_height,
selected_code_height > 0 and tostring(selected_code_height) or "hidden",
todos_height > 0 and tostring(todos_height) or "hidden",
status_height,
input_height,
usage_height
)
)
end
---@private
---@param container NuiSplit
---@param name string
function M:_setup_container_events(container, name)
-- Setup container-specific keymaps
if name == "todos" then
self:_setup_todos_keymaps(container)
elseif name == "input" then
self:_setup_input_keymaps(container)
elseif name == "status" then
-- No special keymaps for status container (read-only)
end
end
---@private
---@param name string
function M:_handle_container_closed(name)
-- Handle when a container window is closed
if self.containers[name] then
self.containers[name].winid = nil
end
end
---@private
---@param container NuiSplit
function M:_setup_todos_keymaps(container)
-- Setup keymaps for todos container
container:map("n", "<Space>", function()
local line = vim.api.nvim_win_get_cursor(container.winid)[1]
if line > 0 then
local header_offset = Config.windows.sidebar_header.enabled and 1 or 0
local todo_index = line - header_offset
if todo_index > 0 and todo_index <= #self._todos then
self:toggle_todo(todo_index)
end
end
end, { noremap = true, silent = true })
container:map("n", "<Enter>", function()
local line = vim.api.nvim_win_get_cursor(container.winid)[1]
if line > 0 then
local header_offset = Config.windows.sidebar_header.enabled and 1 or 0
local todo_index = line - header_offset
if todo_index > 0 and todo_index <= #self._todos then
self:toggle_todo(todo_index)
end
end
end, { noremap = true, silent = true })
end
---@private
---@param container NuiSplit
function M:_setup_input_keymaps(container)
-- Setup keymaps for input container
container:map("n", "<C-s>", function()
self:_handle_input()
end, { noremap = true, silent = true })
container:map("i", "<C-s>", function()
self:_handle_input()
end, { noremap = true, silent = true })
end
---@private
function M:_update_container_sizes()
if not self:is_open() then
return
end
-- Recalculate heights
local new_heights = {
contexts = self:get_contexts_height(),
chat = self:get_chat_height(),
selected_code = self:get_selected_code_height(),
todos = self:get_todos_height(),
status = 1,
input = Config.windows.input.height,
usage = 1,
}
-- Update container sizes
for name, height in pairs(new_heights) do
local container = self.containers[name]
if container and container.winid and vim.api.nvim_win_is_valid(container.winid) then
if height > 0 then
vim.api.nvim_win_set_height(container.winid, height)
end
end
end
end
-- Include all the height calculation methods from the original sidebar
function M:get_selected_code_height()
if not self._selected_code or not Config.selected_code.enabled then
return 0
end
local lines = vim.split(self._selected_code.content or "", "\n")
local content_lines = #lines
local header_lines = Config.windows.sidebar_header.enabled and 1 or 0
return math.min(Config.selected_code.max_height, content_lines + header_lines + 1)
end
function M:get_todos_height()
if #self._todos == 0 or not Config.todos.enabled then
return 0
end
local header_lines = Config.windows.sidebar_header.enabled and 1 or 0
local todo_lines = math.min(#self._todos, Config.todos.max_height - header_lines)
return math.min(Config.todos.max_height, todo_lines + header_lines)
end
function M:get_contexts_height()
if #self._contexts == 0 then
return 1 -- Always show at least "No contexts"
end
local header_lines = 1 -- "📂 Contexts (N):"
local bubble_lines = 1 -- All bubbles on same line as requested
return header_lines + bubble_lines
end
function M:get_chat_height()
local total_height = vim.o.lines - vim.o.cmdheight - 1
local input_height = Config.windows.input.height
local usage_height = 1
local status_height = 1
local contexts_height = self:get_contexts_height()
local selected_code_height = self:get_selected_code_height()
local todos_height = self:get_todos_height()
return math.max(
MIN_CHAT_HEIGHT,
total_height
- input_height
- usage_height
- status_height
- contexts_height
- selected_code_height
- todos_height
- WINDOW_MARGIN
)
end
-- Include all the context/todo/selected_code management methods from original sidebar
-- (These will be copied from the original implementation)
---@param context table Context object with type, path, content
function M:add_context(context)
-- Check if context already exists (by path)
for i, existing in ipairs(self._contexts) do
if existing.path == context.path then
-- Update existing context
self._contexts[i] = context
self:_update_contexts_display()
Logger.info("Updated context: " .. context.path)
return
end
end
-- Add new context
table.insert(self._contexts, context)
self:_update_contexts_display()
Logger.info("Added context: " .. context.path .. " (" .. #self._contexts .. " total)")
end
---@param path string Path to remove from contexts
function M:remove_context(path)
for i, context in ipairs(self._contexts) do
if context.path == path then
table.remove(self._contexts, i)
self:_update_contexts_display()
Logger.info("Removed context: " .. path)
return true
end
end
Logger.warn("Context not found: " .. path)
return false
end
---@return table List of active contexts
function M:get_contexts()
return vim.deepcopy(self._contexts)
end
---@return integer Number of active contexts
function M:get_context_count()
return #self._contexts
end
function M:clear_contexts()
local count = #self._contexts
self._contexts = {}
self:_update_contexts_display()
Logger.info("Cleared " .. count .. " contexts")
end
---@param code table Selected code object with filepath, content, start_line, end_line
function M:set_selected_code(code)
self._selected_code = code
self:_update_selected_code_display()
Logger.info("Selected code updated: " .. (code and code.filepath or "none"))
end
function M:clear_selected_code()
self._selected_code = nil
self:_update_selected_code_display()
Logger.info("Selected code cleared")
end
---@param todo table Todo object with content, status ("pending", "completed")
function M:add_todo(todo)
table.insert(self._todos, todo)
self:_update_todos_display()
Logger.info("Added TODO: " .. todo.content)
end
---@param index integer Index of todo to toggle
function M:toggle_todo(index)
if index <= 0 or index > #self._todos then
Logger.warn("Invalid TODO index: " .. index)
return false
end
local todo = self._todos[index]
todo.status = todo.status == "completed" and "pending" or "completed"
self:_update_todos_display()
Logger.info("Toggled TODO " .. index .. ": " .. todo.content)
return true
end
function M:clear_todos()
local count = #self._todos
self._todos = {}
self:_update_todos_display()
Logger.info("Cleared " .. count .. " TODOs")
end
---@return table List of active todos
function M:get_todos()
return vim.deepcopy(self._todos)
end
-- Placeholder methods for the display and setup functions
-- These will use the same logic as the original sidebar but with nui containers
function M:_setup_containers()
-- Setup each container's content and behavior
self:_setup_contexts_container()
self:_setup_chat_container()
if self.containers.selected_code then
self:_setup_selected_code_container()
end
if self.containers.todos then
self:_setup_todos_container()
end
self:_setup_status_container()
self:_setup_input_container()
self:_setup_usage_container()
self._initialized = true
end
function M:_refresh_container_content()
-- Refresh content without full setup
if self.containers.contexts then
self:_update_contexts_display()
end
if self.containers.chat then
self:_set_welcome_content()
end
if self.containers.selected_code then
self:_update_selected_code_display()
end
if self.containers.todos then
self:_update_todos_display()
end
if self.containers.status then
self:_update_status_display()
end
if self.containers.input then
self:_add_input_line()
end
if self.containers.usage then
self:_update_usage_info(self._usage_info)
end
end
-- Placeholder for all the other methods from original sidebar
-- (These would be copied over with minimal modifications to work with nui containers)
function M:_setup_chat_container()
local chat = self.containers.chat
if not chat then
return
end
-- Set buffer options for chat
vim.api.nvim_set_option_value("buftype", "nofile", { buf = chat.bufnr })
vim.api.nvim_set_option_value("bufhidden", "hide", { buf = chat.bufnr })
vim.api.nvim_set_option_value("swapfile", false, { buf = chat.bufnr })
vim.api.nvim_set_option_value("modifiable", true, { buf = chat.bufnr })
-- Disable treesitter initially to prevent highlighting errors during setup
vim.api.nvim_set_option_value("syntax", "off", { buf = chat.bufnr })
-- Set initial content first
self:_set_welcome_content()
-- Set filetype to markdown for syntax highlighting
vim.defer_fn(function()
if vim.api.nvim_buf_is_valid(chat.bufnr) then
vim.api.nvim_set_option_value("filetype", "markdown", { buf = chat.bufnr })
vim.api.nvim_set_option_value("syntax", "on", { buf = chat.bufnr })
end
end, 200)
end
function M:_setup_selected_code_container()
local container = self.containers.selected_code
if not container then
return
end
-- Set initial content
self:_update_selected_code_display()
-- Set filetype based on the selected code's language
if self._selected_code and self._selected_code.filetype then
vim.api.nvim_set_option_value("filetype", self._selected_code.filetype, { buf = container.bufnr })
end
end
function M:_setup_todos_container()
local container = self.containers.todos
if not container then
return
end
-- Set initial content
self:_update_todos_display()
end
function M:_setup_contexts_container()
local contexts = self.containers.contexts
if not contexts then
return
end
-- Set initial contexts display
self:_update_contexts_display()
end
function M:_setup_status_container()
local status = self.containers.status
if not status then
return
end
-- Set initial status display
self:_update_status_display()
end
function M:_setup_usage_container()
local usage = self.containers.usage
if not usage then
return
end
-- Set initial usage info
vim.api.nvim_set_option_value("modifiable", true, { buf = usage.bufnr })
vim.api.nvim_buf_set_lines(usage.bufnr, 0, -1, false, { "Usage: Tokens | Cost" })
vim.api.nvim_set_option_value("modifiable", false, { buf = usage.bufnr })
end
function M:_setup_input_container()
local input = self.containers.input
if not input then
return
end
-- Set initial input prompt
self:_add_input_line()
end
-- Placeholder methods that need to be implemented
-- (These would be copied from the original sidebar with minimal modifications)
function M:_set_welcome_content()
-- Implementation from original sidebar
local chat = self.containers.chat
if not chat then
return
end
-- Check if we should force welcome content (new chat)
if not self._force_welcome then
-- Check if buffer already has content (more than just empty lines)
local existing_lines = vim.api.nvim_buf_get_lines(chat.bufnr, 0, -1, false)
local has_content = false
for _, line in ipairs(existing_lines) do
if line:match("%S") then -- Has non-whitespace content
has_content = true
break
end
end
-- Only set welcome content if buffer is empty or has no meaningful content
if has_content then
Logger.debug("Preserving existing chat content")
return
end
else
-- Force welcome content and reset the flag
Logger.debug("Forcing welcome content for new chat")
self._force_welcome = false
end
local lines = {
"# 🤖 ECA - Editor Code Assistant",
"",
"> **Welcome to ECA!** Your AI-powered code assistant is ready to help.",
"",
"## 🚀 Getting Started",
"",
"- **Chat**: Type your message in the input field at the bottom and press `Ctrl+S` to send",
"- **Multiline**: Use `Enter` for new lines, `Ctrl+S` to send",
"- **Context**: Use `@` to mention files or directories",
"- **Context**: Use `:EcaAddFile` to add files, `:EcaListContexts` to view, `:EcaClearContexts` to clear",
"- **Selection**: Use `:EcaAddSelection` to add code selection",
"- **RepoMap**: Use `:EcaAddRepoMap` to add repository structure context",
"",
"---",
}
Logger.debug("Setting welcome content for new chat")
vim.api.nvim_buf_set_lines(chat.bufnr, 0, -1, false, lines)
-- Auto-add repoMap context if enabled and not already present
if Config.options.context.auto_repo_map then
-- Check if repoMap already exists
local has_repo_map = false
for _, context in ipairs(self._contexts) do
if context.type == "repoMap" then
has_repo_map = true
break
end
end
if not has_repo_map then
self:add_context({
type = "repoMap",
path = "repoMap",
content = "Repository structure and code mapping for better project understanding",
})
Logger.debug("Auto-added repoMap context on welcome")
end
end
end
function M:_add_input_line()
return vim.schedule(function()
local input = self.containers.input
if not input then
return
end
local prefix = Config.windows.input.prefix or "> "
vim.api.nvim_buf_set_lines(input.bufnr, 0, -1, false, { prefix })
-- Set cursor to end of line
if vim.api.nvim_win_is_valid(input.winid) then
vim.api.nvim_win_set_cursor(input.winid, { 1, #prefix })
end
end)
end
function M:_focus_input()
local input = self.containers.input
if not input or not vim.api.nvim_win_is_valid(input.winid) then
Logger.notify("Cannot focus input: invalid window", vim.log.levels.ERROR)
return
end
vim.defer_fn(function()
if vim.api.nvim_win_is_valid(input.winid) and vim.api.nvim_buf_is_valid(input.bufnr) then
vim.api.nvim_set_current_win(input.winid)
local lines = vim.api.nvim_buf_get_lines(input.bufnr, 0, -1, false)
local prefix = Config.windows.input.prefix or "> "
if #lines > 0 then
local first_line = lines[1] or ""
local cursor_col = math.max(#prefix, #first_line)
vim.api.nvim_win_set_cursor(input.winid, { 1, cursor_col })
else
self:_add_input_line()
end
-- Enter insert mode
local mode = vim.api.nvim_get_mode().mode
if mode == "n" then
vim.cmd("startinsert!")
end
end
end, 50)
end
function M:_handle_input()
local input = self.containers.input
if not input then
return
end
local lines = vim.api.nvim_buf_get_lines(input.bufnr, 0, -1, false)
if #lines == 0 then
return
end
-- Process input (remove prefix, concatenate lines)
local message_lines = {}
local prefix = Config.windows.input.prefix or "> "
for _, line in ipairs(lines) do
local content = line
if vim.startswith(line, prefix) then
content = line:sub(#prefix + 1)
end
if content ~= "" then
table.insert(message_lines, content)
end
end
local message = table.concat(message_lines, "\n")
if message == "" then
return
end
-- Clear input
vim.api.nvim_buf_set_lines(input.bufnr, 0, -1, false, {})
-- Send message
self:_send_message(message)
-- Add new input line and focus
self:_add_input_line()
self:_focus_input()
end
-- Placeholder for the other display update methods
function M:_update_selected_code_display()
-- Implementation would be similar to original but use nui container
local container = self.containers.selected_code
if not container or not vim.api.nvim_buf_is_valid(container.bufnr) then
return
end