Repository navigation
Expand file tree
/
Copy pathvimini.vim
More file actions
581 lines (499 loc) · 15.7 KB
/
Copy pathvimini.vim
File metadata and controls
581 lines (499 loc) · 15.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
" Vimini: Google Gemini Integration for Vim
" Prevent the script from being loaded more than once.
if exists('g:loaded_vimini')
finish
endif
let g:loaded_vimini = 1
" Default configuration
" Configuration: API Key
" Priority:
" 1. g:vimini_api_key
" 2. ~/.vimini/api_key file
let s:api_key = get(g:, 'vimini_api_key', '')
if empty(s:api_key)
let s:api_key_path = expand('~/.config/gemini.token')
if filereadable(s:api_key_path)
try
let s:api_key = trim(readfile(s:api_key_path)[0])
catch
" Handle empty file case
let s:api_key = ''
endtry
endif
endif
" Configuration: Model name
let g:vimini_model = get(g:, 'vimini_model', 'gemini-2.5-flash')
" Configuration: Temperature for generation. Must be between 0.0 and 2.0.
let g:vimini_temperature = get(g:, 'vimini_temperature', v:null)
" Configuration: How to split new windows ('vertical' or 'horizontal').
let g:vimini_split_method = get(g:, 'vimini_split_method', 'vertical')
" Configuration: Log file
let g:vimini_log_file = get(g:, 'vimini_log_file', expand('~/.var/vimini/vimini.log'))
" Configuration: Logging on/off
let g:vimini_logging = get(g:, 'vimini_logging', 'off')
" Configuration: Default path for saved reviews
let g:vimini_review_path = get(g:, 'vimini_review_path', '')
let s:plugin_root_dir = fnamemodify(resolve(expand('<sfile>:p')), ':h')
py3 << EOF
from os.path import normpath, join
import vim
try:
# It's good practice to add the plugin's python directory to sys.path
# to ensure imports work correctly, especially for larger projects.
import sys
import os
plugin_root_dir = vim.eval('s:plugin_root_dir')
python_root_dir = normpath(join(plugin_root_dir, '..', 'python3'))
sys.path.insert(0, python_root_dir)
from vimini import main
api_key = vim.eval('s:api_key')
model = vim.eval('g:vimini_model')
log_file = vim.eval('g:vimini_log_file') if vim.eval('g:vimini_logging') == 'on' else None
main.initialize(api_key=api_key, model=model, logfile=log_file)
except Exception as e:
# Escape single quotes in the error message to prevent Vimscript errors
error_message = str(e).replace("'", "''")
vim.command(f"echoerr '[Vimini] Error: {error_message}'")
EOF
" Expose a function to list available models
function! ViminiListModels()
py3 << EOF
try:
from vimini import main
main.list_models()
except Exception as e:
error_message = str(e).replace("'", "''")
vim.command(f"echoerr '[Vimini] Error: {error_message}'")
EOF
endfunction
command! ViminiListModels call ViminiListModels()
" Expose a function to Chat with Gemini
function! ViminiChat(prompt)
py3 << EOF
try:
from vimini import main
prompt = vim.eval('a:prompt')
main.chat(prompt)
except Exception as e:
error_message = str(e).replace("'", "''")
vim.command(f"echoerr '[Vimini] Error: {error_message}'")
EOF
endfunction
command! -nargs=* ViminiChat call ViminiChat(string(<q-args>))
let g:vimini_thinking = get(g:, 'vimini_thinking', 'on')
" Expose a function to toggle thinking on and off
function! ViminiThinking(...)
let l:option = get(a:, 1, '')
" Handle explicit setting
if !empty(l:option)
if l:option ==# 'on' || l:
8000
option ==# 'off'
let g:vimini_thinking = l:option
else
echoerr "[Vimini] Invalid argument for ViminiThinking. Use 'on' or 'off'."
return
endif
" Handle toggling
else
if g:vimini_thinking ==# 'on'
let g:vimini_thinking = 'off'
else
let g:vimini_thinking = 'on'
endif
endif
echo "[Vimini] Thinking is now " . g:vimini_thinking
endfunction
command! -nargs=? ViminiThinking call ViminiThinking(<f-args>)
" Expose a function to toggle logging on and off
function! ViminiToggleLogging(...)
let l:option = get(a:, 1, '')
" Handle explicit setting
if !empty(l:option)
if l:option ==# 'on' || l:option ==# 'off'
let g:vimini_logging = l:option
else
echoerr "[Vimini] Invalid argument for ViminiToggleLogging. Use 'on' or 'off'."
return
endif
" Handle toggling
else
if g:vimini_logging ==# 'on'
let g:vimini_logging = 'off'
else
let g:vimini_logging = 'on'
endif
endif
" Call Python function to update logging state.
py3 << EOF
try:
from vimini import main
log_state = vim.eval('g:vimini_logging')
if log_state == 'on':
log_file = vim.eval('g:vimini_log_file')
main.logging(log_file)
else:
main.logging()
except Exception as e:
error_message = str(e).replace("'", "''")
vim.command(f"echoerr '[Vimini] Error setting log state: {error_message}'")
EOF
echo "[Vimini] Logging is now " . g:vimini_logging
endfunction
command! -nargs=? ViminiToggleLogging call ViminiToggleLogging(<f-args>)
" Expose a function to generate code with Gemini
function! ViminiCode(prompt)
py3 << EOF
try:
from vimini import main
prompt = vim.eval('a:prompt')
verbose = vim.eval('g:vimini_thinking') == 'on'
temperature = vim.eval("get(g:, 'vimini_temperature', v:null)")
main.code(prompt, verbose=verbose, temperature=temperature)
except Exception as e:
error_message = str(e).replace("'", "''")
vim.command(f"echoerr '[Vimini] Error: {error_message}'")
EOF
endfunction
command! -nargs=* ViminiCode call ViminiCode(string(<q-args>))
" Expose a function to apply the generated code
function! ViminiApply(...)
let l:job_id = v:null
if a:0 > 0
let l:job_id = a:1
endif
py3 << EOF
try:
from vimini import main
job_id_arg = vim.eval('l:job_id')
job_id = int(job_id_arg) if job_id_arg is not None else None
main.apply_code(job_id=job_id)
except Exception as e:
error_message = str(e).replace("'", "''")
vim.command(f"echoerr '[Vimini] Error: {error_message}'")
EOF
endfunction
command! -nargs=* ViminiApply call ViminiApply(<f-args>)
function! ViminiReview(args)
" Reviews git diffs. Git objects can be specified with "-c <refs>".
" A security-focused review can be requested with "--security".
" The rest of the arguments are treated as a prompt.
let l:git_objects_arg = v:null
let l:prompt_arg = ''
let l:security_focus = 0
let l:save_review = 0
let l:save_path = ''
let l:args = a:args
" Handle --save=path
let l:idx = 0
while l:idx < len(l:args)
if l:args[l:idx] =~# '^--save='
let l:save_path = substitute(l:args[l:idx], '^--save=', '', '')
call remove(l:args, l:idx)
" Specifying a path implies saving
let l:save_review = 1
continue
endif
let l:idx += 1
endwhile
let l:c_idx = index(l:args, '-c')
if l:c_idx != -1
if l:c_idx + 1 < len(l:args)
let l:git_objects_arg = l:args[l:c_idx + 1]
call remove(l:args, l:c_idx, l:c_idx + 1)
else
echoerr "[Vimini] Error: -c option requires an argument."
return
endif
endif
let l:s_idx = index(l:args, '--security')
if l:s_idx != -1
let l:security_focus = 1
call remove(l:args, l:s_idx)
endif
let l:save_idx = index(l:args, '--save')
if l:save_idx != -1
let l:save_review = 1
call remove(l:args, l:save_idx)
endif
let l:prompt_arg = join(l:args, ' ')
py3 << EOF
try:
from vimini import main
prompt = vim.eval('l:prompt_arg')
git_objects = vim.eval('l:git_objects_arg')
security_focus = bool(int(vim.eval('l:security_focus')))
save_review = bool(int(vim.eval('l:save_review')))
save_path = vim.eval('l:save_path')
verbose = vim.eval('g:vimini_thinking') == 'on'
temperature = vim.eval("get(g:, 'vimini_temperature', v:null)")
main.review(prompt, git_objects=git_objects, security_focus=security_focus, verbose=verbose, temperature=temperature, save=save_review, save_path=save_path)
except Exception as e:
error_message = str(e).replace("'", "''")
vim.command(f"echoerr '[Vimini] Error: {error_message}'")
EOF
endfunction
command! -nargs=* ViminiReview call ViminiReview([<f-args>])
" Expose a function to show git diff
function! ViminiDiff()
py3 << EOF
try:
from vimini import main
main.show_diff()
except Exception as e:
error_message = str(e).replace("'", "''")
vim.command(f"echoerr '[Vimini] Error: {error_message}'")
EOF
endfunction
command! ViminiDiff call ViminiDiff()
" Configuration: Commit author trailer
let g:vimini_commit_author = get(g:, 'vimini_commit_author', 'Co-authored-by: Gemini <gemini@google.com>')
" Expose a function to generate and execute a git commit
function! ViminiCommit(q_args)
let l:author = g:vimini_commit_author
let l:regenerate = 0
let l:other_args = []
let l:args = split(a:q_args)
for l:arg in l:args
if l:arg ==# '-n'
let l:author = v:null
elseif l:arg ==# '-r'
let l:regenerate = 1
else
call add(l:other_args, l:arg)
endif
endfor
let l:prompt_refinement = join(l:other_args, ' ')
py3 << EOF
try:
from vimini import main
author = vim.eval('l:author')
regenerate = bool(int(vim.eval('l:regenerate')))
refinement = vim.eval('l:prompt_refinement')
temperature = vim.eval("get(g:, 'vimini_temperature', v:null)")
main.commit(author=author, temperature=temperature, regenerate=regenerate, refinement=refinement)
except Exception as e:
error_message = str(e).replace("'", "''")
vim.command(f"echoerr '[Vimini] Error: {error_message}'")
EOF
endfunction
command! -nargs=* ViminiCommit call ViminiCommit(<q-args>)
" Expose a function to manage uploaded files
function! ViminiFiles()
py3 << EOF
try:
from vimini import main
main.files_command()
except Exception as e:
error_message = str(e).replace("'", "''")
vim.command(f"echoerr '[Vimini] Error: {error_message}'")
EOF
endfunction
command! -nargs=0 ViminiFiles call ViminiFiles()
" Expose a function to manage context files
function! ViminiContextFiles()
py3 << EOF
try:
from vimini import main
main.context_files_command()
except Exception as e:
error_message = str(e).replace("'", "''")
vim.command(f"echoerr '[Vimini] Error: {error_message}'")
EOF
endfunction
command! -nargs=0 ViminiContextFiles call ViminiContextFiles()
" Expose a function for autocompletion.
" This calls the non-blocking python function that handles the async request.
function! ViminiAutocomplete()
py3 << EOF
try:
from vimini import main
verbose = vim.eval('g:vimini_thinking') == 'on'
main.autocomplete(verbose)
except Exception as e:
error_message = str(e).replace("'", "''")
vim.command(f"echoerr '[Vimini] Error: {error_message}'")
EOF
endfunction
" Configuration: Autocomplete on/off
let g:vimini_autocomplete = get(g:, 'vimini_autocomplete', 'off')
let s:autocomplete_timer = -1
let s:autocomplete_queue_timer = -1
" Function to be called by the queue timer to process results from python
function! s:ProcessAutocompleteQueue(timer)
py3 << EOF
try:
from vimini import main
main.process_autocomplete_queue()
except Exception:
# Fail silently, this is called frequently
pass
EOF
endfunction
" Expose a function to toggle autocomplete on and off
function! ViminiToggleAutocomplete(...)
let l:option = get(a:, 1, '')
let l:old_state = g:vimini_autocomplete
" Handle explicit setting
if !empty(l:option)
if l:option ==# 'on' || l:option ==# 'off'
let g:vimini_autocomplete = l:option
else
echoerr "[Vimini] Invalid argument for ViminiToggleAutocomplete. Use 'on' or 'off'."
return
endif
" Handle toggling
else
if g:vimini_autocomplete ==# 'on'
let g:vimini_autocomplete = 'off'
else
let g:vimini_autocomplete = 'on'
endif
endif
if g:vimini_autocomplete ==# 'on'
" Start the queue processor timer if it's not already running
if s:autocomplete_queue_timer == -1
let s:autocomplete_queue_timer = timer_start(100, 's:ProcessAutocompleteQueue', {'repeat': -1})
endif
else " 'off'
call s:StopAutocompleteTimer() " Stops trigger timer and cancels jobs
" Also stop the queue processor timer
if s:autocomplete_queue_timer != -1
call timer_stop(s:autocomplete_queue_timer)
let s:autocomplete_queue_timer = -1
endif
endif
echo "[Vimini] Autocomplete is now " . g:vimini_autocomplete
endfunction
command! -nargs=? ViminiToggleAutocomplete call ViminiToggleAutocomplete(<f-args>)
function! s:CancelAutocomplete()
py3 << EOF
try:
from vimini import main
# This signals the python background thread to stop and clears the queue.
main.cancel_autocomplete()
except Exception:
# Fail silently. It's not critical if this fails.
pass
EOF
endfunction
function! s:StopAutocompleteTimer()
if exists('s:autocomplete_timer') && s:autocomplete_timer != -1
call timer_stop(s:autocomplete_timer)
let s:autocomplete_timer = -1
endif
" Cancel any running python autocomplete job.
call s:CancelAutocomplete()
endfunction
function! s:ResetAutocompleteTimer()
" Stop the previous timer and cancel any pending request.
call s:StopAutocompleteTimer()
if g:vimini_autocomplete ==# 'on' && mode() ==# 'i'
let s:autocomplete_timer = timer_start(1000, 's:TriggerAutocomplete')
endif
endfunction
function! s:TriggerAutocomplete(timer)
let s:autocomplete_timer = -1
if g:vimini_autocomplete ==# 'on' && mode() ==# 'i'
call ViminiAutocomplete()
endif
endfunction
augroup vimini_autocomplete
autocmd!
autocmd TextChangedI * call s:ResetAutocompleteTimer()
autocmd InsertLeave * call s:StopAutocompleteTimer()
augroup END
" --- Async Job Timers (Generic) ---
let s:job_timer = -1
function! ViminiInternalProcessJobQueue(timer)
py3 << EOF
try:
from vimini import util
util.process_queue()
except Exception:
pass
EOF
endfunction
function! ViminiInternalStartJobTimer()
if s:job_timer != -1
call timer_stop(s:job_timer)
endif
let s:job_timer = timer_start(50, 'ViminiInternalProcessJobQueue', {'repeat': -1})
endfunction
function! ViminiInternalStopJobTimer()
if s:job_timer != -1
call timer_stop(s:job_timer)
let s:job_timer = -1
endif
endfunction
" --- End Async Job Timers ---
" Expose ripgrep search functionality
function! s:ViminiRipGrep(q_args)
py3 << EOF
try:
from vimini import main
arg_string = vim.eval('a:q_args')
main.ripgrep_command(arg_string)
except Exception as e:
error_message = str(e).replace("'", "''")
vim.command(f"echoerr '[Vimini] Error: {error_message}'")
EOF
endfunction
command! -nargs=* ViminiRipGrep call s:ViminiRipGrep(<q-args>)
" Expose ripgrep apply functionality
function! ViminiRipGrepApply()
py3 << EOF
try:
from vimini import main
main.ripgrep_apply()
except Exception as e:
error_message = str(e).replace("'", "''")
vim.command(f"echoerr '[Vimini] Error: {error_message}'")
EOF
endfunction
command! -nargs=0 ViminiRipGrepApply call ViminiRipGrepApply()
" Expose help functionality
function! ViminiHelp(...)
let l:cmd = get(a:, 1, '')
py3 << EOF
try:
from vimini import main
cmd = vim.eval('l:cmd')
main.help(cmd)
except Exception as e:
error_message = str(e).replace("'", "''")
vim.command(f"echoerr '[Vimini] Error: {error_message}'")
EOF
endfunction
command! -nargs=? -complete=command ViminiHelp call ViminiHelp(<f-args>)
" --- Status Monitor ---
function! ViminiStatus()
py3 << EOF
try:
from vimini import main
main.status_command()
except Exception as e:
error_message = str(e).replace("'", "''")
vim.command(f"echoerr '[Vimini] Error: {error_message}'")
EOF
endfunction
command! ViminiStatus call ViminiStatus()
let s:status_timer = -1
function! ViminiInternalUpdateStatus(timer)
py3 << EOF
try:
from vimini import util
util.update_status_buffer()
except Exception:
pass
EOF
endfunction
function! ViminiInternalStartStatusTimer()
if s:status_timer == -1
let s:status_timer = timer_start(1000, 'ViminiInternalUpdateStatus', {'repeat': -1})
endif
endfunction
function! ViminiInternalStopStatusTimer()
if s:status_timer != -1
call timer_stop(s:status_timer)
let s:status_timer = -1
endif
endfunction