@@ -12,6 +12,11 @@ endfunction
1212
1313let s: NOT_RUNNING_MSG = ' The Augment language server is not running. See ":Augment log" for more details.'
1414
15+ " Get the text of the current buffer, accounting for the newline at the end
16+ function ! s: GetBufText () abort
17+ return join (getline (1 , ' $' ), " \n " ) . " \n "
18+ endfunction
19+
1520" Notify the server that a buffer has been opened
1621function ! s: OpenBuffer () abort
1722 if ! s: IsRunning ()
@@ -23,7 +28,7 @@ function! s:OpenBuffer() abort
2328 call luaeval (' require("augment").open_buffer(_A[1], _A[2])' , [client.client_id, bufnr (' %' )])
2429 else
2530 let uri = ' file://' . expand (' %:p' )
26- let text = join ( getline ( 1 , ' $ ' ), " \n " )
31+ let text = s: GetBufText ( )
2732 call client.Notify (' textDocument/didOpen' , {
2833 \ ' textDocument' : {
2934 \ ' uri' : uri,
@@ -51,7 +56,7 @@ function! s:UpdateBuffer() abort
5156 let b: _augment_buf_tick = b: changedtick
5257
5358 let uri = ' file://' . expand (' %:p' )
54- let text = join ( getline ( 1 , ' $ ' ), " \n " )
59+ let text = s: GetBufText ( )
5560 call augment#client#Client ().Notify (' textDocument/didChange' , {
5661 \ ' textDocument' : {
5762 \ ' uri' : uri,
@@ -96,12 +101,12 @@ function! s:RequestCompletion() abort
96101endfunction
97102
98103" Show the log
99- function ! s: CommandLog () abort
104+ function ! s: CommandLog (... ) abort
100105 call augment#log#Show ()
101106endfunction
102107
103108" Send sign-in request to the language server
104- function ! s: CommandSignIn () abort
109+ function ! s: CommandSignIn (... ) abort
105110 if ! s: IsRunning ()
106111 echohl WarningMsg
107112 echo s: NOT_RUNNING_MSG
@@ -113,7 +118,7 @@ function! s:CommandSignIn() abort
113118endfunction
114119
115120" Send sign-out request to the language server
116- function ! s: CommandSignOut () abort
121+ function ! s: CommandSignOut (... ) abort
117122 if ! s: IsRunning ()
118123 echohl WarningMsg
119124 echo s: NOT_RUNNING_MSG
@@ -124,15 +129,15 @@ function! s:CommandSignOut() abort
124129 call augment#client#Client ().Request (' augment/logout' , {})
125130endfunction
126131
127- function ! s: CommandEnable () abort
132+ function ! s: CommandEnable (... ) abort
128133 let g: augment_enabled = v: true
129134endfunction
130135
131- function ! s: CommandDisable () abort
136+ function ! s: CommandDisable (... ) abort
132137 let g: augment_enabled = v: false
133138endfunction
134139
135- function ! s: CommandStatus () abort
140+ function ! s: CommandStatus (... ) abort
136141 if ! s: IsRunning ()
137142 echohl WarningMsg
138143 echo s: NOT_RUNNING_MSG
@@ -143,6 +148,73 @@ function! s:CommandStatus() abort
143148 call augment#client#Client ().Request (' augment/status' , {})
144149endfunction
145150
151+ function ! s: CommandChat (range , args ) abort
152+ if exists (' g:augment_enabled' ) && ! g: augment_enabled
153+ echohl WarningMsg
154+ echo ' Augment: Not enabled. Run ":Augment enable" to enable the plugin.'
155+ echohl None
156+ return
157+ endif
158+
159+ if ! s: IsRunning ()
160+ echohl WarningMsg
161+ echo s: NOT_RUNNING_MSG
162+ echohl None
163+ return
164+ endif
165+
166+ " If range arguments were provided (when using :Augment chat) or in visual
167+ " mode, get the selected text
168+ if a: range == 2 || mode () == # ' v' || mode () == # ' V'
169+ let selected_text = augment#chat#GetSelectedText ()
170+ else
171+ let selected_text = ' '
172+ endif
173+
174+ " Use the message from the additional command arguments if provided, or
175+ " prompt the user for a message
176+ let message = empty (a: args ) ? input (' Message: ' ) : a: args
177+
178+ " Handle cancellation or empty input
179+ if message == # ' ' || message = ~# ' ^\s*$'
180+ redraw
181+ echo ' Chat cancelled'
182+ return
183+ endif
184+
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)
190+
191+ call augment#log#Info (
192+ \ ' Making chat request in buffer ' . chat_bufname
193+ \ . ' with selected_text="' . selected_text
194+ \ . ' "' . ' message="' . message . ' "' )
195+
196+ let params = {
197+ \ ' textDocumentPosition' : {
198+ \ ' textDocument' : {
199+ \ ' uri' : ' file://' . expand (' %:p' ),
200+ \ },
201+ \ ' position' : {
202+ \ ' line' : line (' .' ) - 1 ,
203+ \ ' character' : col (' .' ) - 1 ,
204+ \ },
205+ \ },
206+ \ ' message' : message,
207+ \ ' partialResultToken' : chat_bufname,
208+ \ }
209+
210+ " Add selected text if available
211+ if ! empty (selected_text)
212+ let params[' selectedText' ] = selected_text
213+ endif
214+
215+ call augment#client#Client ().Request (' augment/chat' , params)
216+ endfunction
217+
146218" Handle user commands
147219let s: command_handlers = {
148220 \ ' log' : function (' s:CommandLog' ),
@@ -151,24 +223,30 @@ let s:command_handlers = {
151223 \ ' enable' : function (' s:CommandEnable' ),
152224 \ ' disable' : function (' s:CommandDisable' ),
153225 \ ' status' : function (' s:CommandStatus' ),
226+ \ ' chat' : function (' s:CommandChat' ),
154227 \ }
155228
156- function ! augment#Command (command ) abort
157- if empty (a: command )
229+ function ! augment#Command (range , args ) abort range
230+ if empty (a: args )
158231 call s: command_handlers [' status' ]()
159232 return
160233 endif
161234
235+ let command = split (a: args )[0 ]
162236 for [name, Handler] in items (s: command_handlers )
163237 " Note that ==? is case-insensitive comparison
164- if a: command == ? name
165- call Handler ()
238+ if command == ? name
239+ " Call the command handler with the count of range arguments as
240+ " the first parameter, followed by the rest of the arguments to
241+ " the command as a single string
242+ let command_args = substitute (a: args , ' ^\S*\s*' , ' ' , ' ' )
243+ call Handler (a: range , command_args)
166244 return
167245 endif
168246 endfor
169247
170248 echohl WarningMsg
171- echo ' Augment: Unknown command: "' . a: command . ' "'
249+ echo ' Augment: Unknown command: "' . command . ' "'
172250 echohl None
173251endfunction
174252
0 commit comments