-
Notifications
You must be signed in to change notification settings - Fork 15
/
buffer.vim
70 lines (60 loc) · 1.87 KB
/
buffer.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
let s:words = {}
let s:last_word = ''
function! asyncomplete#sources#buffer#completor(opt, ctx)
if empty(s:words)
return
endif
let l:matches = []
let l:col = a:ctx['col']
let l:typed = a:ctx['typed']
let l:kw = matchstr(l:typed, '\w\+$')
let l:kwlen = len(l:kw)
if l:kwlen < 1
return
endif
let l:words = keys(s:words)
if !empty(s:last_word) && l:kw !=? s:last_word && !has_key(s:words, s:last_word)
let l:words += [s:last_word]
endif
let l:matches = map(l:words,'{"word":v:val,"dup":1,"icase":1,"menu": "[buffer]"}')
let l:startcol = l:col - l:kwlen
call asyncomplete#complete(a:opt['name'], a:ctx, l:startcol, l:matches)
endfunction
function! asyncomplete#sources#buffer#get_source_options(opts)
return extend(extend({}, a:opts), {
\ 'events': ['CursorHold','CursorHoldI','BufWinEnter','BufWritePost','TextChangedI'],
\ 'on_event': function('s:on_event'),
\ })
endfunction
let s:last_ctx = {}
function! s:on_event(opt, ctx, event) abort
if a:event == 'TextChangedI'
call s:refresh_keyword_incr(a:ctx['typed'])
else
if s:last_ctx == a:ctx
return
endif
let s:last_ctx = a:ctx
call s:refresh_keywords()
endif
endfunction
function! s:refresh_keywords() abort
let l:text = join(getline(1, '$'), "\n")
for l:word in split(l:text, '\W\+')
if len(l:word) > 1
let s:words[l:word] = 1
endif
endfor
endfunction
function! s:refresh_keyword_incr(typed) abort
let l:words = split(a:typed, '\W\+')
if len(l:words) > 1
for l:word in l:words[:len(l:words)-2]
let s:words[l:word] = 1
endfor
endif
if len(l:words) > 0
let l:new_last_word = l:words[len(l:words)-1:][0]
let s:last_word = l:new_last_word
endif
endfunction