-
Notifications
You must be signed in to change notification settings - Fork 12
/
buffer.vim
80 lines (64 loc) · 2.19 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
71
72
73
74
75
76
77
78
79
80
let s:words = {}
let s:last_word = ''
let g:asyncomplete_buffer_clear_cache = get(g:, 'asyncomplete_buffer_clear_cache', 1)
function! asyncomplete#sources#buffer#completor(opt, ctx)
let l:typed = a:ctx['typed']
call s:refresh_keyword_incremental(l:typed)
if empty(s:words)
return
endif
let l:matches = []
let l:col = a:ctx['col']
let l:kw = matchstr(l:typed, '\w\+$')
let l:kwlen = len(l:kw)
let l:matches = map(keys(s: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({
\ 'events': ['BufWinEnter'],
\ 'on_event': function('s:on_event'),
\}, a:opts)
endfunction
function! s:should_ignore(opt) abort
let l:max_buffer_size = 5000000 " 5mb
if has_key(a:opt, 'config') && has_key(a:opt['config'], 'max_buffer_size')
let l:max_buffer_size = a:opt['config']['max_buffer_size']
endif
if l:max_buffer_size != -1
let l:buffer_size = line2byte(line('$') + 1)
if l:buffer_size > l:max_buffer_size
call asyncomplete#log('asyncomplete#sources#buffer', 'ignoring buffer autocomplete due to large size', expand('%:p'), l:buffer_size)
return 1
endif
endif
return 0
endfunction
let s:last_ctx = {}
function! s:on_event(opt, ctx, event) abort
if s:should_ignore(a:opt) | return | endif
if a:event == 'BufWinEnter'
call s:refresh_keywords()
endif
endfunction
function! s:refresh_keywords() abort
if g:asyncomplete_buffer_clear_cache
let s:words = {}
endif
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
call asyncomplete#log('asyncomplete#buffer', 's:refresh_keywords() complete')
endfunction
function! s:refresh_keyword_incremental(typed) abort
let l:words = split(a:typed, '\W\+')
for l:word in l:words
if len(l:word) > 1
let s:words[l:word] = 1
endif
endfor
endfunction