Repository navigation
Expand file tree
/
Copy pathvimini.vim
More file actions
327 lines (285 loc) · 9 KB
/
Copy pathvimini.vim
File metadata and controls
327 lines (285 loc) · 9 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
" plugin/vimini.vim
" Main entry point for vimini
" 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')
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')
main.initialize(api_key=api_key, model=model)
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: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 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'
main.code(prompt, verbose)
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:option = get(a:, 1, '')
py3 << EOF
try:
from vimini import main
option = vim.eval('l:option')
if option == 'append':
main.append_code()
else:
main.apply_code()
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(q_args)
" Reviews git diffs. The first argument can be a git object if prefixed
" with "C:". The rest of the arguments are treated as a prompt.
" If no "C:" argument is found, all arguments form the prompt.
let l:git_objects_arg = v:null
let l:prompt_arg = ''
let l:args = split(a:q_args) " Parse the string from <q-args> into arguments
if !empty(l:args)
let l:first_arg = l:args[0]
" Check if the first argument is a git object reference
if strpart(l:first_arg, 0, 2) ==# 'C:'
let l:git_objects_arg = strpart(l:first_arg, 2)
" The rest of the arguments form the prompt
if len(l:args) > 1
let l:prompt_arg = join(l:args[1:], ' ')
endif
else
" All arguments form the prompt
let l:prompt_arg = join(l:args, ' ')
endif
endif
py3 << EOF
try:
from vimini import main
prompt = vim.eval('l:prompt_arg')
git_objects = vim.eval('l:git_objects_arg')
verbose = vim.eval('g:vimini_thinking') == 'on'
main.review(prompt, git_objects=git_objects, verbose=verbose)
except Exception as e:
error_message = str(e).replace("'", "''")
vim.command(f"echoerr '[Vimini] Error: {error_message}'")
EOF
endfunction
command! -nargs=* ViminiReview call ViminiReview(<q-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()
" Expose a function to generate and execute a git commit
function! ViminiCommit()
py3 << EOF
try:
from vimini import main
main.commit()
except Exception as e:
error_message = str(e).replace("'", "''")
vim.command(f"echoerr '[Vimini] Error: {error_message}'")
EOF
endfunction
command! ViminiCommit call ViminiCommit()
" 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, '')
7CA6
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