-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathvimini.vim
More file actions
158 lines (138 loc) · 4.04 KB
/
vimini.vim
File metadata and controls
158 lines (138 loc) · 4.04 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
" 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>))
" Expose a function to generate code with Gemini
function! ViminiCode(prompt)
py3 << EOF
try:
from vimini import main
prompt = vim.eval('a:prompt')
main.code(prompt)
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(prompt)
py3 << EOF
try:
from vimini import main
prompt = vim.eval('a:prompt')
main.review(prompt)
except Exception as e:
error_message = str(e).replace("'", "''")
vim.command(f"echoerr '[Vimini] Error: {error_message}'")
EOF
endfunction
command! -nargs=* ViminiReview call ViminiReview(string(<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()