Skip to content

Commit

Permalink
redo buffer completion
Browse files Browse the repository at this point in the history
  • Loading branch information
prabirshrestha committed Jun 16, 2019
1 parent b8f00ea commit c431ec2
Showing 1 changed file with 27 additions and 34 deletions.
61 changes: 27 additions & 34 deletions autoload/asyncomplete/sources/buffer.vim
Original file line number Diff line number Diff line change
Expand Up @@ -3,60 +3,55 @@ 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: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: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': ['CursorHold','CursorHoldI','BufWinEnter','BufWritePost','TextChangedI'],
\ 'on_event': function('s:on_event'),
\}, a:opts)
\ 'events': ['BufWinEnter'],
\ 'on_event': function('s:on_event'),
\}, a:opts)
endfunction

let s:last_ctx = {}
function! s:on_event(opt, ctx, event) abort
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('ignoring buffer due to large size', expand('%:p'), l:buffer_size)
return
call asyncomplete#log('asyncomplete#sources#buffer', 'ignoring buffer autocomplete due to large size', expand('%:p'), l:buffer_size)
return 1
endif
endif

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
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
Expand All @@ -71,17 +66,15 @@ function! s:refresh_keywords() abort
let s:words[l:word] = 1
endif
endfor
call asyncomplete#log('asyncomplete#buffer', 's:refresh_keywords() complete')
endfunction

function! s:refresh_keyword_incr(typed) abort
function! s:refresh_keyword_incremental(typed) abort
let l:words = split(a:typed, '\W\+')
if len(l:words) > 1
for l:word in l:words[:len(l:words)-2]

for l:word in l:words
if len(l:word) > 1
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
endif
endfor
endfunction

0 comments on commit c431ec2

Please sign in to comment.