-
Notifications
You must be signed in to change notification settings - Fork 66
/
complete.vim
247 lines (191 loc) · 6.37 KB
/
complete.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
" A wiki plugin for Vim
"
" Maintainer: Karl Yngve Lervåg
" Email: karl.yngve@gmail.com
"
function! wiki#complete#omnicomplete(findstart, base) abort " {{{1
if a:findstart
return wiki#complete#findstart(getline('.')[:col('.') - 2])
else
return wiki#complete#complete(a:base)
endif
endfunction
" }}}1
function! wiki#complete#url(lead, line, pos) abort " {{{1
let l:parts = split(a:lead, '::')
if empty(l:parts) || len(l:parts) > 1 | return [] | endif
let l:base = ''
let l:input = l:parts[0]
let l:cnum = s:completer_wikilink.findstart('[[' . l:input) - 2
if l:cnum > 0
let l:base = l:input[:l:cnum-1]
let l:input = l:input[l:cnum:]
endif
let l:candidates = s:completer_wikilink.complete(l:input)
call map(l:candidates, 'v:val.word')
if !s:completer_wikilink.is_anchor
return s:filter_candidates(l:candidates, '^' . l:input)
endif
return map(l:candidates, 'l:base . v:val')
endfunction
" }}}1
function! wiki#complete#tag_names(lead, line, pos) abort " {{{1
return wiki#tags#get_tag_names()
endfunction
" }}}1
function! wiki#complete#findstart(line) abort " {{{1
if exists('s:completer') | unlet s:completer | endif
for l:completer in s:completers
let l:cnum = l:completer.findstart(a:line)
if l:cnum >= 0
let s:completer = l:completer
return l:cnum
endif
endfor
" -2 cancel silently and stay in completion mode.
" -3 cancel silently and leave completion mode.
return -3
endfunction
" }}}1
function! wiki#complete#complete(input) abort " {{{1
if !exists('s:completer') | return [] | endif
return s:completer.complete(a:input)
endfunction
" }}}1
"
" Completers
"
" {{{1 WikiLink
let s:completer_wikilink = {
\ 'is_anchor': 0,
\ 'rooted': 0,
\}
function! s:completer_wikilink.findstart(line) dict abort " {{{2
let l:cnum = match(a:line, '\[\[\zs[^\\[\]]\{-}$')
if l:cnum < 0 | return -1 | endif
let l:base = a:line[l:cnum:]
let self.rooted = l:base[0] ==# '/'
let self.is_anchor = l:base =~# '#'
if !self.is_anchor | return l:cnum | endif
let self.base = substitute(l:base, '\(.*#\).*', '\1', '')
return l:cnum + strlen(self.base)
endfunction
function! s:completer_wikilink.complete(regex) dict abort " {{{2
let l:candidates = self.is_anchor
\ ? self.complete_anchor(a:regex)
\ : self.complete_page(a:regex)
return map(l:candidates, "{'word': v:val, 'menu': '[wiki]'}")
endfunction
function! s:completer_wikilink.complete_anchor(regex) dict abort " {{{2
let l:url = wiki#url#resolve(self.base)
let l:base = '#' . (empty(l:url.anchor) ? '' : l:url.anchor . '#')
let l:length = strlen(l:base)
let l:anchors = wiki#toc#gather_anchors(l:url)
call s:filter_candidates(l:anchors, '^' . wiki#u#escape(l:base) . '[^#]*$')
call map(l:anchors, 'strpart(v:val, l:length)')
if !empty(a:regex)
call s:filter_candidates(l:anchors, a:regex)
endif
return l:anchors
endfunction
function! s:completer_wikilink.complete_page(regex) dict abort " {{{2
let l:root = self.rooted ? b:wiki.root : expand('%:p:h')
let l:pre = self.rooted ? '/' : ''
if executable('fd')
let l:extensions = ' -e ' . join(g:wiki_filetypes, ' -e ')
let l:cands = wiki#jobs#capture(
\ printf("fd -Ia -t f %s . '%s'", l:extensions, l:root))
else
let l:cands = []
for l:ext in g:wiki_filetypes
let l:cands += globpath(l:root, '**/*.' . l:ext, 0, 1)
endfor
endif
call map(l:cands, 'strpart(v:val, strlen(l:root)+1)')
call map(l:cands,
\ empty(wiki#link#get_creator('url_extension'))
\ ? 'l:pre . fnamemodify(v:val, '':r'')'
\ : 'l:pre . v:val')
call s:filter_candidates(l:cands, a:regex)
call sort(l:cands)
return l:cands
endfunction
" }}}1
" {{{1 MdLink
let s:completer_mdlink = deepcopy(s:completer_wikilink)
function! s:completer_mdlink.findstart(line) dict abort " {{{2
let l:cnum = match(a:line, '\](\zs[^)]\{-}$')
if l:cnum < 0 | return -1 | endif
let l:base = a:line[l:cnum:]
let self.rooted = l:base[0] ==# '/'
let self.is_anchor = l:base =~# '#'
if !self.is_anchor | return l:cnum | endif
let self.base = substitute(l:base, '\(.*#\).*', '\1', '')
return l:cnum + strlen(self.base)
endfunction
" }}}1
" {{{1 AdocBracketLink
let s:completer_adocbracketlink = deepcopy(s:completer_wikilink)
function! s:completer_adocbracketlink.findstart(line) dict abort " {{{2
let l:cnum = match(a:line, '<<\zs[^,]\{-}$')
if l:cnum < 0 | return -1 | endif
let l:base = a:line[l:cnum:]
let self.rooted = l:base[0] ==# '/'
" Completion of anchors is disabled because they won't be found for asciidoc
" anyway: wiki#rx#header_items regex is used for search of anchors and it
" requires Markdown-style headers. This could be resolved by passing a
" specific regular expression to wiki#toc#gather_anchors().
let self.is_anchor = 0
return l:cnum
endfunction
" }}}1
" {{{1 Zotero
let s:completer_zotero = {}
function! s:completer_zotero.findstart(line) dict abort " {{{2
return match(a:line, '\%(zot:\|\%(\s\|^\|\[\)@\)\zs\S*$')
endfunction
function! s:completer_zotero.complete(regex) dict abort " {{{2
let l:cands = map(wiki#zotero#search(a:regex), 'fnamemodify(v:val, '':t'')')
return map(sort(l:cands), "{
\ 'word': split(v:val)[0],
\ 'menu': join(split(v:val)[2:]),
\ 'kind': '[z]'
\}")
endfunction
" }}}1
" {{{1 Tags
let s:completer_tags = {}
function! s:completer_tags.findstart(line) dict abort " {{{2
for l:parser in g:wiki_tag_parsers
if !has_key(l:parser, 're_findstart') | continue | endif
let l:col = match(a:line, l:parser.re_findstart)
if l:col > 0 | return l:col | endif
endfor
return -1
endfunction
function! s:completer_tags.complete(regex) dict abort " {{{2
let l:candidates = keys(wiki#tags#get_all())
call s:filter_candidates(l:candidates, a:regex)
return map(sort(l:candidates), {_, x -> {
\ 'word': x,
\ 'kind': '[tag]'
\}})
endfunction
" }}}1
"
" Utility
"
function s:filter_candidates(cands, regex) " {{{1
" Filter list a:cands for a match with a:regex anywhere in items, with
" case-sensitivity depending on the value of g:wiki_completion_case_sensitive
call filter(a:cands, {_,x -> match(x,
\ (g:wiki_completion_case_sensitive ? '\C' : '\c')
\ . a:regex) >= 0})
endfunction
" }}}1
"
" Initialize module
"
let s:completers = map(
\ filter(items(s:), 'v:val[0] =~# ''^completer_'''),
\ 'v:val[1]')