11--- @class CopilotChat.Chat
22--- @field bufnr number
33--- @field winnr number
4+ --- @field separator string
45--- @field spinner CopilotChat.Spinner
56--- @field valid fun ( self : CopilotChat.Chat )
67--- @field visible fun ( self : CopilotChat.Chat )
3334
3435local Chat = class (function (self , mark_ns , help , on_buf_create )
3536 self .mark_ns = mark_ns
37+ self .header_ns = vim .api .nvim_create_namespace (' copilot-chat-headers' )
3638 self .help = help
3739 self .on_buf_create = on_buf_create
3840 self .bufnr = nil
3941 self .winnr = nil
4042 self .spinner = nil
43+ self .separator = nil
4144
4245 self .buf_create = function ()
4346 local bufnr = vim .api .nvim_create_buf (false , true )
@@ -66,6 +69,33 @@ function Chat:visible()
6669 and vim .api .nvim_win_get_buf (self .winnr ) == self .bufnr
6770end
6871
72+ function Chat :render ()
73+ if not self :visible () then
74+ return
75+ end
76+ vim .api .nvim_buf_clear_namespace (self .bufnr , self .header_ns , 0 , - 1 )
77+ local lines = vim .api .nvim_buf_get_lines (self .bufnr , 0 , - 1 , false )
78+ for l , line in ipairs (lines ) do
79+ if line :match (self .separator .. ' $' ) then
80+ local sep = vim .fn .strwidth (line ) - vim .fn .strwidth (self .separator )
81+ -- separator line
82+ vim .api .nvim_buf_set_extmark (self .bufnr , self .header_ns , l - 1 , sep , {
83+ virt_text_win_col = sep ,
84+ virt_text = { { string.rep (self .separator , vim .go .columns ), ' CopilotChatSeparator' } },
85+ priority = 100 ,
86+ strict = false ,
87+ })
88+ -- header hl group
89+ vim .api .nvim_buf_set_extmark (self .bufnr , self .header_ns , l - 1 , 0 , {
90+ end_col = sep + 1 ,
91+ hl_group = ' CopilotChatHeader' ,
92+ priority = 100 ,
93+ strict = false ,
94+ })
95+ end
96+ end
97+ end
98+
6999function Chat :active ()
70100 return vim .api .nvim_get_current_win () == self .winnr
71101end
@@ -101,11 +131,13 @@ function Chat:append(str)
101131 last_column ,
102132 vim .split (str , ' \n ' )
103133 )
134+ self :render ()
104135end
105136
106137function Chat :clear ()
107138 self :validate ()
108139 vim .api .nvim_buf_set_lines (self .bufnr , 0 , - 1 , false , {})
140+ self :render ()
109141end
110142
111143function Chat :open (config )
@@ -161,6 +193,8 @@ function Chat:open(config)
161193 vim .api .nvim_win_set_buf (self .winnr , self .bufnr )
162194 end
163195
196+ self .separator = config .separator
197+
164198 vim .wo [self .winnr ].wrap = true
165199 vim .wo [self .winnr ].linebreak = true
166200 vim .wo [self .winnr ].cursorline = true
@@ -173,6 +207,7 @@ function Chat:open(config)
173207 else
174208 vim .wo [self .winnr ].foldcolumn = ' 0'
175209 end
210+ self :render ()
176211end
177212
178213function Chat :close ()
0 commit comments