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
|
function! s:RacerGetPrefixCol(base)
let col = col('.') - 1
let b:racer_col = col
let b:tmpfname = tempname()
call writefile(s:RacerGetBufferContents(a:base), b:tmpfname)
let cmd = g:racer_cmd . ' prefix ' . line('.') . ' ' . col . ' ' . b:tmpfname
let res = system(cmd)
let prefixline = split(res, '\n')[0]
let startbyte = split(prefixline[7:], ',')[0]
return startbyte - line2byte(byte2line(startbyte)) + 1
endfunction
function! s:RacerGetExpCompletions(base)
let col = col('.')-1
let b:tmpfname = tempname()
call writefile(s:RacerGetBufferContents(a:base), b:tmpfname)
let fname = expand('%:p')
let cmd = g:racer_cmd . ' complete ' . line('.') . ' ' . col . ' "' . fname . '" "' . b:tmpfname . '"'
let res = system(cmd)
let typeMap = {
\ 'Struct' : 's', 'Module' : 'M', 'Function' : 'f',
\ 'Crate' : 'C', 'Let' : 'v', 'StructField' : 'm',
\ 'Impl' : 'i', 'Enum' : 'e', 'EnumVariant' : 'E',
\ 'Type' : 't', 'FnArg' : 'v', 'Trait' : 'T'
\ }
let lines = split(res, '\n')
let out = []
for line in lines
if line !~# '^MATCH'
continue
endif
let completions = split(line[6:], ',')
let kind = get(typeMap, completions[4])
let completion = { 'kind' : kind, 'word' : completions[0], 'dup' : 1 }
let info = join(completions[5:], ',')
if kind ==# 'f'
" function
let completion['menu'] = substitute(
\ substitute(
\ substitute(info, '\(pub\|fn\) ', '', 'g'),
\ '{*$', '', ''
\ ),
\ ' where\s\?.*$', '', ''
\ )
if g:racer_insert_paren == 1
let completion['abbr'] = completions[0]
let completion['word'] .= '('
endif
let completion['info'] = info
elseif kind ==# 's' " struct
let completion['menu'] = substitute(
\ substitute(info, '\(pub\|struct\) ', '', 'g'),
\ '{*$', '', ''
\ )
endif
if stridx(tolower(completions[0]), tolower(a:base)) == 0
let out = add(out, completion)
endif
endfor
call delete(b:tmpfname)
return out
endfunction
function! s:RacerSplitLine(line)
let separator = ';'
let placeholder = '{PLACEHOLDER}'
let line = substitute(a:line, '\\;', placeholder, 'g')
let parts = split(line, separator)
let docs = substitute(
\ substitute(
\ substitute(
\ substitute(get(parts, 7, ''), '^\"\(.*\)\"$', '\1', ''),
\ '\\\"', '\"', 'g'
\ ),
\ '\\''', '''', 'g'
\ ),
\ '\\n', '\n', 'g'
\ )
let parts = add(parts[:6], docs)
let parts = map(copy(parts), 'substitute(v:val, ''{PLACEHOLDER}'', '';'', ''g'')')
return parts
endfunction
function! racer#ShowDocumentation()
let winview = winsaveview() " Save the current cursor position
" Move to the end of the word for the entire token to search.
" Move one char back to avoid moving to the end of the *next* word.
execute 'normal he'
let col = col('.')
let b:tmpfname = tempname()
" Create temporary file with the buffer's current state
call writefile(getline(1, '$'), b:tmpfname)
let fname = expand('%:p')
let cmd = g:racer_cmd . ' complete-with-snippet ' . line('.') . ' ' . col . ' ' . fname . ' ' . b:tmpfname
let res = system(cmd)
" Restore de cursor position
call winrestview(winview)
" Delete the temporary file
call delete(b:tmpfname)
let lines = split(res, '\n')
for line in lines
if line !~# '^MATCH'
continue
endif
let docs = s:RacerSplitLine(line[6:])[7]
if len(docs) == 0
break
endif
" Only open doc buffer if there're docs to show
let bn = bufnr('__doc__')
if bn > 0
let wi = index(tabpagebuflist(tabpagenr()), bn)
if wi >= 0
" If the __doc__ buffer is open in the current tab, jump to it
silent execute (wi+1) . 'wincmd w'
else
silent execute 'sbuffer ' . bn
endif
else
split '__doc__'
endif
setlocal nobuflisted
setlocal modifiable
setlocal noswapfile
setlocal buftype=nofile
silent normal! ggdG
silent $put=docs
silent normal! 1Gdd
setlocal nomodifiable
setlocal nomodified
setlocal filetype=rustdoc
break
endfor
endfunction
function! s:RacerGetCompletions(base)
let col = col('.') - 1
let b:tmpfname = tempname()
" HACK: Special case to offer autocompletion on a string literal
if getline('.')[:col-1] =~# '".*"\.$'
call writefile(['fn main() {', ' let x: &str = "";', ' x.', '}'], b:tmpfname)
let fname = expand('%:p')
let cmd = g:racer_cmd . ' complete 3 6 "' . fname . '" "' . b:tmpfname . '"'
else
call writefile(s:RacerGetBufferContents(a:base), b:tmpfname)
let fname = expand('%:p')
let cmd = g:racer_cmd . ' complete ' . line('.') . ' ' . col . ' "' . fname . '" "' . b:tmpfname . '"'
endif
let res = system(cmd)
let lines = split(res, '\n')
let out = []
for line in lines
if line !~# '^MATCH'
continue
endif
let completion = split(line[6:], ',')[0]
if stridx(tolower(completion), tolower(a:base)) == 0
let out = add(out, completion)
endif
endfor
call delete(b:tmpfname)
return out
endfunction
function! racer#GoToDefinition()
if s:ErrorCheck()
return
endif
let col = col('.') - 1
let b:racer_col = col
let fname = expand('%:p')
let tmpfname = tempname()
call writefile(getline(1, '$'), tmpfname)
let cmd = g:racer_cmd . ' find-definition ' . line('.') . ' ' . col . ' ' . fname . ' ' . tmpfname
let res = system(cmd)
let lines = split(res, '\n')
for line in lines
if res =~# ' error: ' && line !=# 'END'
call s:Warn(line)
elseif line =~# '^MATCH'
let linenum = split(line[6:], ',')[1]
let colnum = split(line[6:], ',')[2]
let fname = split(line[6:], ',')[3]
call s:RacerJumpToLocation(fname, linenum, colnum)
break
endif
endfor
call delete(tmpfname)
endfunction
function! s:RacerGetBufferContents(base)
" Re-combine the completion base word from omnicomplete with the current
" line contents. Since the base word gets remove from the buffer before
" this function is invoked we have to put it back in to out tmpfile.
let col = col('.') - 1
let buf_lines = getline(1, '$')
let line_contents = getline('.')
let buf_lines[line('.') - 1] =
\ strpart(line_contents, 0, col) .
\ a:base .
\ strpart(line_contents, col, len(line_contents))
return buf_lines
endfunction
function! s:RacerJumpToLocation(filename, linenum, colnum)
if a:filename == ''
return
endif
" Record jump mark
normal! m`
if a:filename != bufname('%')
try
exec 'keepjumps e ' . fnameescape(a:filename)
catch /^Vim\%((\a\+)\)\=:E37/
" When the buffer is not saved, E37 is thrown. We can ignore it.
endtry
endif
call cursor(a:linenum, a:colnum + 1)
" Center definition on screen
normal! zz
endfunction
function! racer#RacerComplete(findstart, base)
if a:findstart
if s:ErrorCheck()
return -1
endif
return s:RacerGetPrefixCol(a:base)
else
if s:ErrorCheck()
return []
endif
if g:racer_experimental_completer == 1
return s:RacerGetExpCompletions(a:base)
else
return s:RacerGetCompletions(a:base)
endif
endif
endfunction
function! s:Warn(msg)
echohl WarningMsg | echomsg a:msg | echohl NONE
endfunction
function! s:ErrorCheck()
if !executable(g:racer_cmd)
call s:Warn('No racer executable found in $PATH (' . $PATH . ')')
return 1
endif
endfunction
|