Skip to content

Commit fb29a9f

Browse files
Augment Vim v0.10.1
1 parent 97ceeb2 commit fb29a9f

File tree

8 files changed

+285
-108
lines changed

8 files changed

+285
-108
lines changed

README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,15 @@ appear.
5858
The following commands are provided:
5959
6060
```vim
61-
:Augment status " View the current status of the plugin
62-
:Augment signin " Start the sign in flow
63-
:Augment signout " Sign out of Augment
64-
:Augment enable " Globally enable suggestions (on by default)
65-
:Augment disable " Globally disable suggestions
66-
:Augment log " View the plugin log
67-
:Augment chat " Start a chat with Augment AI
61+
:Augment status " View the current status of the plugin
62+
:Augment signin " Start the sign in flow
63+
:Augment signout " Sign out of Augment
64+
:Augment enable " Globally enable suggestions (on by default)
65+
:Augment disable " Globally disable suggestions
66+
:Augment log " View the plugin log
67+
:Augment chat " Send a chat message to Augment AI
68+
:Augment chat-new " Start a new chat conversation
69+
:Augment chat-toggle " Toggle the chat panel visibility
6870
```
6971
7072
## Chat

autoload/augment.vim

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,9 @@ function! s:CommandChat(range, args) abort
171171
let selected_text = ''
172172
endif
173173

174+
let uri = augment#chat#GetUri()
175+
let history = augment#chat#GetHistory()
176+
174177
" Use the message from the additional command arguments if provided, or
175178
" prompt the user for a message
176179
let message = empty(a:args) ? input('Message: ') : a:args
@@ -182,39 +185,46 @@ function! s:CommandChat(range, args) abort
182185
return
183186
endif
184187

185-
" Create new buffer for chat response
186-
let chat_bufname = 'AugmentChat-' . strftime("%Y%m%d-%H%M%S")
187-
let current_win = bufwinid(bufnr('%'))
188-
call augment#chat#CreateBuffer(chat_bufname)
189-
call win_gotoid(current_win)
188+
call augment#chat#OpenChatPanel()
189+
call augment#chat#AppendMessage(message)
190190

191191
call augment#log#Info(
192-
\ 'Making chat request in buffer ' . chat_bufname
193-
\ . ' with selected_text="' . selected_text
192+
\ 'Making chat request with file=' . uri
193+
\ . ' selected_text="' . selected_text
194194
\ . '"' . ' message="' . message . '"')
195195

196196
let params = {
197197
\ 'textDocumentPosition': {
198198
\ 'textDocument': {
199-
\ 'uri': 'file://' . expand('%:p'),
199+
\ 'uri': uri,
200200
\ },
201201
\ 'position': {
202202
\ 'line': line('.') - 1,
203203
\ 'character': col('.') - 1,
204204
\ },
205205
\ },
206206
\ 'message': message,
207-
\ 'partialResultToken': chat_bufname,
208207
\ }
209208

210-
" Add selected text if available
209+
" Add selected text and history if available
211210
if !empty(selected_text)
212211
let params['selectedText'] = selected_text
213212
endif
213+
if !empty(history)
214+
let params['history'] = history
215+
endif
214216

215217
call augment#client#Client().Request('augment/chat', params)
216218
endfunction
217219

