@@ -11,18 +11,23 @@ local function render_tab_header(buf)
1111end
1212
1313local function render_tab_content (buf , callback )
14- local history = require (" grok.chat.history" ).get () -- For re-rendering (Issue 1)
14+ local history = require (" grok.chat.history" ).get () -- For re-rendering
1515 vim .api .nvim_buf_set_option (buf , " modifiable" , true )
16- vim .api .nvim_buf_set_lines (buf , 1 , - 1 , false , {})
16+ vim .api .nvim_buf_set_lines (buf , 0 , - 1 , false , {})
1717 vim .cmd (" stopinsert" )
18+
19+ -- Apply chat window background
20+ vim .api .nvim_set_option_value (" winhl" , " Normal:GrokChatWindow" , { win = require (" grok.ui" ).current_win })
21+
1822 if require (" grok.ui" ).current_tab == 1 then -- Grok
1923 local chat_lines = {}
24+ local line_idx = 1 -- Track buffer line for highlighting
2025 for _ , msg in ipairs (history ) do
2126 local role = msg .role == " user" and " You" or " Grok"
2227 local content_lines = vim .split (msg .content , " \n " , { plain = true })
2328 table.insert (chat_lines , role .. " : " .. (content_lines [1 ] or " " ))
2429 for i = 2 , # content_lines do
25- table.insert (chat_lines , " " .. content_lines [i ])
30+ table.insert (chat_lines , " " .. content_lines [i ]) -- Indent continuation lines
2631 end
2732 table.insert (chat_lines , " " )
2833 end
@@ -31,6 +36,19 @@ local function render_tab_content(buf, callback)
3136 table.insert (chat_lines , " " )
3237 end
3338 vim .api .nvim_buf_set_lines (buf , 1 , - 1 , false , chat_lines )
39+
40+ -- Apply highlights per message block
41+ local current_line = 1
42+ for _ , msg in ipairs (history ) do
43+ local role = msg .role
44+ local msg_lines = # vim .split (msg .content , " \n " , { plain = true }) + 1 -- +1 for empty line
45+ local hl_group = (role == " user" ) and " GrokUser" or " GrokAssistant"
46+ for i = 0 , msg_lines - 1 do
47+ vim .api .nvim_buf_add_highlight (buf , require (" grok.ui" ).ns , hl_group , current_line + i - 1 , 0 , - 1 )
48+ end
49+ current_line = current_line + msg_lines
50+ end
51+
3452 if require (" grok.ui" ).current_win and vim .api .nvim_win_is_valid (require (" grok.ui" ).current_win ) then
3553 require (" grok.util" ).auto_scroll (buf , require (" grok.ui" ).current_win ) -- v0.1.1 Auto-scroll
3654 end
@@ -57,10 +75,9 @@ local function render_tab_content(buf, callback)
5775 " Max Tokens: " .. config .max_tokens ,
5876 " Debug: " .. tostring (config .debug ),
5977 " Prompt Position: " .. config .prompt_position , -- v0.1.1 Visible UI option
60- -- TODO: Add more; make editable? For real-time, use autocmd on BufLeave
6178 }
6279 end
63- vim .api .nvim_buf_set_lines (buf , - 1 , - 1 , false , content_lines )
80+ vim .api .nvim_buf_set_lines (buf , 1 , - 1 , false , content_lines )
6481 render_tab_header (buf ) -- Refresh header before locking
6582 vim .api .nvim_buf_set_option (buf , " modifiable" , false ) -- Lock non-chat tabs
6683 end
@@ -86,6 +103,14 @@ local function append_response(text)
86103 local lines = vim .split (new_text , " \n " , { plain = true })
87104 vim .api .nvim_buf_set_lines (require (" grok.ui" ).current_buf , line_count - 1 , line_count , false , lines )
88105 require (" grok.util" ).auto_scroll (require (" grok.ui" ).current_buf , require (" grok.ui" ).current_win ) -- v0.1.1 Auto-scroll
106+ vim .api .nvim_buf_add_highlight (
107+ require (" grok.ui" ).current_buf ,
108+ require (" grok.ui" ).ns ,
109+ " GrokAssistant" ,
110+ line_count - 1 ,
111+ 0 ,
112+ - 1
113+ )
89114 vim .api .nvim_buf_set_option (require (" grok.ui" ).current_buf , " modifiable" , false )
90115 end )
91116 if not ok then
0 commit comments