@@ -12,9 +12,17 @@ let s:token_headers = [
1212 \ ' Content-Type: application/json' ,
1313 \ ]
1414
15+ function ! UserInputSeparator ()
16+ let l: width = winwidth (0 )-2
17+ let l: separator = " "
18+ let l: separator .= repeat (' ━' , l: width )
19+ call append (line (' $' ), l: separator )
20+ call append (line (' $' ), ' ' )
21+ endfunction
22+
1523function ! CopilotChat ()
1624 " Open a new split window for the chat
17- split
25+ vsplit
1826 enew
1927 setlocal buftype = nofile
2028 setlocal bufhidden = hide
@@ -27,14 +35,26 @@ function! CopilotChat()
2735 " Set the buffer name to indicate it's a chat window
2836 file CopilotChat
2937
38+ syntax match CopilotWelcome / ^Welcome to Copilot Chat!.*$/
39+ syntax match CopilotSeparatorIcon / ^/ containedin =CopilotSeparatorLine
40+ syntax match CopilotSeparatorIcon / ^/ containedin =CopilotSeparatorLine
41+ syntax match CopilotSeparatorLine / ━\+ $/
42+
43+ highlight CopilotWelcome ctermfg= 205 guifg= #ff69b4
44+ highlight CopilotSeparatorIcon ctermfg= 45 guifg= #00 d7ff
45+ highlight CopilotSeparatorLine ctermfg= 205 guifg= #ff69b4
46+
3047 call append (0 , ' Welcome to Copilot Chat! Type your message below:' )
48+ call UserInputSeparator ()
3149
3250 normal ! G
3351endfunction
3452
3553function ! SubmitChatMessage ()
36- " TODO: this should be grabbing the full prompt between the separators
37- let l: message = getline (' $' )
54+ let l: separator_line = search (' ━\+$' , ' nw' )
55+ let l: start_line = l: separator_line + 1
56+ let l: end_line = line (' $' )
57+ let l: message = join (getline (l: start_line , l: end_line ), " \n " )
3858
3959 call AsyncRequest (l: message )
4060endfunction
@@ -139,6 +159,17 @@ function! CheckDeviceToken()
139159 " if the call fails we should get a new chat token and update the file
140160endfunction
141161
162+ function ! UpdateWaitingDots ()
163+ let l: line = line (' $' )
164+ let l: current_text = getline (l: line )
165+ if l: current_text = ~ ' ^Waiting for response'
166+ let l: dots = len (matchstr (l: current_text , ' \..*$' ))
167+ let l: new_dots = (l: dots % 3 ) + 1
168+ call setline (l: line , ' Waiting for response' . repeat (' .' , l: new_dots ))
169+ endif
170+ return 1
171+ endfunction
172+
142173function ! AsyncRequest (message)
143174 let s: curl_output = []
144175 let l: url = ' https://api.githubcopilot.com/chat/completions'
@@ -150,6 +181,9 @@ function! AsyncRequest(message)
150181 endif
151182
152183 let l: chat_token = GetChatToken (l: bearer_token )
184+ call append (line (' $' ), " Waiting for response" )
185+ let s: waiting_timer = timer_start (500 , {- > UpdateWaitingDots ()}, {' repeat' : -1 })
186+
153187 let l: messages = [{' content' : a: message , ' role' : ' user' }]
154188 let l: data = json_encode ({
155189 \ ' intent' : v: false ,
@@ -186,17 +220,26 @@ endfunction
186220function ! HandleCurlClose (channel, msg)
187221 let l: result = ' '
188222 for line in s: curl_output
189- if line = ~ ' ^data: {'
190- let l: json_completion = json_decode (line [6 :])
191- try
192- let l: result .= l: json_completion .choices[0 ].delta.content
193- catch
194- let l: result .= " \n "
195- endtry
196- endif
197- endfor
198- call append (line (' $' ), split (l: result , " \n " ))
199- normal ! G
223+ if line = ~ ' ^data: {'
224+ let l: json_completion = json_decode (line [6 :])
225+ try
226+ let l: content = l: json_completion .choices[0 ].delta.content
227+ if type (l: content ) != type (v: null )
228+ let l: result .= l: content
229+ endif
230+ catch
231+ let l: result .= " \n "
232+ endtry
233+ endif
234+ endfor
235+
236+ let l: width = winwidth (0 )-2
237+ let l: separator = " "
238+ let l: separator .= repeat (' ━' , l: width )
239+ call append (line (' $' ), l: separator )
240+ call append (line (' $' ), split (l: result , " \n " ))
241+ call UserInputSeparator ()
242+ normal ! G
200243endfunction
201244
202245function ! HandleCurlOutput (channel, msg)
0 commit comments