-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathtern.vim
More file actions
200 lines (177 loc) · 6.04 KB
/
tern.vim
File metadata and controls
200 lines (177 loc) · 6.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
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
if !has('python') && !has('python3')
echo 'tern requires python support'
finish
endif
let s:plug = expand("<sfile>:p:h:h")
let s:script = s:plug . '/script/tern.py'
if has('python3')
execute 'py3file ' . fnameescape(s:script)
elseif has('python')
execute 'pyfile ' . fnameescape(s:script)
endif
if !exists('g:tern#command')
let g:tern#command = ["node", expand('<sfile>:h') . '/../node_modules/tern/bin/tern', '--no-port-file']
endif
if !exists('g:tern#arguments')
let g:tern#arguments = []
endif
function! tern#PreviewInfo(info)
pclose
let s:originalSplitBelow = &splitbelow
set splitbelow
new +setlocal\ previewwindow|setlocal\ buftype=nofile|setlocal\ noswapfile|setlocal\ wrap
exe "normal z" . &previewheight . "\<cr>"
call append(0, type(a:info)==type("") ? split(a:info, "\n") : a:info)
wincmd p
if (s:originalSplitBelow != 1)
set nosplitbelow
endif
endfunction
function! tern#Complete(findstart, complWord)
if a:findstart
if has('python3')
python3 tern_ensureCompletionCached()
elseif has('python')
python tern_ensureCompletionCached()
endif
return b:ternLastCompletionPos['start']
elseif b:ternLastCompletionPos['end'] - b:ternLastCompletionPos['start'] == len(a:complWord)
return b:ternLastCompletion
else
let rest = []
for entry in b:ternLastCompletion
if stridx(entry["word"], a:complWord) == 0
call add(rest, entry)
endif
endfor
return rest
endif
endfunction
function! tern#LookupType()
if has('python3')
python3 tern_lookupType()
elseif has('python')
python tern_lookupType()
endif
return ''
endfunction
function! tern#LookupArgumentHints()
if g:tern_show_argument_hints == 'no'
return
endif
let fname = get(matchlist(getline('.')[:col('.')-2],'\([a-zA-Z0-9_]*\)([^()]*$'),1)
let pos = match(getline('.')[:col('.')-2],'[a-zA-Z0-9_]*([^()]*$')
if pos >= 0
if has('python3')
python3 tern_lookupArgumentHints(vim.eval('fname'),int(vim.eval('pos')))
elseif has('python')
python tern_lookupArgumentHints(vim.eval('fname'),int(vim.eval('pos')))
endif
endif
return ''
endfunction
if !exists('g:tern_show_argument_hints')
let g:tern_show_argument_hints = 'no'
endif
if !exists('g:tern_show_signature_in_pum')
let g:tern_show_signature_in_pum = 0
endif
if !exists('g:tern_set_omni_function')
let g:tern_set_omni_function = 1
endif
if !exists('g:tern_map_keys')
let g:tern_map_keys = 0
endif
if !exists('g:tern_map_prefix')
let g:tern_map_prefix = '<LocalLeader>'
endif
if !exists('g:tern_request_timeout')
let g:tern_request_timeout = 1
endif
if !exists('g:tern_request_query')
let g:tern_request_query = {}
endif
if !exists('g:tern_show_loc_after_rename')
let g:tern_show_loc_after_rename = 1
endif
function! tern#DefaultKeyMap(...)
let prefix = len(a:000)==1 ? a:1 : "<LocalLeader>"
execute 'nnoremap <buffer> '.prefix.'tD' ':TernDoc<CR>'
execute 'nnoremap <buffer> '.prefix.'tb' ':TernDocBrowse<CR>'
execute 'nnoremap <buffer> '.prefix.'tt' ':TernType<CR>'
execute 'nnoremap <buffer> '.prefix.'td' ':TernDef<CR>'
execute 'nnoremap <buffer> '.prefix.'tpd' ':TernDefPreview<CR>'
execute 'nnoremap <buffer> '.prefix.'tsd' ':TernDefSplit<CR>'
execute 'nnoremap <buffer> '.prefix.'ttd' ':TernDefTab<CR>'
execute 'nnoremap <buffer> '.prefix.'tr' ':TernRefs<CR>'
execute 'nnoremap <buffer> '.prefix.'tR' ':TernRename<CR>'
endfunction
function! tern#Enable()
if stridx(&buftype, "nofile") > -1 || stridx(&buftype, "nowrite") > -1
return
endif
if has('python3')
command! -buffer TernDoc py3 tern_lookupDocumentation()
command! -buffer TernDocBrowse py3 tern_lookupDocumentation(browse=True)
command! -buffer TernType py3 tern_lookupType()
command! -buffer TernDef py3 tern_lookupDefinition("edit")
command! -buffer TernDefPreview py3 tern_lookupDefinition("pedit")
command! -buffer TernDefSplit py3 tern_lookupDefinition("split")
command! -buffer TernDefTab py3 tern_lookupDefinition("tabe")
command! -buffer TernRefs py3 tern_refs()
command! -buffer TernRename exe 'py3 tern_rename("'.input("new name? ",expand("<cword>")).'")'
elseif has('python')
command! -buffer TernDoc py tern_lookupDocumentation()
command! -buffer TernDocBrowse py tern_lookupDocumentation(browse=True)
command! -buffer TernType py tern_lookupType()
command! -buffer TernDef py tern_lookupDefinition("edit")
command! -buffer TernDefPreview py tern_lookupDefinition("pedit")
command! -buffer TernDefSplit py tern_lookupDefinition("split")
command! -buffer TernDefTab py tern_lookupDefinition("tabe")
command! -buffer TernRefs py tern_refs()
command! -buffer TernRename exe 'py tern_rename("'.input("new name? ",expand("<cword>")).'")'
endif
let b:ternProjectDir = ''
let b:ternLastCompletion = []
let b:ternLastCompletionPos = {'row': -1, 'start': 0, 'end': 0}
if !exists('b:ternBufferSentAt')
let b:ternBufferSentAt = undotree()['seq_cur']
endif
let b:ternInsertActive = 0
if g:tern_set_omni_function
setlocal omnifunc=tern#Complete
endif
if g:tern_map_keys
call tern#DefaultKeyMap(g:tern_map_prefix)
endif
augroup TernAutoCmd
autocmd! * <buffer>
if has('python3')
autocmd BufLeave <buffer> :py3 tern_sendBufferIfDirty()
elseif has('python')
autocmd BufLeave <buffer> :py tern_sendBufferIfDirty()
endif
if g:tern_show_argument_hints == 'on_move'
autocmd CursorMoved,CursorMovedI <buffer> call tern#LookupArgumentHints()
elseif g:tern_show_argument_hints == 'on_hold'
autocmd CursorHold,CursorHoldI <buffer> call tern#LookupArgumentHints()
endif
autocmd InsertEnter <buffer> let b:ternInsertActive = 1
autocmd InsertLeave <buffer> let b:ternInsertActive = 0
augroup END
endfunction
augroup TernShutDown
autocmd VimLeavePre * call tern#Shutdown()
augroup END
function! tern#Disable()
augroup TernAutoCmd
autocmd! * <buffer>
augroup END
endfunction
function! tern#Shutdown()
if has('python3')
py3 tern_killServers()
elseif has('python')
py tern_killServers()
endif
endfunction