-
Notifications
You must be signed in to change notification settings - Fork 0
/
python_pydiction.vim
169 lines (145 loc) · 5.25 KB
/
python_pydiction.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
if v:version < 700
echoerr "Pydiction requires vim version 7 or greater."
finish
endif
" Make the Tab key do python code completion:
inoremap <silent> <buffer> <Tab>
\<C-R>=<SID>SetVals()<CR>
\<C-R>=<SID>TabComplete('down')<CR>
\<C-R>=<SID>RestoreVals()<CR>
" Make Shift+Tab do python code completion in the reverse direction:
inoremap <silent> <buffer> <S-Tab>
\<C-R>=<SID>SetVals()<CR>
\<C-R>=<SID>TabComplete('up')<CR>
\<C-R>=<SID>RestoreVals()<CR>
if !exists("*s:GetPreviousWord")
function! s:GetPreviousWord()
let lig = getline(line('.'))
let lig = strpart(lig,0,col('.')-1)
let exp = matchstr(lig, '\<\k*\>\s*$')
let index = strridx(exp, '.')
if index == -1
return exp
else
return exp[0:index]
endfunction
endif
if !exists("*s:ParseCompleteInfo")
function! s:ParseCompleteInfo(pystring)
let mappings = {}
for entry in split(a:pystring, '!')
let key_kwd_pair = split(entry, '#')
if len(key_kwd_pair) >= 2
let key = key_kwd_pair[0]
let kwds = key_kwd_pair[1]
let kwd_list = split(kwds, '\$')
let mappings[key] = kwd_list
endif
endfor
return mappings
endfunction
endif
""" if it doesnt exist, parse the mapping dict
if !exists("w:mapping_dict")
let curr_file_name = expand('%:p')
let curr_path = expand('%:p:h')
let w:mapping_dict_string = system(printf('%s %s %s', g:py_parser_path, curr_file_name, curr_path))
let w:mapping_dict = s:ParseCompleteInfo(w:mapping_dict_string)
let w:test_mapping = w:mapping_dict
endif
if !exists("*s:TabComplete")
function! s:TabComplete(direction)
" Check if the char before the char under the cursor is an
" underscore, letter, number, dot or opening parentheses.
" If it is, and if the popup menu is not visible, use
" I_CTRL-X_CTRL-K ('dictionary' only completion)--otherwise,
" use I_CTRL-N to scroll downward through the popup menu or
" use I_CTRL-P to scroll upward through the popup menu,
" depending on the value of a:direction.
" If the char is some other character, insert a normal Tab:
if searchpos('[_a-zA-Z0-9.]\%#', 'nb') != [0, 0]
if !pumvisible()
return "\<C-X>\<C-U>"
else
if a:direction == 'down'
return "\<C-N>"
else
return "\<C-P>"
endif
endif
else
return "\<Tab>"
endif
endfunction
endif
if !exists("*g:PyCompleter")
function g:PyCompleter(findstart, base)
if a:findstart
let line = getline('.')
let start = col('.') - 1
while start > 0 && line[start - 1] =~ '\s'
let start -= 1
endwhile
while start > 0 && line[start - 1] =~ '\w'
let start -= 1
endwhile
return start
else
let res = []
if exists("w:mapping_dict")
let prev_keyword = s:GetPreviousWord()
" check if prev_keyword has dot, if so, remove it, other wise
" use global
let dot_index = strridx(prev_keyword, '.')
if dot_index == -1
let dict_name = '@GLOBAL'
else
let dict_name = prev_keyword[0 : dot_index-1]
endif
" TODO add check for dict_name existing
if has_key(w:mapping_dict, dict_name)
for m in w:mapping_dict[dict_name]
if m =~ '^' . a:base
call add(res, m)
endif
endfor
endif
endif
return res
endif
endfunction
endif
if !exists("*s:SetVals")
function! s:SetVals()
"let g:test_dict = {'word' : 'basiermoot', 'abbr': 'basier', 'menu' : 'basier_menu', 'info' : 'basier_info', 'kind' : 'f' }
"let g:results = []
"call add(g:results, g:test_dict)
" Save and change any config values we need.
" Temporarily change isk to treat periods and opening
" parenthesis as part of a keyword -- so we can complete
" python modules and functions:
let s:pydiction_save_isk = &iskeyword
setlocal iskeyword +=.
set completefunc=g:PyCompleter
" Save the ins-completion options the user has set:
let s:pydiction_save_cot = &completeopt
" Have the completion menu show up for one or more matches:
let &completeopt = "menu,menuone"
" Set the popup menu height:
let s:pydiction_save_pumheight = &pumheight
if !exists('g:pydiction_menu_height')
let g:pydiction_menu_height = 15
endif
let &pumheight = g:pydiction_menu_height
return ''
endfunction
endif
if !exists("*s:RestoreVals")
function! s:RestoreVals()
" Restore the user's initial values.
let &completeopt = s:pydiction_save_cot
let &pumheight = s:pydiction_save_pumheight
let &iskeyword = s:pydiction_save_isk
return ''
endfunction
endif