-
Notifications
You must be signed in to change notification settings - Fork 2
/
vimpy.vim
282 lines (249 loc) · 6.98 KB
/
vimpy.vim
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
" Vim Plugin to make navigation across python files easy.
"
" Author: Amit Dev
" Version: 0.1
" License: This file is placed in the public domain.
"
if exists("g:loaded_vimpy")
finish
endif
let g:loaded_vimpy = 1
if !has('python')
echo "Error: vimpy requires Vim compiled with python."
finish
endif
" Key Bindings
nnoremap <leader>om :call <SID>OpenModule()<CR>
nnoremap <leader>oc :call <SID>OpenClass()<CR>
nnoremap <leader>of :call <SID>OpenFun()<CR>
nnoremap <leader>gm :call <SID>GotoModule()<CR>
nnoremap <leader>gc :call <SID>GotoClass()<CR>
nnoremap <leader>gf :call <SID>GotoFun()<CR>
" Open new files in a split or buffer?
let s:EditCmd = "e"
" -- Implementation starts here - modify with care --
let s:bufdetails = { 'module' : ['~Module', 'Enter Module Name: ', '<SID>CloseModule'],
\ 'class' : ['~Class', 'Enter Class Name: ', '<SID>CloseClass'],
\ 'function' : ['~Function', 'Enter Function: ', '<SID>CloseFun'] }
au VimLeavePre * call s:WriteIndex()
au BufWritePost *.py,*.pyx call s:UpdateIndex()
python << endpython
import vim
import os
import sys
scriptdir = os.path.dirname(vim.eval('expand("<sfile>")'))
if not scriptdir.endswith('vimpy'):
scriptdir = os.path.join(scriptdir, 'vimpy')
sys.path.insert(0, scriptdir)
import storage
import vimpy
import tok
def _set_storage(path):
if os.path.exists(path):
st.init(path)
print 'Loaded %d modules which has %d classes and %d functions.' % st.counts()
else:
print 'Invalid Project File'
st = storage.storage('')
endpython
if !exists(":VimpyLoad")
command -nargs=1 -complete=file VimpyLoad :python _set_storage(<q-args>)
endif
if !exists(":VimpyCreate")
command -nargs=+ -complete=file VimpyCreate :python vimpy.start(<f-args>)
endif
fun! s:UpdateIndex()
python << endpython
if vim.current.buffer.name in st.paths:
vimpy.st = st
vimpy.parsefile(vim.current.buffer.name)
endpython
endfun
fun! s:WriteIndex()
python << endpython
st.close()
endpython
endfun
fun! s:GetModule(pfx)
python << endpython
pfx = vim.eval("a:pfx")
#print 'Matching %s in %d modules' % (pfx, len(st.modules.skeys))
matches = [i for i in st.modules.skeys if i.startswith(pfx)]
completions = [{'word' : i, 'menu' : st.modules.d[i]} for i in matches]
vim.command("let l:res = %r" % completions)
endpython
return l:res
endfun
fun! s:GetClass(pfx)
python << endpython
pfx = vim.eval("a:pfx")
matches = [i for i in st.classes.skeys if i.startswith(pfx)]
completions = [{'word' : i, 'menu' : st.classes.d[i][0]} for i in matches]
vim.command("let l:res = %r" % completions)
endpython
return l:res
endfun
fun! s:GetFun(pfx)
python << endpython
pfx = vim.eval("a:pfx")
matches = [i for i in st.functs.skeys if i.startswith(pfx)]
completions = [{'word' : i, 'menu' : st.functs.d[i][0]} for i in matches]
vim.command("let l:res = %r" % completions)
endpython
return l:res
endfun
fun! s:Completer(findstart, base, fn)
echo a:findstart
if a:findstart
let line = getline('.')
let start = col('.') - 1
while start > 0 && line[start - 1] =~ '[^ :]'
let start -= 1
endwhile
return start
else
return call (a:fn, [a:base])
endif
endfun
fun! VimpyCompleteModules(findstart, base)
return s:Completer(a:findstart, a:base, function('s:GetModule'))
endfun
fun! VimpyCompleteClasses(findstart, base)
return s:Completer(a:findstart, a:base, function('s:GetClass'))
endfun
fun! VimpyCompleteFuns(findstart, base)
return s:Completer(a:findstart, a:base, function('s:GetFun'))
endfun
fun! s:OpenBuf(type)
let bp = s:bufdetails[a:type]
exe "split " . bp[0]
setlocal buftype=nofile
setlocal bufhidden=hide
setlocal noswapfile
exe "normal i" . bp[1]
call feedkeys("C")
setlocal completeopt=longest,menu
exe 'inoremap <buffer> <silent> <cr> <cr><c-\><c-n>:call <SID>CloseBuf(function("' . bp[2] .'"))<cr>'
inoremap <buffer> <silent> <tab> <c-x><c-u>
endfun
function! s:OpenClass()
call s:OpenBuf('class')
setlocal completefunc=VimpyCompleteClasses
endfunction
function! s:OpenFun()
call s:OpenBuf('function')
setlocal completefunc=VimpyCompleteFuns
endfunction
function! s:OpenModule()
call s:OpenBuf('module')
setlocal completefunc=VimpyCompleteModules
endfunction
function! s:CloseBuf(fn)
let s = getline(1)
let ind = stridx(s, ':')
if ind != -1
let name = strpart(s, ind+1)
let pos = a:fn(name)
if pos != ''
exe "bdelete"
let ind = strridx(pos, ':')
let path = strpart(pos, 0, ind)
let line = strpart(pos, ind+1)
exe s:EditCmd . " " . path
call cursor(line, 0)
endif
else
exe "bdelete"
endif
endfunction
function! s:CloseModule(name)
let l:res = ''
python << endpython
k = vim.eval("a:name").strip()
if k in st.modules.d:
pth = st.modules.d[k]
vim.command("let l:res = '%s:1'" % pth)
endpython
return l:res
endfunction
function! s:CloseClass(name)
let l:res = ''
python << endpython
k = vim.eval("a:name").strip()
if k in st.classes.d:
(_, pth, line) = st.classes.d[k]
#TODO: Check if moving to class name col is better
vim.command("let l:res = '%s:%d'" % (pth, line))
endpython
return l:res
endfunction
function! s:CloseFun(name)
let l:res = ''
python << endpython
k = vim.eval("a:name").strip()
if k in st.functs.d:
(_, pth, line) = st.functs.d[k]
#TODO: Check if moving to class name col is better
vim.command("let l:res = '%s:%d'" % (pth, line))
endpython
return l:res
endfunction
python << endpython
def open_file(match, path, get):
vim.command("unlet! l:res")
line = vim.current.line
pos = vim.current.window.cursor[1]
word = tok.get_token(line, pos)
if word:
word = get(word)
lw = len(word)
matches = [i for i in match.skeys if i.startswith(word)]
matches = [i for i in matches if len(i) == lw or i[lw] == ' ']
if len(matches) == 1:
_, pth, line = path(word)
vim.command("e %s" % pth)
if line:
vim.current.window.cursor = (line, 0)
elif len(matches) > 1:
vim.command("let l:res = '%s'" % word)
else:
print 'No match!'
else:
print 'No match!'
endpython
function! s:GotoModule()
python << endpython
open_file(st.modules,
lambda p: (None, st.modules.d[p], None),
lambda w: "%s%s" % (w, '.py'))
endpython
if exists("l:res")
call s:OpenModule()
call feedkeys(l:res)
call feedkeys("\t")
endif
endfunction
function! s:GotoClass()
python << endpython
open_file(st.classes,
lambda p: st.classes.d[p],
lambda w: w)
endpython
if exists("l:res")
call s:OpenClass()
call feedkeys(l:res)
call feedkeys("\t")
endif
endfunction
function! s:GotoFun()
python << endpython
open_file(st.functs,
lambda p: st.functs.d[p],
lambda w: w)
endpython
if exists("l:res")
call s:OpenFun()
call feedkeys(l:res)
call feedkeys("\t")
endif
endfunction