220+
function! s:CommandChatNew(range, args) abort
221+
call augment#chat#Reset()
222+
endfunction
223+
224+
function! s:CommandChatToggle(range, args) abort
225+
call augment#chat#Toggle()
226+
endfunction
227+
218228
" Handle user commands
219229
let s:command_handlers = {
220230
\ 'log': function('s:CommandLog'),
@@ -224,6 +234,8 @@ let s:command_handlers = {
224234
\ 'disable': function('s:CommandDisable'),
225235
\ 'status': function('s:CommandStatus'),
226236
\ 'chat': function('s:CommandChat'),
237+
\ 'chat-new': function('s:CommandChatNew'),
238+
\ 'chat-toggle': function('s:CommandChatToggle'),
227239
\ }
228240

229241
function! augment#Command(range, args) abort range
@@ -261,6 +273,7 @@ endfunction
261273

262274
function! augment#OnBufEnter() abort
263275
call s:OpenBuffer()
276+
call augment#chat#SaveUri()
264277
endfunction
265278

266279
function! augment#OnTextChanged() abort

autoload/augment/chat.vim

Lines changed: 149 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,155 @@
33

44
" Utilities for chat
55

6+
function! s:ResetChatContents() abort
7+
let chat_buf = bufnr('AugmentChatHistory')
8+
if chat_buf == -1
9+
call augment#log#Error('Chat reset failed: Could not find chat history buffer')
10+
return
11+
endif
12+
13+
call setbufvar(chat_buf, '&modifiable', v:true)
14+
call deletebufline(chat_buf, 1, '$')
15+
call augment#chat#AppendText('# Augment Chat History'
16+
\ . "\n\n"
17+
\ . '`:Augment chat` Send a chat message in the current conversation'
18+
\ . "\n"
19+
\ . '`:Augment chat-new` Start a new conversation'
20+
\ . "\n"
21+
\ . '`:Augment chat-toggle` Toggle the chat panel visibility'
22+
\ . "\n\n")
23+
endfunction
24+
25+
function! augment#chat#Toggle() abort
26+
let chat_id = bufwinid('AugmentChatHistory')
27+
if chat_id == -1
28+
call augment#chat#OpenChatPanel()
29+
else
30+
" Don't close if it's the last window
31+
if winnr('$') > 1
32+
call win_execute(chat_id, 'close')
33+
endif
34+
endif
35+
endfunction
36+
37+
function! augment#chat#OpenChatPanel() abort
38+
let current_win = win_getid()
39+
40+
" Check if the panel already exists and has been setup
41+
if bufexists('AugmentChatHistory') && !getbufvar('AugmentChatHistory', '&modifiable')
42+
if bufwinid('AugmentChatHistory') == -1
43+
botright 80vnew AugmentChatHistory
44+
endif
45+
call win_gotoid(current_win)
46+
return
47+
endif
48+
49+
" Open a buffer for the chat history with a width of 80 characters
50+
botright 80vnew AugmentChatHistory
51+
setlocal buftype=nofile " Buffer will never be written to a file
52+
setlocal nomodifiable " Prevent any modifications
53+
setlocal noswapfile " Don't create a swapfile
54+
setlocal winfixbuf " Keep buffer in window when splitting
55+
setlocal bufhidden=hide " When buffer is abandoned, hide it
56+
setlocal nobuflisted " Hide from :ls
57+
setlocal wrap " Wrap long lines
58+
setlocal linebreak " Wrap at word boundaries
59+
setlocal filetype=markdown " Use markdown syntax highlighting
60+
setlocal nonumber " Hide line numbers
61+
setlocal norelativenumber " Hide relative line numbers
62+
setlocal signcolumn=no " Hide sign column
63+
setlocal nocursorline " Disable cursor line highlighting
64+
setlocal nospell " Disable spell checking
65+
setlocal nofoldenable " Disable folding
66+
setlocal textwidth=0 " Disable text width limit
67+
setlocal scrolloff=0 " Disable scrolloff
68+
69+
" Add the chat header to the buffer
70+
call s:ResetChatContents()
71+
72+
" TODO(AU-6480): create another buffer for the chat input
73+
" new AugmentChatInput
74+
75+
call win_gotoid(current_win)
76+
endfunction
77+
78+
function! augment#chat#Reset() abort
79+
call s:ResetChatContents()
80+
call s:ResetHistory()
81+
endfunction
82+
83+
function! s:ResetHistory() abort
84+
let g:_augment_chat_history = []
85+
endfunction
86+
87+
function! augment#chat#AppendText(text) abort
88+
let chat_buf = bufnr('AugmentChatHistory')
89+
if chat_buf == -1
90+
call augment#log#Error('Chat append failed: Could not find chat history buffer')
91+
return
92+
endif
93+
94+
let lines = split(a:text, "\n", v:true)
95+
let last_line = getbufline(chat_buf, '$')[0]
96+
97+
call setbufvar(chat_buf, '&modifiable', v:true)
98+
call setbufline(chat_buf, '$', last_line . lines[0])
99+
call appendbufline(chat_buf, '$', lines[1:])
100+
call setbufvar(chat_buf, '&modifiable', v:false)
101+
endfunction
102+
103+
function! augment#chat#AppendMessage(message) abort
104+
" If not the first message, scroll to the bottom
105+
let chat_id = bufwinid('AugmentChatHistory')
106+
if !empty(augment#chat#GetHistory()) && chat_id != -1
107+
let command = "call winrestview({'lnum': line('$'), 'topline': line('$')})"
108+
call win_execute(chat_id, command)
109+
endif
110+
111+
let message_text = '================================================================================'
112+
\ . "\n\n"
113+
\ . "\t*You*"
114+
\ . "\n\n"
115+
\ . a:message
116+
\ . "\n\n"
117+
\ . '--------------------------------------------------------------------------------'
118+
\ . "\n\n"
119+
\ . "\t*Augment*"
120+
\ . "\n\n"
121+
call augment#chat#AppendText(message_text)
122+
endfunction
123+
124+
function! augment#chat#AppendHistory(request_message, response_text, request_id) abort
125+
if !exists('g:_augment_chat_history')
126+
let g:_augment_chat_history = []
127+
endif
128+
call add(g:_augment_chat_history, {
129+
\ 'request_message': a:request_message,
130+
\ 'response_text': a:response_text,
131+
\ 'request_id': a:request_id,
132+
\ })
133+
endfunction
134+
135+
function! augment#chat#GetHistory() abort
136+
if exists('g:_augment_chat_history')
137+
return g:_augment_chat_history
138+
endif
139+
return []
140+
endfunction
141+
142+
function! augment#chat#SaveUri() abort
143+
if bufname('%') !=# 'AugmentChatHistory'
144+
let g:_augment_current_uri = 'file://' . expand('%:p')
145+
endif
146+
endfunction
147+
148+
function! augment#chat#GetUri() abort
149+
if exists('g:_augment_current_uri')
150+
return g:_augment_current_uri
151+
endif
152+
return 'file://' . expand('%:p')
153+
endfunction
154+
6155
function! s:GetBufSelection(line_start, col_start, line_end, col_end) abort
7156
if a:line_start == a:line_end
8157
return getline(a:line_start)[a:col_start - 1:a:col_end - 1]
@@ -63,15 +212,3 @@ function! augment#chat#GetSelectedText() abort
63212
let [line_end, col_end] = getpos("'>")[1:2]
64213
return s:GetBufSelection(line_start, col_start, line_end, col_end)
65214
endfunction
66-
67-
function! augment#chat#CreateBuffer(bufname) abort
68-
botright vnew
69-
setlocal buftype=nofile
70-
setlocal bufhidden=hide
71-
setlocal noswapfile
72-
setlocal wrap
73-
setlocal linebreak
74-
execute 'file ' . a:bufname
75-
setlocal readonly
76-
setlocal filetype=markdown
77-
endfunction

0 commit comments

Comments
 (0)